-1

I wrote this simple script to control a servo over serial comm -

#include <Servo.h>
Servo steeringServo;
char data = ' ';

void setup() {
  steeringServo.attach(9);
  Serial.begin(115200);

}

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

  if(data =='L'){
    steeringServo.write(50);
  }
  else if(data == 'R'){
    steeringServo.write(135);
  }
  else if(data == 'S'){
    steeringServo.write(90);
  }


}

It was working fine, I was sending values through a python script. I turned my Arduino off and after a while I come back and reconnect all stuff exactly the way it was, but now my Arduino isn't responding! I tried sending values through the serial monitor to the above sketch but nothing happens! I tried another code which takes values over serial comm and that didn't work as well. The Arduino just receives the values but doesn't do what its supposed to when it receives.

Not only this, I tried uploading the sweep sketch and that didn't work either. Everything is connected exactly the way it was when stuff was fine.

What has gone wrong with my Arduino?

UPDATE
The Arduino works fine if the values are sent through my script.

UPDATE 2
The Arduino works fine if the values are sent through puTTY.

  • What script? Possibly when you turned it off and on again it went to a different serial port. Check which serial port shows up in the IDE. – Nick Gammon Jul 3 '16 at 22:07
  • Does the script give you some errors? Something like "cannot open COM port" or "COM port busy". Also, try to run your python scripts in administrator mode. – FrAxl93 Jul 3 '16 at 22:59
  • @FrAxl93 no the script doesn't give any errors. Tried running in admin mode, ran flawlessly – YaddyVirus Jul 3 '16 at 23:22
  • @NickGammon Nah, I thought that initially too the Serial Port is the same. – YaddyVirus Jul 3 '16 at 23:22
1

You never reset data. Every time you enter loop, data keep the last value you read.

You don't need to test data in every loop, only after you read something:

void loop()
{
    if(Serial.available()) {
        data = Serial.read();

        if(data =='L') {
            steeringServo.write(50);
        } else if(data == 'R') {
            steeringServo.write(135);
        } else if(data == 'S') {
            steeringServo.write(90);
        }
    }

I'm afraid that there are additional problems with the servo library: some implementations (looking at Servo-master) disable interrupts, which mess with your Serial port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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