#3 Advice required for amended #3 to combine heat sensor with #2 (no errors)

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

Just a couple of observations:

I personally am on a mobile broadband connection - every byte I download gets deducted from my monthly allowance, so I won't be downloading your images, they are far too large. Consider resizing them to ~ 200k? You can embed images in your post, I think there is an topic in 'Website and forum' which covers this.

As for your code, you can give the pins a meaningful name (as you have done with sensorPin):

const byte SYSTEM = 4;
const byte HEATING = 5;
const byte BOARD_STATUS = 6;
const byte RELAY = 10;

and then you can refer to them by name when reading / writing, which makes the code easier to read and there is less reliance on comments:

       digitalWrite(SYSTEM, HIGH);

Hi,

Thanks for the suggestions on both - I've attached low res and will read up on embed options (doesn't look like Drive my default works but will investigate).

Code that makes total sense - I wrote it in the comments at first but started to get confused so makes sense to name all elements, thanks :slight_smile:

If possible, let me know if there is anything you see you on the wiring front, so helpful to advice whilst beginning

Thanks
R