2

I just made a very basic circuit with a LED and a resistor. I also wrote a program that's supposed to take an input for how long to keep the LED on for. It works fine for shorter times like 10 seconds but as soon as I try to input a time like 120 seconds the LED never turns off.
My code's below. What could be wrong?

int light=9;
void setup() {
 Serial.begin(9600); 
 pinMode(light, OUTPUT); 
 }
void loop() {
 Serial.println("How long do you want the light on for (in seconds)?"); 
 while(Serial.available()==0){} 
 int on_time = (Serial.parseInt() * 1000); 
 digitalWrite(light, HIGH); 
 delay(on_time); 
 digitalWrite(light, LOW); 
 Serial.print("The light was just on for "); 
 Serial.print(on_time/1000);
 Serial.print(" seconds. ");
} 
asked May 30, 2019 at 20:12
4
  • please include all information .... your code prints to the serial console ... what do you see printed out? Commented May 30, 2019 at 20:23
  • 1
    What Arduino do you have? On an Uno, an int has values -32,768 to 32,767. An unsigned int is from 0 to 65,535 (ie. it's only 16 bits). Try using an unsigned long instead of int to see if that helps. Commented May 30, 2019 at 20:28
  • Ok, that makes a lot of sense. I'll try that. Thanks so much! And yeah i have an Uno btw Commented May 30, 2019 at 21:30
  • "the LED never turns off" is not exactly true. It will turn off, but after a very long delay. Commented May 31, 2019 at 7:38

1 Answer 1

2

If you add Serial.println(on_time); right after int on_time = (Serial.parseInt() * 1000); you will see the problem. Once you enter the number 33 for example, the integer value printed is -32536. The Allowed data type for delay() is unsigned long, so the negative number is causing a problem.

Using Serial.println() judiciously can be very helpful when you are trying to debug a sketch.

answered May 30, 2019 at 20:50
4
  • Hi! Thanks for taking the time to help! I've just done as you suggested an yeah i saw the negative number. 2 questions: 1) Why is this happening? Shouldn't it just be 33x1000? I'm a total noob so i don't understand what "unsigned long" is. 2) How can i go about fixing this/avoiding this problem in the future? Commented May 30, 2019 at 21:10
  • 1) On an UNO, the largest "number" you can store in a integer is 32767. 33 seconds times 1000ms = 33000, which is over the 32767 maximum, so the integer becomes a negative "number". Anunsigned long variable can hold a "number" up to 4,294,967,295. 2) To avoid this type of problem, arm yourself with knowledge. I found the examples that came with the IDE were very helpful to determine what variable types to use, and how to use them. Another source of information about variable types, functions, etc. is the Language Reference found here: arduino.cc/reference/en. Commented May 30, 2019 at 22:19
  • That all makes sense. Thanks again! Commented May 31, 2019 at 0:35
  • @ThisUsernameHasBeenTaken: Re "Why is this happening?": Since you are using a signed number, the most significant bit is interpreted as a sign bit. This is how two's complement arithmetic works. Commented Jun 1, 2019 at 17:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.