Hello,
First time posting in the forum having got the starter kit for my birthday today, I hope I follow the rules and have posted in the appropriate place. I decided to use the third project as a fun game for my nieces over Christmas, amending "Love-O-Meter" to be "How excited are you for Christmas?"
Idea is they hold the sensor to see how excited they are for Christmas. As it was for 3-4 year olds, I wanted to build in a 'cheat' or failsafe button that I could subtly put on all LEDs if their hands were too cold and the room was too hot to easily set it up for success. Therefore I amalgamated project #2 and #3 to include a button switch and heat sensor. Hand goes on, sensors light up. If sensors don't like up, I press the front of the cardboard which lights all / remaining lights.
What I'm looking for today, is for someone to check the code and the board and let me know are there any improvements or is there anything I've done which is wrong / bad practice. It runs without error. I've got no experience in the board/wiring side, but bits and pieces on the coding side (comfortable with loop concepts, and the logic behind the language but used to JS, Apps Script/VBA).
int switchState = 0;
const int sensorPin = A0;
const float baselineTemp = 22.0;
void setup() {
pinMode(7, INPUT);
Serial.begin(9600); // open a serial port
for(int pinNumber = 2; pinNumber <6; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(2,OUTPUT);
}
}
void loop() {
//Print temperature
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
// convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts:");
Serial.print(voltage);
Serial.print(", degrees C: ");
//convert the voltage to temperature in degrees
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
//Setup switch
switchState = digitalRead(7);
//if button is on switch all lights on.
if (switchState == HIGH) {
digitalWrite(4, HIGH);
digitalWrite(3, HIGH); //Bottom light
digitalWrite(2, HIGH); //middle light
}
else if (switchState == LOW) {
digitalWrite(4, LOW);
digitalWrite(3, LOW); //Bottom light
digitalWrite(2, LOW); //middle light
if(temperature < baselineTemp) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if(temperature >= baselineTemp+1 &&
temperature < baselineTemp+2){
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
} else if(temperature >= baselineTemp+2 &&
temperature < baselineTemp+3){
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
} else if(temperature >= baselineTemp+3) {
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(2, HIGH);
}
//delay(1);
}
}
Grateful of any thoughts and second look throughs whilst amending easier project numbers.
Thanks
R