output not responding to input

I am stumped on where my simple program is failing. I am currently learning to use the arduino and its program. I have done a few simple things and am trying to slowly work my way up. The point of the program is to turn on one of 6 leds with a serial input. I have all the cathodes tied to ground and the anodes each have there own output pin. I decided to try and code the first led and debug it before adding the other 5. The problem is the led stays on no matter what with or without input. I have researched now for about 2 hours and have changed the code multiple times with no avail. Here is the code if someone can tell me what will inevitable be a simple solution.

int led1 =9;           //define pins
int led2 =8;
int led3 =7;
int led4 =6;
int led5 =5;
int led6 =4;
char output;

void setup(){
  
  pinMode(led1, OUTPUT);          //set led's to ouputs
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  
  Serial.begin(9600);            // being serial
}

void loop(){

  if (Serial.available() > 0)        /ensure input is present
  {
    
    output = Serial.read(); 
    if(isDigit(output))              //convert char to int
    {
        Serial.print("Turn on light: ");
        Serial.println(output);
  }
}
  if(output = 1)                    //turn light on if input is 1
    {
      digitalWrite(led1, HIGH);
      delay(2000);
      digitalWrite(led1, LOW);
}
  else
  {
    digitalWrite(led1, LOW);        //if input is not 1 turn off led
  }
}

if (output == 1)

LEDs with no resistors?

no they each have a 330 ohm resistor. Just didn't think it was pertinent to the question. adding the extra = fixed the problem of the light always being on but now nothing happens at all.

Are you expecting it to turn on as a result of receiving a "1"?

Yes I expect it to turn on for 2 seconds then turn off again.

do not understand what you want to do-
can you explain better?

if(isDigit(output)) //convert char to int
{
Serial.print("Turn on light: ");
Serial.println(output, DEC);
}

Adding the DEC to the Serial.println only accomplishes reverting it back to char which changes my serial monitor from saying "turn on light: 1" to "turn on light: 49".

I want to input a int from 1 to 6 in the serial monitor and it turn on the corresponding led for 2 seconds. I'm using this only to learn and nothing more. My code seems sound to me cause i'm imputing 1 and receiving 1 back. My only guess at this point is the program is still interpreting a char instead of an int. If i change if(output == 1) to if(output ==49) the led will turn on but won't turn back off until a int other than 1 is inputted.

if you want to learn, you must do it alone.
here my code

By Omni

String readString="";
int n=0;

int led1 =9;           //define pins
int led2 =8;
int led3 =7;
int led4 =6;
int led5 =5;
int led6 =4;
char output;

void setup(){
  
  pinMode(led1, OUTPUT);          //set led's to ouputs
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  
  Serial.begin(9600);            // being serial
}

void loop(){

    ReadPort();
				  if(n == 1)                    //turn light on if input is 1
					{
					  digitalWrite(led1, HIGH);
					  Serial.println("ON");

					  delay(2000);
					  digitalWrite(led1, LOW);
				   }
				  else
				  {
				   			

					digitalWrite(led1, LOW);        //if input is not 1 turn off led
				  }
				  
				  if(n == 2)                    //turn light on if input is 1
					{
					Serial.println("ON");

					  digitalWrite(led1, HIGH);
					  delay(2000);
					  digitalWrite(led1, LOW);
				   }
				  else
				  {

					digitalWrite(led1, LOW);        //if input is not 1 turn off led
				  }
}
void ReadPort(){
    int index=0;
	  while (Serial.available()) {
	  
	    delay(10);
	   if (Serial.available() >0 ) {
	       char c = Serial.read();  //gets one byte from serial buffer
	       readString += c;
               index++; //makes the string readString
             }
              
	  }

	if (index >0) {   
    	 
    	     char carray[readString.length()+1];
             readString.toCharArray(carray, sizeof(carray));
             n = atof(carray); ////  N, DATA SEND
            readString="";
			Serial.println(n);
        
	}
           
         
   
   
   
   }
if(isDigit(output))              //convert char to int

No, it does not.

PaulS:

if(isDigit(output))              //convert char to int

No, it does not.

I was thinking that was his issue. I'm not that familiar specifically with isDigit(), but knowing how functions work I wouldn't expect the argument (output in this case) to be touched. I would expect that it would return a boolean, and that is how it is being used. After checking isdigit - C++ Reference I see that I'm right (good educated guess on my part).

@jbruce4390
To explain a bit further on what should be done, the contents of output is likely to be an ASCII character. isDigit() checks to see if the binary value of the contents of output matches any of the digits in the ASCII table. If not, it returns a 0 (false), otherwise it returns a non-zero (true). So now all the code knows is that the value of the variable output matches on the ASCII table a decimal digit, but output has not changed. For what you are doing, you need to actually convert that value to another value that matches the digit in the ASCII table. There are several ways of doing this, but the simplest for a single decimal digit is to subtract the value that corresponds to zero on the ASCII table. Any of these three lines should be the first statement in your if:

output = output - 48;
output = output - 0x30;
output = output - '0';

Personally, I'd probably use the third because it is easier to understand the intent of the statement (subtract the ASCII value for digit zero), especially since I don't have the ASCII table memorized. Those single quotes are important, don't use double quotes. Also, that is a zero, not an upper-case letter o. Shorter syntax is available if you are comfortable with it:

output -= '0';

Your observation of

Adding the DEC to the Serial.println only accomplishes reverting it back to char which changes my serial monitor from saying "turn on light: 1" to "turn on light: 49".

is an indication of this. The value in the ASCII table for the decimal digit '1' is 49. Again, adding that DEC to the Serial.println() didn't actually change the contents of output, just merely showed you another interpretation of the value.

Thank you for all the help Runaway Pancake and Sembazuru. All of that makes sense now. I got the isDigit() function from some forum while I was trying to figure this all out. And believe it or not it was while looking for how to change the output value. I believed it was right cause the serial monitor was giving me what I want. I know this was a simple problem but figuring it out is nice to keep me motivated on learning this new topic (to me anyways).

Great Topic and Great answers Too...

Doc

jbruce4390:
Thank you for all the help Runaway Pancake and Sembazuru. All of that makes sense now. I got the isDigit() function from some forum while I was trying to figure this all out. And believe it or not it was while looking for how to change the output value. I believed it was right cause the serial monitor was giving me what I want. I know this was a simple problem but figuring it out is nice to keep me motivated on learning this new topic (to me anyways).

and I??
do not help you more :stuck_out_tongue:

@Naruto128
I do appreciate the attempted help, but just giving someone a completed code without telling them how you got there only perpetuates the problem. No one learns anything. I would just continually be on forums getting people to fix my newbie errors. When someone shows you what you did wrong and explains the reasoning you learn.

"Give a man a fish you feed him for a day, teach a man to fish you feed him for a lifetime."

jbruce4390:
@Naruto128
I do appreciate the attempted help, but just giving someone a completed code without telling them how you got there only perpetuates the problem. No one learns anything. I would just continually be on forums getting people to fix my newbie errors. When someone shows you what you did wrong and explains the reasoning you learn.

"Give a man a fish you feed him for a day, teach a man to fish you feed him for a lifetime."

walk to school, you will learn faster. :stuck_out_tongue: