Need help with code

Hello!

Im trying to build some kind of thermometer with arduino that is also going to work as a lamp. Im going to put diffrent colored leds behind a sheet of plexiglas and they will lit up the plexi with diffrent kind of colors depending on the temprature outside. Im going to have 12 leds connected to 12 pins at arduino.

But I have problem with the code and getting the leds to light up togheter. So that I can have that led 1,2,3 lights at one temprature and led 2,3,4 lights at another and 4,5,6,7,8 lights and so on. With the code below I only get the led blinking and not working at all at their designated temprature.

This is what I have writen so far but I think there are some more easier way to write this. The code below is far from ready and Im only using 2 leds so far even if I have declared 12 in the begining so dont be scared away. :slight_smile:

int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 5;
int ledPin6 = 6;
int ledPin7 = 7;
int ledPin8 = 8;
int ledPin9 = 9;
int ledPin10 = 10;
int ledPin11 = 11;
int ledPin12 = 13;

int lightPin = 0;
int val = 0;

int value1 = 150;
int value2 = 151;
int value3 = 152;
int value4 = 153;
int value5 = 160;
int value6 = 165;
int value7 = 156;
int value8 = 157;
int value9 = 158;
int value10 = 150;
int value11 = 150;
int value12 = 150;

void setup() {
pinMode(ledPin12, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
beginSerial(9600);
}

void loop() {

val = analogRead(lightPin)/4;

printInteger(val);
printByte(10);
printByte(13);

if(val<value1 ){

digitalWrite(ledPin12, HIGH);
}

else ( digitalWrite(ledPin12, LOW));

if(val>value4 ){

if(val<value5 ){

digitalWrite(ledPin4, HIGH);
}

else ( digitalWrite(ledPin4, LOW));
}

else ( digitalWrite(ledPin4, LOW));

if(val>value5 ){

if(val<value6 ){

digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin12, HIGH);
}

else ( digitalWrite(ledPin4, LOW));
}

else ( digitalWrite(ledPin4, LOW));

delay (200);

}

:slight_smile: :slight_smile:

Ok, I will try to answer (arduino masters, please correct me if I miss something, or go wrong):

First:
If you are going to use pins digital pins 0 and 1 you won't be able to communicate with the computer, since this seems to be a standalone instalation, it shouldn't matter. Nevertheless, disconnect pins 0 and when you upload the program, and connect them afterwards.

Second: I am not sure how you want to light up the LEDs, and what you are connecting to the ligthPin (and therefore what you are getting as analog input)so I will make a simple version of your thermometer:

Let's say that you have 4 LEDs connected to pins 3, 4 , 5 and 6. That you are getting analog values from the analog pin 0, which are between 100 and 200, which you will send to your computer through the serial port, and read from a terminal for debugging purposes (once your program works, this will not be necessary any more). Finally, for controlling the LEDS lets say that you are going to consider 3 temperature frontiers: 150, 160 and 170, so the first Led (connected to pin 3) will light up when your temperature is smaller that 150, the second LED 4when it is bigger thatn 150 but smaller thatn 160, and so on for the other leds and frontiers...

this is more or less how it should be done (check it cause I am writing directly on the forum, and I haven't tested this):

// * Variable Declaration *//

int ledArray [] = { 3,4,5,6 }; // Array where I declare the pins connected to the LEDs

int ledArrayLength = 4; // Number of LEDs used for the thermometer

int lightPin = 0; // Temperature entrance conected to ANALOG pin 0

int val = 0; // Variable which will stock the value red from the analog pin

//* Auxiliary functions*//

// Function that lights up the LED in the position ledPosition, and turns off the other ones
void ligthUpLed(int ledPosition)
{
int i;
for (i=0; i<ledArrayLength ; i++)
{
if (i = ledPosition)
{
digitalWrite(ledArray *, HIGH); *

  • }*
  • else*
  • {*
    _ digitalWrite(ledArray*, LOW);_
    _
    }_
    _
    }_
    _
    }_
    _
    // Initialization*_
    void setup()
    {
    // Declare the digital pins connected to the LEDs as outputs
    int i;
    for (i=0; i<ledArrayLength ; i++)
    * {*
    _ pinMode(ledArray*, OUTPUT);
    }
    // Necessary for debugging part*
    beginSerial(9600);
    }
    // Main Loop
    void loop()
    {
    // Read the temperature value
    val = analogReaD (lightPin);
    // Necessary for debugging part
    printInteger(val);
    printByte(10);
    printByte(13);
    // Light up the LED corresponding to the temperature
    if (val < 150)
    {
    * ligthUpLed(0); // Light up the first LED in the array (connected to analog pin 3)
    }
    else
    if (val < 160)
    {
    ligthUpLed(1);*_

}
else
if (val < 170)
{
* ligthUpLed(2);*
}
else
{
* ligthUpLed(3);*
}
delay (1000); // this will wait for 1 sec to make sure you see the LED light up, this way you will sample every second
}
LAST TIP:
If you want to learn how to play with LEDs, I suggest that you read the different tutorials using them on the page: bliking LED, night rider, shootingStar... so that you understand better how they work
I hope this helped!!
Cheers

hey

you could also use the logical operator AND &&
to check in the same if statement that the value is inside the desired range... like so:

//assuming 3 values 150, 160, 170)

if (val < 150) {
          ligthUpLed(0); // Light up the first LED in the array 

//this is the range check...this just says :
//if the value is between 150 and 160 (greater than 150 AND at the same time smaller than 160)
//turn on the first led
} else if ((val > 150) && (val<160)) {
          ligthUpLed(1);

//keep going on with all the ranges you want
} else if ((val > 160) && (val<170)) {
          ligthUpLed(2);
}
//etc etc. - repeat for all desired ranges

this sums up to a much tidier and clearer code.

just to know...what sensor are you using?

there is something that can interest you:
yesterday on makezine.com there was a link to a termometer project...might have some C code for you
or other reference
http://www.makezine.com/blog/archive/2006/02/how_to_measure_temperature_wit.html

hope this helps :slight_smile:
b.