#include <Servo.h> #include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservoE;
int elevator = 40;
int CodeIn, elevatormovement;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myservoE.attach(3);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
elevator = constrain (elevator, 0,100);
myservoE.write(elevator);
if (Serial.available() > 0)
{
CodeIn=getChar();
if (CodeIn == '<')
{
lcd.print(
int returnValue= LESSTHAN();
if (returnValue == 1)
{
elevator ++;
myservoE.write(elevator);
}
else if (returnValue ==0)
{
elevator --;
myservoE.write(elevator);
}
}
}
}
char getChar()// The serial buffer routine to get a character
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());// Thanks Doug
}// end of getchar Roputine.
int LESSTHAN()
{
CodeIn=getChar();
switch (CodeIn)
{
case 'Q':
delay(11);
elevatormovement += char (Serial.read());
if (elevatormovement == '-') //pitch up
{
digitalWrite (13, HIGH);
return 1;
}
else if (elevatormovement == '+')//pitch down
{
digitalWrite(13,LOW);
return 0;
}
}
}
How do I allow continues feedback/return value ? The return value only sends out once and stops.
Separate the code for receiving data from the code for controlling your servo. That way you can test each part on its own. The code in loop() could be as simple as
Hi Guys,
Appreciated all the feedback given, as i am new to Arduino. I am trying to store the first character received by the serial port to elevatormovevement. This character would be either a + or - . After receiving the char (+ or - ) i would return a 1 or 0 accordingly. So with the return value either 1 or 0 . i would compare it with an int variable (returnValue) and it should turn the servo accordingly. However the problem i am facing now is that it only return the first char and it stops return the rest. Is there anyway to ensure that the returning of 0 and 1 is continuous and do not stop at the first return?
Thanks helping!
As Nick pointed out, the problem is that you're adding it to that variable... which is of integer type....
So the first time you get a + or -, it works, because the variable starts out as 0. The second time, however, since it starts with the old value, and you add the value of the received character to it, it's a value that's neither '+' nor '-'.
Thanks DrAzzy for the reply!
So my mistake was that i did not cleared my first value of the first time? How does changing the += to = helps in clearing the first value of the first time ?