So I just got my Arduino start pack in the mail this week. I went through all the tutorials, and have a basic understanding of programming/electronics, so went forth with my first project idea. Fairly simple - my thermostat only controls whether my heat is on or off (it's an old mercury thermostat, so the mercury actually connects two leads when the temperature is too low, thus switching on the heating). The idea is that I should be able to digitize this.
As far as the circuitry is concerned, I'm working at it as three different parts. There will be a control circuit (actually turning the heat on/off), a temperature reading circuit, and a user input circuit.
This is the actual problem:
Currently I'm just working on the user input. I've put together a simple circuit with two switches and two LEDs. I've also written a simple sketch, switch1 is pushed, led1 turns on for one second and the same for switch2/led2 respectively. In the final project the code shouldn't differ too much, except adding lines to increase and decrease a setTemp variable (and maybe outputting to a small LED screen? Let's get the buttons working first...). Below is the code:
Code: Select all
/* Simple code to test a two switch/two LED setup. When switch1Pin is HIGH, led1Pin should light up for one second. Same for switch2Pin and led2Pin respectively. */
int led1Pin = 8;
int led2Pin = 9;
int switch1Pin = 1;
int Switch2Pin = 2;
int switch1state;
int switch2state;
int val;
int val2;
int val3;
int val4;
void setup() {
Serial.begin(9600);
Serial.print("Setup...");
pinMode(led1Pin,OUTPUT);
pinMode(led2Pin,OUTPUT);
pinMode(switch1Pin,INPUT);
pinMode(switch2Pin,INPUT);
switch1state = digitalRead(switch1Pin);
switch2state = digitalRead(switch2Pin);
Serial.println("...complete.");
}
void loop() {
val = digitalRead(switch1Pin);
val3 = digitalRead(switch2Pin);
delay(10);
val2 = digitalRead(switch1Pin);
val4 = digitalRead(switch2Pin);
if (val == val2) {
if (val != switch1state) {
if (val == HIGH) {
digitalWrite(led1Pin,HIGH);
delay(1000);
digitalWrite(led1Pin,LOW);
Serial.println("Switch 1");
}
}
}
if (val3 == val4) {
if (val3 != switch2state) {
if (val3 == HIGH)
digitalWrite(led2Pin,HIGH);
delay(1000);
digitalWrite(led2Pin,LOW);
Serial.println("Switch 2");
}
}
switch1state = val;
switch2state = val3;
}
When I compile this code I get the fallowing error message:
Code: Select all
error: redefinition of 'int val' In function 'void setup()':
In function 'void loop()':