[QUESTION] arduino code.

spazmoid

Member
this is my first attempt at coding in arduino and before i have an arduino to test it would anybod be able to tell me if this would work?
an led with 220ohm resistor on pin 13 and an LM35 temperature sensor on analog pin 0. if themp falls below 40 deg then the led should turn off.

int led = 13;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue, DEC)
if sensorValue > 40 {
digitalWrite(led, HIGH);
}
if sensorValue < 40 {
digitalWrite(led, LOW );
}
}

thanks in advance :mrgreen:
 
Okay, so with analog reads you get a value from 0 to 1023 as your input. What you need to do is figure out how that corresponds to millivolts, which is how your sensor tells you what the temp is; in the datasheet here: http://www.ti.com/lit/ds/symlink/lm35.pdf it says that for every 10mV, it represents 1 degree C.

So luckily Arduino themselves made some notes here to make that translation easier: http://playground.arduino.cc/Main/LM35HigherResolution . All you need here is the equation for low precision results. I would play around with that, get a feel for the code and then try the higher resolution method below the equation.
 
i have made an updated code which compiled.
can you tell me if it would work or if there are any glaring issues.
Code:
#include <EEPROM.h>
int but = 2;
int led = 13;
int addr = 14;
int butState = 0;
int lastbutState = 0;
void setup() {
Serial.begin(9600);
pinMode(but, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int butState = digitalRead(but);
int val = analogRead(A0);
int midpoint =  EEPROM.read(14);
Serial.println(val, DEC);
if (val > midpoint) {
digitalWrite(led, HIGH);
}
if (val < midpoint) {
digitalWrite(led, LOW );
}
if (butState !=lastbutState) {
if (butState == HIGH) {
EEPROM.write(addr, val);
}
}
}
 
Back
Top