Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: 11Trevor11 on Jul 16, 2014, 03:57 pm

Title: Not sure if i understand the code right
Post by: 11Trevor11 on Jul 16, 2014, 03:57 pm
Really new to Arduino and did this project if found making a temp. sensor that light up one of three LEDs according to the TEMP. In the code it seems as if they should blink but they don't and this is not the first project with the same section of code and they don't blink, maybe I just don't understand what it should be doing. everything else works perfectly. I attached the sketch if that helps.

Title: Re: Not sure if i understand the code right
Post by: Peter_n on Jul 16, 2014, 04:19 pm
When I see that code, I would like to change it immediately  ;)

The temperature is measured and according to the temperature, a led is turned on, a second waiting, and the led is turned off.
So the led is actually turned off.
After that the temperate is read again.

Reading the temperature and calculating the degrees in Celsius is so fast, you won't see that the led is turned off.
If the temperature is the same, the same led is turned on again. I might seem that the led just stays on. You can test it with the high speed camera or oscilloscope that the led turnes off for a few milliseconds.

The result of the sketch is that the temperature is tested only once a second.

Next step: Can you write the temperature to the serial monitor ? So that you can see the real temperature on the computer.
http://arduino.cc/en/Serial/Println
Title: Re: Not sure if i understand the code right
Post by: steinie44 on Jul 16, 2014, 04:31 pm
Code: [Select]
void loop()
{
  // read the temperature sensor and conver the result to degrees celsius
  sensor = analogRead(0);

Add a delay 1st thing and the LEDs will blink
Code: [Select]
void loop()
{
  delay(1000);
  // read the temperature sensor and conver the result to degrees celsius
  sensor = analogRead(0);


This is just a Workaround to blink, the sketch can be improved quite a bit.