In the testing phase of developing a clap on clap off light switch with an attiny.
First step, test out some code.
[code]
const int buttonPin = 2;
const int ledPin = 0;
int buttonstate = 0;
int ledstate = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonstate = digitalRead(buttonPin);
ledstate = digitalRead(ledPin);
if (ledstate == HIGH && buttonstate == LOW) {
delay(250);
digitalWrite(ledPin, LOW);
}
if (ledstate == LOW && buttonstate == LOW) {
delay(250);
digitalWrite(ledPin, HIGH);
}
}
[/code]
The above code i have received for the arduino to receive a clap on pin 2, then change the ledstate on pin 0. I don't really understand how it works though, which is odd considering my strong knowledge of c++, c#, and objective-C. are HIGH and LOW boolean values like true and false? Won't this either just constantly loop because it is constantly changing the values? or will it just never loop because both states start at 0 which is neither HIGH or LOW. if it does work and somebody understands it, could you explain it to me, and then explain how i would get it to work with two claps, because I wan't to be able to clap twice and the lights go on. Also, if somebody is willing to work through this with me till the end, i have the circuit all planned out, i'm just very new to the arduino and the namespace, that would be very helpful.
THanks