Go Down

Topic: serial read echo and servo (Read 920 times) previous topic - next topic

THX_RoG

i want to write code for my arduino for echo communication but have some problem
the code should look more or less like this
if i send to serial monitor "L90" he should turn my servo to left 90 degrees
can some one help?
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

PaulS

What have you tried? Doing what you want is trivial. There are dozens of examples posted every data for reading serial data and storing it in an array. There are daily examples for parsing that data. There are daily examples of converting a string to an int.

Which of them have you failed to find? Which did you fail to find because you failed to look?

liudr

Indeed Paul. Also, without YOUR code, we can't help.

THX_RoG

u help some how and now have some problem with verifying
Code: [Select]
error: invalid conversion from 'int*' to 'int'
and this is the error in code
Code: [Select]
int nextVal = serialValues;
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

AWOL

We don't do keyhole surgery.
Post your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

THX_RoG

Code: [Select]
char inData[10];
int index;
boolean started = false;
boolean ended = false;
boolean final = false;
int serialValues[16];
int serialIndex = 0;
const int Pin13 =  13;
const int Pin12 =  12;
const int Pin11 =  11;
const int Pin10 =  10;
int buttonState1 = 0;
int buttonState2 = 0;


  void setup()
    {
    //Serial1.begin(9600);
    Serial.begin(9600);

    //setting pinouts
    pinMode(Pin13, OUTPUT);
    pinMode(Pin12, OUTPUT);
    pinMode(Pin11, INPUT);
    pinMode(Pin10, INPUT);
    }

  void loop()
    {
      buttonState1 = digitalRead(Pin11);
      buttonState2 = digitalRead(Pin10);
     
      while(Serial.available() > 0)
        {
         char aChar = Serial.read();
         if(aChar == '<')
           {
             started = true;
             index = 0;
             inData[index] = '\0';
           }
         else if(aChar == '>')
           {
             ended = true;
           }
       
           else if(started)
           {
             inData[index] = aChar;
             index++;
             inData[index] = '\0';             
             }
         
           else if (aChar =='*')
             {
               final = true;
             }
        }
 

      if(started && ended)
        {
         // Convert the string to an integer
         int inInt = atoi(inData);
           Serial.println(inInt);
         // Use the value
           serialValues [serialIndex] = inInt;
         serialIndex++;
         
           // Get ready for the next time
         started = false;
         ended = false;

         index = 0;
         inData[index] = '\0';     
        }
       
        if(final)
            while(1)
            {
            // loop through the serialValues, and do something
            for(int b=0; b <16;b++ )
             {
             
                int nextVal = serialValues;
                //if serial value is 24 and pin10 is connected to 5V switch led12 on
                if (nextVal == 24 && buttonState2 == HIGH)
                   {
                   digitalWrite(Pin12, HIGH);                                                       
                   }
                 //if serial value is 12 and pin11 is connected to 5V switch led13 on
                 if (nextVal == 12 && buttonState2 == LOW)
                   {
                   digitalWrite(Pin12, LOW);                                                       
                 }
                  Serial.println(b);
                   delay(1000);
             }
            }
    }
   
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

AWOL

Code: [Select]
int nextVal = serialValues[b];?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

THX_RoG

dude i find my self embarrassed
but no one know everything from start
and now the game begin
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up