Serial commands not working

hey there
i got small project here, for feeding pets. its working all fine, except serial commands - it giving me bad command and numbers (not sure where numbers come from)

15:26:09.632 -> bad command
15:26:09.632 -> Command: 102
15:26:10.147 -> bad command
15:26:10.147 -> Command: 101
15:26:10.658 -> bad command
15:26:10.658 -> Command: 101
15:26:11.171 -> bad command
15:26:11.171 -> Command: 110
15:26:11.685 -> bad command
15:26:11.685 -> Command: 10

here is my code. thanks.

#include <Servo.h>
#include <string.h>
#include <Wire.h>
#include <iarduino_RTC.h>
iarduino_RTC time(RTC_DS1307);

String hrs;
String mins;
String timenow;

int Data;

int angle = 112; 
Servo myservo;  // create servo object to control a servo
const int servoPWcontrol = 5;     // the number of serwo porwe control 


void setup() 
{  
  delay(300);
  Serial.begin(9600);
  
  time.begin();
           
    myservo.attach(9);
    myservo.write(angle); 
    pinMode(servoPWcontrol, OUTPUT);
    digitalWrite(servoPWcontrol, LOW);   // Turn powersource od servo OFF
    
    Serial.println("Type Command (feednow, gettime, RTC.setTime(yy:mm:dd:dow:hh:mm:ss)");
}

void loop()
{
  timenow=time.gettime("H:i:s");
  delay(500);
   digitalWrite(servoPWcontrol, LOW);
   if(timenow=="10:00:00")
    {  
    digitalWrite(servoPWcontrol, HIGH);
    delay(1000);
    myservo.write(70);
    delay(1500);
    myservo.write(112);
    delay(1000);
    digitalWrite(servoPWcontrol, LOW);
    Serial.println("They always hungry...");
  }  
     if(Serial.available()){
     Data = Serial.read();
     if (Data == "feednow") {
     digitalWrite(servoPWcontrol, HIGH);
     delay(1000);
    myservo.write(70);
    delay(1500);
    myservo.write(112);
    delay(1000);
    digitalWrite(servoPWcontrol, LOW);
      Serial.println("They always hungry...");
    }
    else if (Data == "gettime") {
      Serial.println(time.gettime("H:i:s"));
    }
    else {
      Serial.println("bad command");
    }
     Serial.print("Command: ");
    Serial.println(Data);
         }
}

Read up on serial comma. Serial.read(), for example, returns one character, if available, at a time.

It would be easier and you could get this working if you just looked for single characters at a time.

     int aChar = Serial.read();
     if (aChar == 'F') { // whatever

This ignores the -1 you get when there are no characters waiting, and does the if thing when you enter a capital F.

Note the use of 'F', a character, not "F" or "feed now", strings…

If you really want to have to type "feednow" then read up more, you can acquire strings and compare them and stuff.

But do the character thing for the time being.

Read up on serial comms. Serial.read(), for example, returns one character, if available, at a time.

It would be easier and you could get this working if you just looked for single characters at a time.

     int aChar = Serial.read();
     if (aChar == 'F') { // whatever

This ignores the -1 you get when there are no characters waiting, and does the if thing when you enter a capital F.

Note the use of 'F', a character, not "F" or "feed now", strings…

If you really want to have to type "feednow" then read up more, you can acquire strings and compare them and stuff.

But do the character thing for the time being.

a7

1 Like

get rid of the delay()s, and consider those alongside comparing times precisely to the second…. a good chance those won’t ever be true.

delays not issue here. and its needed for motor got time to rotate on position and else..

got you. i changed code, as you suggested. its working now with letters only. but still for future, i want to know, why even that letters giving me bad command with numbers.

If you say so.

To be more accurate, it works with single characters, and would be fine for seeing a character that was a digit.

If you are trying to see "words" or numbers like 42777, you'll need to learn up on gathering the characters as they are read, or look into functions in the Serial area that help you get numbers from the character stream.

Start here

Personally I prefer to keep things simple… why make someone type feedthecat when F would say the same thing?

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.