Hello, Thanks Nick!
Yes Serial.available() returns the number of characters right?
And I thought using recursion is effective in this situation(processor wise?). Well that didn't work though.
/* RX
* State machine
* 2 servos, esc. extend if a rudder is needed, currently using a servo for esc as well
*/
#include <Servo.h>
#define laileron 11
#define raileron 10
#define throttle 9
typedef enum{NONE,GOT_L,GOT_R,GOT_S} states;
Servo left,right,esc;
states state = NONE;
unsigned int currentValue;
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600); //1152200 only 9600 works, check whats wrong
state=NONE;
left.attach(laileron);
right.attach(raileron);
esc.attach(throttle);
}
void processLeft(const unsigned int value)
{
left.write(value);
//Serial.print(1);
//delay(10);
}
void processRight(const unsigned int value)
{
right.write(value);
//Serial.print(1);
//delay(10);
}
void processEsc(const unsigned int value)
{
esc.write(value);
//Serial.print(1);
//delay(10);
}
void hanldlePreviousState()
{
switch(state)
{
case GOT_L:
processLeft(currentValue);
break;
case GOT_R:
processRight(currentValue);
break;
case GOT_S:
processEsc(currentValue);
break;
}
currentValue=0;
}
void processIncomingByte(const byte c)
{
if(isdigit(c))
{
currentValue *= 10;
currentValue += c-'0';
}
else
{
hanldlePreviousState();
switch(c)
{
case 'L': state=GOT_L;
break;
case 'R': state=GOT_R;
break;
case 'S': state=GOT_S;
break;
default: state=NONE;
}
}
}
void defaults()
{
/*left.write(90);
right.write(90);
esc.write(0);*/
digitalWrite(13,HIGH);
}
/*void signalLossProtocol(int attempts)
{
if(Serial.available()<=0)
{
attempts++;
delay(500);
if(attempts<11)
signalLossProtocol(attempts);
else
{
defaults();
while(1>0);//exit(0); forum advices not to do so. check if works
}
}
}*/
void signalLossProtocol ()
{
for (int attempts = 0; attempts < 10; attempts++)
{
if (Serial.available () > 0)
return; // we have data
delay (500);
} // end of for
defaults ();
}
void loop()
{
if(Serial.available()>0) //>0 not needed?
processIncomingByte(Serial.read());
else
signalLossProtocol();
}
/* TX
* using elevons?
* have 2 servos for 2 elevons, ignore aileron.
*/
#define aileron 2
#define elevon 3
#define esc 4
#define elevontrim 0
#define ailerontrim 1
#define esctrim 5
#define signalLED 13
int ailerontrimVal,elevontrimVal,aileronVal,elevonVal,esctrimVal,escVal;
void setup()
{
Serial.begin(9600);//1152200 only 9600 works, check XBEE forum
pinMode(signalLED,OUTPUT);
}
void loop()
{
aileronVal = analogRead(aileron);
aileronVal = map(aileronVal, 0, 1023, 0, 179);
ailerontrimVal = analogRead(ailerontrim);
ailerontrimVal = map(ailerontrimVal, 0, 1023, -25, 25);
aileronVal = aileronVal+ ailerontrimVal;
constrain(aileronVal, 0, 179);
elevonVal = analogRead(elevon);
elevonVal = map(elevonVal, 0, 1023, 0, 179);
elevontrimVal = analogRead(elevontrim);
elevontrimVal = map(elevontrimVal, 0, 1023, -25, 25);
elevonVal = elevonVal+ elevontrimVal;
constrain(elevonVal, 0, 179);
escVal = analogRead(esc);
escVal = map(escVal, 0, 1023, 0, 179);
esctrimVal = analogRead(esctrim);
esctrimVal = map(esctrimVal, 0, 1023, -25, 25);
escVal = escVal+ esctrimVal;
constrain(escVal, 0, 179);
Serial.print("L");
Serial.print(aileronVal);
Serial.print("R");
Serial.print(elevonVal);
Serial.print("S");
Serial.print(escVal);
delay(10);
/*if(Serial.available())
{
if(Serial.read() == 1)
digitalWrite(signalLED,HIGH);
else
digitalWrite(signalLED,LOW);
}*/
}
This is my whole code. Please ignore the commented parts. I am trying to work with 2 things here. What I need to accomplish now is that if i loose signal the plane should come to a default state.
I tried my recursion and your loop too. But my servos keep going at random places now, even i don't have to push the joystick, it does go on changing its value. When I comment out the signalLossProtocol() method, it works like before. I still have a problem in the code.
Anyhow I am delaying (10ms) the Serial.print() part in the TX. At that very time we don't have a Serial input for the RX and we wait 500ms for the next input. Could this lead to a problem?
If I don't delay at the TX the RX doesn't read the Serial as I need it do be.
Thanks,
Bhashithe