Reading serial data and moving servo

hi – I think I have overcome my basic arduino issues but still can’t get one (continuous motion) servo to respond at all to incoming bytes.

  #include <Servo.h> 
  Servo myservo;   
  int pos = 0;    // variable to store the servo position 
  byte incomingByte;
  
  
  void setup() 
  { 
    myservo.attach(9);  
    Serial.begin(9600);
    int incomingByte = 0;     
   } 
  
  
  void loop() { 
  
    if (Serial.available() > 0) {   // read the incoming byte:
      incomingByte = Serial.read();
  
      if(incomingByte = 1) {              // 1 = value that is coming from processing
        for(pos = 0; pos < 180; pos += 6)  // i want servo to move backward 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(100);                       // waits 100ms for the servo to reach the position 
        } 
      }
  
  
      if(incomingByte = 2){  // 2 = value that is coming from processing
        for(pos = 180; pos >=1; pos -=6)     //I want servo to twitch in place 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(100);                       // waits 100ms for the servo to reach the position 
        } 
      }
  
      if(incomingByte = 3){  // 3 = value that is coming from processing
        for(pos = 180; pos >=1; pos -=6)     //I want servo to move FORWARD 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(100);                       // waits 100ms for the servo to reach the position 
        } 
      }
    }
  }

My processing code (I know this isn’t the place for it but just to say) seems to be working well, this is how it’s sending data:

void ShowDominantColor(int count)
{
   switch (count) {

    case 1: println("move back");  break; 
    case 2: println("hover");   break;
    case 3: println("move forward x");      break;
    case 4: println("move forward x2");      break;

    }

Firstly, would the if statement condition: (incomingByte = 1) need to be: (incomingByte == 1) for logic?

Also, serial data is read in as a string in ASCII, so would numbers need to be converted from ASCII to an integer using atoi()

I thought that it might be == as well but got my hand slapped for using it in an earlier query. I tied it both ways and there was no apparent change. I should say, the code compiles and uploads fine, it just doesn't do anything.

The Processing code you posted doesn't tell us a thing.

The Arduino code has several issues. The = vs. == thing that hworyd pointed out is one of them. Whether the ASCII issue is relevant is unknown, since we don't know what is being sent the the Arduino. The atoi() proposed solution is rubbish since you only have one character at a time to consider, and atoi requires a string.

If the character you are getting is a '1' (which is not the same thing as a 1), you need to compare incomingByte to '1', not 1, or you need to subtract '0' from incomingByte before comparing it with 1, 2, 3, etc.

The biggest problem though is that the thing you are trying to move is NOT a servo. Once the things ability to stop at a specific point has been removed, it is not a servo anymore. It can still be controlled by the Arduino, but it can not be commanded to go to a specific position and stop.

Continuous rotation no-longer-servos are commanded using servo.writeMicroseconds(). A value around 1500 will make the not-a-servo stop. Values less than 1500 will make it move one way, while values above 1500 will make it move the other way. The larger the difference between the value and 1500, the faster the servo moves, up to difference of around 700. All values need to be experimentally determined.

I thought that it might be == as well but got my hand slapped for using it in an earlier query.

You got your hand slapped earlier for trying to use == where = should have been used. Each has a specific meaning, and specific times and places where they should be used.

thanks Paul, every hand slap is useful, I’m not complaining, just trying to learn. I’ll change up the code and see what happens. I attach my processing code.

color_recog_1.pde (2.47 KB)

That code creates a connection to the Arduino, called port. You never write a thing to port. It's no wonder the Arduino doesn't do anything.

ah. what is the command line for writing to port? Or should I change the processing code?

ah. what is the command line for writing to port?

Gee, do you suppose it might be port.write()?

Or should I change the processing code?

Well, let's think about that for a minute. It doesn't work, which doesn't suit your needs. So, you could either fix it or leave it as is. Let me think about that for a while, and get back to you later.