Hello everyone, I am am having trouble writing a code to light up some leds as the temperature picked up by an lm35 goes up. I want an individual led to light up, which will correspond to that particular temperature. So far when I run the program, only one LED lights up, but it does not switch to the next one when the temperature changes. This is what I have so far,
int pins(int value);
int inPin=1;
void setup() {
int value=analogRead(inPin);
int ledpin= int pins(int value);
int inPin=1;
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
void loop() {
int ledpin;
int value=analogRead(inPin);
ledpin= pins(value);
Serial.println(value);
delay(1000);
digitalWrite( ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
}
int pins(int value)
{int i;
switch(value)
{
case 39:
i=12;
break;
case 40:
i=11;
break;
case 41:
i=10;
break;
case 42:
i=9;
break;
case 43:
i=8;
break;
case 44:
i=7;
break;
case 45:
i=6;
break;
case 46:
i=5;
break;
case 47:
i=4;
break;
default:
i=0;
}
return i;
}
I am new to Arduino and currently taking a c++ course. I just want to implement what I am learning in class so that I can learn better. Thanks in advance for the help.
please use the # button to get code tags for your code.
use proper indention for your code to make it more readable (Press CTRL-T in the IDE before save/copy/paste)
use meaningful variable names even in short code
A rewrite of your code to get started... (code not tested) give it a try and adjust it to learn from it
int inPin;
int ledpin;
void setup()
{
Serial.begin(9600);
Serial.println("start");
// set all pins to OUTPUT
for (int pin=4; pin < 13; pin++)
{
pinMode(pin,OUTPUT);
}
}
void loop()
{
// MAKE MEASUREMENT
int value = analogRead(inPin); // returns 0..1023
// CALCULATE LEDPIN FROM VALUE
ledpin = map(value, 0, 1023, 4, 12); // replace 0 and 1023 with minimum and maximum values within your experiment
ledpin = constrain(ledpin, 4, 12); // force it between 4 and 12
// GIVE FEEDBACK
Serial.print(value);
Serial.print(" => ");
Serial.println(value);
// LIGHT THE LED
digitalWrite( ledpin,HIGH);
delay(100);
digitalWrite(ledpin,LOW);
}
It is not necessary to define function prototypes. The IDE will do this for you, unless the function uses reference arguments. If you are going to define them anyway, there should be comments before the prototypes to remind people what these strange things are.
int value=analogRead(inPin);
int ledpin= int pins(int value);
int inPin=1;
These local variables go out of scope before being referenced. Why are they here?
Do you have a clue what that second one is assigning as a value to ledPin?
ledpin= pins(value);
Since the output of analogRead() is NOT the temperature, it is unlikely that the value corresponds to any case in the switch statement in pins(), so the default case will be executed, which assigns 0 to ledPin.