I'm essentially trying to code my Arduino Uno to change the colour of my WS2812B Led strip according to the temperature reading on my LM35 temp sensor. I've got a code using Adafruit_Neopixel but the lights don't seem to be changing, instead just taking the top value from the code.
Maybe you could post that here - in code tags.
(Look up 'Code Tags' before posting.)
#include <Adafruit_NeoPixel.h>
float temp = 0 ;
float NtomV;
const float VREF = 1050;
#define PIN 8
#define NUMPIXELS 20
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 50
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pixels.begin();
analogReference(INTERNAL);
NtomV = VREF/1023;
}
void loop() {
// put your main code here, to run repeatedly:
int temp_val = analogRead(A1);
float mv = (temp_val/1024.0)*5000;
float cel = mv/10;
temp = cel;
if (temp = 37){
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
for(int i=0;i<20;i++)
pixels.setPixelColor(i, pixels.Color(0, 20, 0));// Green
pixels.show();
}
else if (temp >38){
pixels.clear(); //
for(int i=0;i<20;i++)
pixels.setPixelColor(i, pixels.Color(50, 0, 0));// Orange
pixels.show();
}
else if (temp <36){
pixels.clear();
for(int i=0;i<20;i++)
pixels.setPixelColor(i, pixels.Color(50, 0, 0));// Orange
pixels.show();
}
else if (temp <35){
pixels.clear();
for(int i=0;i<20;i++)
pixels.setPixelColor(i, pixels.Color(70, 0, 0));// Red
pixels.show();
}
else if (temp >39 ){
pixels.clear();
for(int i=0;i<20;i++)
pixels.setPixelColor(i, pixels.Color(70, 0, 0));// Red
pixels.show();
}
delay(5000);
}
I'm hoping I've posted this right...
if (temp = 37)
This is the cause of the problem. You are setting temp to 37 rather than comparing it with 37
You did well.
Establish ranges to yield the results.
if ((temp >= 34) &&(temp < 35))
if ((temp >= 35) && (temp < 36))
if ((temp >= 36) && (temp < 37))
How would I go about fixing this so that the command reads if the temp is equal to 37?
I've changed it to have the ranges but the light still just remains as one colour with the top line (if temp = 37) rather than changing according to the ranges. The temperature on the LM35 doesn't read 37 yet it's still lighting according to temp = 37
Change it to
if (temp == 37)
Thank you so much! Got it working now.
Use
if (temp == 37)
instead of
if (temp = 37)
Cheers, got it working now!
Gotta ask the "elephant" question.
Why are you using an LM35?
I'm actually looking to replace it with a non-contact temperature sensor. Are there any you'd recommend?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.