Commands

I am making a sketch that will allow me to use serial to command my pins, and yes I know there’s problems with the analog section, this is because I can’t even use the digital just goes to invalid. I would like it to continue through the digital section well cause that’s the point

char incomeWord[1];  //State variable incomeWord
int errorPin = 13;  //State the error display pin
int inputPins[] = {7,8};  //State input pins
int numInputs = 2;  //State number of input pins
int outputPins[] = {2,3,4,5,6,9,10,11,12,13};  //State output pins
int numOutputs = 10; //State number of outputs
int pinDigital;
int pinAnalog;

void setup() {
  Serial.begin(9600);  //Starts serial at 9600 baud
  for(int i = 1;i <= numInputs;i++) {
    pinMode(inputPins[i],INPUT);
  }
  for(int w = 1;w <= numOutputs;w++) {
    pinMode(outputPins[w],OUTPUT);
  }
  pinMode(errorPin, OUTPUT);
}

void loop() {
  if(Serial.available() > 0) {
    delay(100);
    for(int i = 0;Serial.available();i++) {
      incomeWord = Serial.read();
    }
    Serial.print(incomeWord);
    if(incomeWord == "digital") {
      Serial.println("Please enter a valid pin.");
      while(Serial.available() == 0) {delay(1);}
      for(int i = 0;Serial.available();i++) {
        incomeWord[i] = Serial.read();
      }
      pinDigital = int(incomeWord);
      Serial.print("Turn pin ");
      Serial.print(pinDigital);
      Serial.println(" on or off?");
      while(Serial.available() == 0) {delay(1);}
      for(int i = 0;Serial.available();i++) {
        incomeWord[i] = Serial.read();
      }
      if(incomeWord == "on") {
        Serial.print("Turning on pin ");
        Serial.print(pinDigital);
        Serial.println(".");
        digitalWrite(pinDigital, HIGH);
      } else if(incomeWord == "off") {
        Serial.print("Turning off pin ");
        Serial.print(pinDigital);
        Serial.println(".");
        digitalWrite(pinDigital, LOW);
      } else {
        Serial.println("Invalid!");
        digitalWrite(errorPin, HIGH);
        delay(2000);
        digitalWrite(errorPin, LOW);
      }
    } else if(incomeWord == "analog") {
      Serial.println("Please enter a valid pin.");
      while(Serial.available() == 0) {delay(1);}
      for(int i = 0;Serial.available();i++) {
        incomeWord[i] = Serial.read();
      }
      pinAnalog = int(incomeWord);
    } else {
      Serial.println("Invalid!");
      digitalWrite(errorPin,HIGH);
      delay(2000);
      digitalWrite(errorPin,LOW);
    }
  }
}

Moderator edit: Italics removed . AWOL

char incomeWord[1];  //State variable incomeWord

If it only has one char in the array, then you don't really need the 1. It's just a char.

  if(Serial.available() > 0) {
    delay(100);
    for(int i = 0;Serial.available();i++) {
      incomeWord = Serial.read();
    }

So we keep reading the next byte and putting into the char variable incomeWord until serial.Available() fails. If we keep overwriting that same char, what do you expect it to have at the end?

if(incomeWord == "digital")
well you only declared it to have one char, and then you only put one char in it, how is it going to equal all seven of those letters.

Serial.println(" on or off?");
      while(Serial.available() == 0) {delay(1);}
      for(int i = 0;Serial.available();i++) {
        incomeWord[i] = Serial.read();

Well it's getting closer. But you still only declared incomeWord to have one element. So this is going to start overwriting other things now.

Ok I set it to 500 and it does the same thing

if(incomeWord == "digital")

You can't compare two arrays of char with ==. That code will compare the addresses of the two arrays which of course will never be the same.

Use strcmp()

if(strcmp (incomeWord, "digital") == 0)

Rob

PoLoMoTo:
Ok I set it to 500 and it does the same thing

It?

Can you please edit your post and put the sketch in code tags? (Select it and hit the "#" button). It is impossible to read when things like [ i ] become italic.

Simple code to control a pin.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;


void setup() {
	Serial.begin(9600);
        pinMode(13, OUTPUT);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(2);  
    	char c = Serial.read();
        readString += c; 
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
     
    if (readString == "on") {
	digitalWrite(13, HIGH);
        Serial.println("Led On");
    }
   if (readString == "off") {
	digitalWrite(13, LOW);
        Serial.println("Led Off");
    }
        readString="";
   } 
}

Array indices run from zero, not one.

    if (readString == "off") {

This is still fundamentally flawed. Serial comms is a streaming protocol. You might get "of" and then "f" neither of which match "off".

You need to have a way of knowing when the input is finished (traditionally a linefeed or carriage-return) and then see what you got before that.

This is still fundamentally flawed.

Glad I'm not the only one that thinks so.

if(Serial.read == "off")

Well its a good thing that that is no where in my code...

If only that were true.

 if(incomeWord == "off") {
char incomeWord[1];

And it is true smart one.

if(incomeWord == "off")

Is in it but NOT:

if(Serial.read == "off")

They are completely different....

I'm not sure what point you're trying to make.

That if(Serial.read == "off") is no where in my code.

???

if(Serial.read == “off”)

Where did this come from?

I just searched back through the thread and the first time it appears is in a post by PoLoMoTo

PoLoMoTo:

if(Serial.read == "off")

Well its a good thing that that is no where in my code…

I don’t think anyone said it was in your code unless I missed something.

Are you now using zoomkat’s code or something that has incomeWord as a String, or is incomeWord a char array? It makes a difference because things like

if(incomeWord == “off”)

Won’t work unless you are using a String variable as he shows.

Can you post the current version of your code?


Rob

The slight delay in the code slows the retreving of bytes from the input buffer such that there is usually enough time for the next byte to arrive. Did you get "of" and then "f" neither of which match "off" when you tried the code? If so, increase the delay. The test code is based on the premis that the person is typing the commands in the serial monitor text box and sending the commands such that a delimiter is not needed. My test code is for use with the serial monitor so people can have something that usually works to start with, instead of "code snippits" and general jawbone support. PaulS always has a whine about my test code, but his apparent anal-retentive issues (potty trained on an electric chair perhaps) have prevented him from ever posting any actual working code (I haven't found any). The old saying "those that can, do. those that can't whine about those that can" is popular with some in the forum. I usually say if you have complete working code, post it. Other wise... :wink:

Well first someone said that if(Serial.read == "off") is fundamentally, therefore implying that it is in my code and second I don't understand why everyone is stuck on the "off" part my code never even gets past "digital"..... Also someone suggested using "strcmp" which I have yet to try since I don't have mt Arduino handy right now.

I think that somebody was you.

No actually it was in zoomkat's post of sample code which has nothing to do with my code.

No, I think zoomkat is far too experienced to compare a pointer to an object’s method to a constant C string.
You’re making this up.