1

how do i change the if statement in arduino in order to move the servo the same degrees as whats randomized in python?

First he's the python code:

import serial
import time
import random

arduinoData = serial.Serial('com4',9600)     
while True:
    low = 0; high = 180
    ran_number = random.randint(low, high)
    print ran_number
    time.sleep(1)

    arduinoData.write(ran_number) 

python code is fine works with other things.

Now the arduino code, fix this:

#include <Servo.h>
Servo myservo;

int data;
int pin=9;
int pos = 0;

void setup() { 
  myservo.attach(9);
  pinMode(pin, OUTPUT); 
  digitalWrite (pin, LOW);
  Serial.begin(9600);
}

void loop() {
while (Serial.available()){
  data = Serial.read();
}
if statement here.....                       
  }              
}

1 Answer 1

0

What you are looking for is the ParseInt method. Instead of using read in a loop and constructing your number, Serial.ParseInt does exactly that for you.

The correct code would be:

#include <Servo.h>

Servo myservo;

int pin = 9;

void setup() {
  myservo.attach(9);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  Serial.setTimeout(100);
  Serial.begin(9600);
}

void loop() {
  int number;

  if (Serial.available()) {
    number = Serial.parseInt();
    myservo.write(number);
    delay(0.5);
  }
}

Notice that I set the serial timeout period to 100ms. This is to prevent parseInt to wait too much before deciding that it has read an entire int; otherwise when receiving a value (say, 42) it waits for about 1 second waiting for some other digits.

The Python script also has a problem or two. First, you should wait for a bit after establishing the connection because every time the serial is opened the Arduino resets, so it won't read the first couple of values. You could also get a cleaner solution by printing some string from the Arduino when ready (as the last instruction of the setup function, for example) and wait for it in the Python script with readLine.

Secondly, arduinoData.write takes a byte array as input, whereas you pass an int. You need to encode such int in bytes, first converting it to a string with str and then encoding it with encode.

I got the following program working:

import serial
import time
import random

arduinoData = serial.Serial('/dev/ttyUSB0',9600)  

# Let Arduino some time to reset
time.sleep(2)

while True:
    low = 0; high = 180
    ran_number = random.randint(low, high)
    print(ran_number)
    arduinoData.write(str(ran_number).encode())

    time.sleep(1)
9
  • sorry ignore my if statement. I understand your code provided, it makes sense, but i couldnt make it work. You mentioned to use Serial.ParseInt but you didnt use it in the code you provided! i did do simpler versions now am trying this. am kinda new to this Jul 16, 2018 at 9:21
  • You are absolutely correct; I kept using Serial.read() instead of Serial.parseInt(). I edited the example accordingly now.
    – Maldus
    Jul 16, 2018 at 10:11
  • the servo keeps going to 90 degrees and then stops moving Jul 16, 2018 at 10:22
  • It appears Python serial library also writes a newline with the string when you use write(); it is possible that parseInt is reading the correct number, then a newline that is interpreted as a 0 (therefore going back to start). I've edited the example again to move the servo once the newline is read. Also, try to sleep for 3 seconds in the python script; 1 might not be enough for the servo to move.
    – Maldus
    Jul 16, 2018 at 10:41
  • Tried all, now its also not moving 90 degrees. Just sitting still. Just a note that I did try the whole randomizing number and having the servo move from 0 to 180 degrees or any degree when using : for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees Jul 16, 2018 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.