Hello all!
I'm getting strated with serial programming, but there seems to be a problem: the arduino due has the incorrect clock.
I am using an UNO to read PWM signals a serial line to send over the data to the Due.
I have some code that seems to be exhibiting this:
DUE code:
#include <Servo.h>
Servo esc;
byte buff[17];
byte currentRead[17];
short currentplace = 0;
char incoming;
int pwm1, pwm2, pwm3, pwm4;
void setup() {
// put your setup code here, to run once:
Serial.begin(230400);//Console Output
Serial1.begin(230400);//UNO Input
esc.attach(2);
delay(10);
Serial.println("start");
}
void loop() {
}
int parseSerial(String st, int ch){
return(st.substring((ch-1)*4, (ch*4)-1).toInt());
}
void serialEvent1(){
while(Serial1.available()){
incoming = Serial1.read();
Serial.println("serial reading.... ");
Serial.println(currentplace);
if(incoming == '\n'){
Serial.println("resetting ");
currentplace = 0;
Serial.println("parsing... ");
parsepwms();
for(char c : currentRead){
c = 0;
}
}else{
currentRead[currentplace] = incoming;
}
currentplace++;
}
}
void parsepwms(){
String concatination;
for(char c : currentRead){
concatination += c;
}
pwm1 = concatination.substring(0,3).toInt();
pwm2 = concatination.substring(4,7).toInt();
pwm3 = concatination.substring(8,11).toInt();
pwm4 = concatination.substring(11,15).toInt();
Serial.print("parsed.... ");
Serial.print(pwm1);
Serial.print(" ");
Serial.print(pwm2);
Serial.print(" ");
Serial.print(pwm3);
Serial.print(" ");
Serial.print(pwm4);
Serial.println();
}
UNO Code:
volatile long DutyCycle[8];
long lastmicros[]={0,0,0,0,0,0,0,0};
int pins[] = {2,3,4,5,6,7,8,9,10};
//long times[8];
boolean state[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
long it = 0;
void setup() {
Serial.begin(230400);
for(int i : pins){
pinMode(pins[i], INPUT);
state[i] = false;
}
}
void loop() {
//it++;
long mic = micros();
DutyCycle[0] = (pulseIn(pins[0], HIGH)+DutyCycle[0]*3)/4;
DutyCycle[1] = (pulseIn(pins[1], HIGH)+DutyCycle[1]*3)/4;
DutyCycle[2] = (pulseIn(pins[2], HIGH)+DutyCycle[2]*2)/3;
DutyCycle[3] = (pulseIn(pins[3], HIGH)+DutyCycle[3]*3)/4;
float delta = (float (micros()-mic)/1000000);
if(delta > 1 || DutyCycle[0] < 500 ||DutyCycle[1] < 500||DutyCycle[2] < 500||DutyCycle[3] < 500){
Serial.println("Radio Off.");
}else{
Serial.print("");
Serial.print(DutyCycle[0]);
Serial.print("");
Serial.print(DutyCycle[1]);
Serial.print("");
Serial.print(DutyCycle[2]);
Serial.print("");
Serial.println(DutyCycle[3]);
}
}
The problem here is that the Due pick up no numbers in the serial In, and in the console serial I have to set the baud to 1 level higher (250000) than the rate I set it into in the code