Pages: [1]   Go Down
Author Topic: serial read echo and servo  (Read 873 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 616
Posts: 49444
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

u help some how and now have some problem with verifying
Code:
error: invalid conversion from 'int*' to 'int'
and this is the error in code
Code:
int nextVal = serialValues;
Logged

UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We don't do keyhole surgery.
Post your code.
Logged

"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.

Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
             }
            }
    }
   
Logged

UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int nextVal = serialValues[b];
?
Logged

"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.

Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dude i find my self embarrassed
but no one know everything from start
and now the game begin
Logged

UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Entirely agree with you.
Logged

"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.

Pages: [1]   Go Up
Jump to: