我对Arduino的编码相对较新,因此希望能获得一些帮助,以使我重回正确的道路。我想要实现的是将值(在txt文件的每一行上一个值)从计算机上的文件发送到Arduino-Due(希望我已经通过使用处理代码来解决此问题,该处理代码通过以下方式将数据作为字符串发送)我目前正在苦苦挣扎的是让Arduino读取字符串,将其转换为浮点值,然后从DAC1引脚输出特定电压。任何帮助将不胜感激。
Arudino代码:
const byte buffSize = 40;
char inputBuffer[buffSize];
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
float currentVal = 0.0;
float writingValue();
//=============
void setup() {
Serial.begin(9600); //Initialises serial port at 9600 Baud rate
analogWriteResolution(12); //Sets analog write resolution (number of bits)
analogReadResolution(12); //Sets analog write resolution (number of bits)
pinMode(DAC1,OUTPUT); //Sets DAC1 pin as an input
analogWrite(DAC1,(0)); //Writes the initial analog value as 0
}
//=============
void loop() {
getDataFromPC();
currentValToPSU();
}
//=============
void getDataFromPC() {
// receive data from PC and save it into inputBuffer
if(Serial.available() > 0) {
char x = Serial.read();
if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
}
}
//=============
void parseData() {
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(NULL, "\n");
currentVal = atof(strtokIndx); // convert this part to a float
}
//=============
void currentValToPSU() {
analogWrite(DAC1,(4095*((currentVal-0.0927)/7.959)));
}
处理代码:
import processing.serial.*;
Serial comPort;
int counter=0; // Helps to keep track of values sent.
int numItems=0; //Keep track of the number of values in text file
boolean sendStrings=false; //Turns sending on and off
StringLoader sLoader; //Used to send values to Arduino
void setup(){
comPort = new Serial(this, Serial.list()[0], 9600);
background(255,0,0); //Start with a Red background
}
void draw(){
}
void mousePressed() {
//Toggle between sending values and not sending values
sendStrings=!sendStrings;
//If sendStrings is True - then send values to Arduino
if(sendStrings){
background(0,255,0); //Change the background to green
/*When the background is green, transmit text file values to the Arduino */
sLoader=new StringLoader();
sLoader.start();
}else{
background(255,0,0); //Change background to red
//Reset the counter
counter=0;
}
}
/*============================================================*/
/* The StringLoader class imports data from a text file
on a new Thread and sends each value once every half second */
public class StringLoader extends Thread{
public StringLoader(){
//default constructor
}
public void run() {
String textFileLines[]=loadStrings("processingData.txt");
numItems=textFileLines.length;
for(int i = counter; i<numItems; i++){
comPort.write(textFileLines[i]);
delay(5000);
}
counter=numItems;
}
}