Tuesday, October 6, 2009

Putra | Script 01



int ledPin = 9; // pin 9 is an output for the LED
int potPin = 2; // pin 2 is an input for the potentiometer

int val; // variable for reading the pin status
int buttonState; // variable to hold the button state
int buttonPresses = 0; // variable for how many times the button has been pressed

void setup(){

pinMode(potPin, INPUT); // declare potentiometer Pin 2 as INPUT
pinMode(ledPin, OUTPUT); // declare LED Pin 9 as OUTPUT

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(potPin); // read the initial state
}

void loop(){
val = digitalRead(potPin); // read input value and store it in val

if (val != buttonState) { // if button state has changed!
if (val == LOW) { // check if the button is pressed
buttonPresses++; // add 1 to buttonPresses variable (shortcut for "buttonPresses = buttonPresses + 1)
Serial.print("Button has been pressed "); // when button is pressed this message will come up
Serial.print(buttonPresses);
Serial.println(" times"); // values for how many times button has been pressed
}

digitalWrite(ledPin, HIGH); // turns ledPin 9 on
delay(analogRead(potPin)); // pause
digitalWrite(ledPin, LOW); // turns ledPin 9 off
delay(analogRead(potPin)); // pause
}

buttonState = val; // save the new state in variable
}

No comments:

Post a Comment