Last active
April 19, 2016 12:47
-
-
Save wietseneven/3ce749e47de3db5c8f92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Button | |
Turns on and off a light emitting diode(LED) connected to digital | |
pin 13, when pressing a pushbutton attached to pin 2. | |
The circuit: | |
* LED attached from pin 13 to ground | |
* pushbutton attached to pin 2 from +5V | |
* 10K resistor attached to pin 2 from ground | |
* Note: on most Arduinos there is already an LED on the board | |
attached to pin 13. | |
created 2005 | |
by DojoDave <http://www.0j0.org> | |
modified 30 Aug 2011 | |
by Tom Igoe | |
This example code is in the public domain. | |
http://www.arduino.cc/en/Tutorial/Button | |
*/ | |
// constants won't change. They're used here to | |
// set pin numbers: | |
const int button1Pin = 2; // the number of the pushbutton pin | |
const int button2Pin = 3; | |
const int led1Pin = 9; // the number of the LED pin | |
const int led2Pin = 10; | |
const int led3Pin = 11; | |
const int led4Pin = 12; | |
const int led5Pin = 13; | |
const int motorPin = 6; | |
// variables will change: | |
int button1State = 0; // variable for reading the pushbutton status | |
int button2State = 0; | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(led1Pin, OUTPUT); | |
pinMode(led2Pin, OUTPUT); | |
pinMode(led3Pin, OUTPUT); | |
pinMode(led4Pin, OUTPUT); | |
pinMode(led5Pin, OUTPUT); | |
// initialize the motor as an output: | |
pinMode(motorPin, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(button1Pin, INPUT); | |
pinMode(button2Pin, INPUT); | |
} | |
int i = 0; | |
void loop() { | |
// read the state of the pushbutton value: | |
button1State = digitalRead(button1Pin); | |
button2State = digitalRead(button2Pin); | |
// check if the pushbutton is pressed. | |
// if it is, the buttonState is HIGH: | |
if (button1State == HIGH) { | |
if (i < 4) { | |
i++; | |
} else { | |
i = 0; | |
} | |
delay(200); | |
} | |
if (button2State == HIGH) { | |
if (i == 0) { | |
i = 4; | |
} else { | |
i = i - 1; | |
} | |
delay(200); | |
} | |
analogWrite(motorPin, i * 63); | |
if (i == 0){ | |
digitalWrite(led1Pin, HIGH); | |
} else { | |
digitalWrite(led1Pin, LOW); | |
} | |
if (i == 1){ | |
digitalWrite(led2Pin, HIGH); | |
} else { | |
digitalWrite(led2Pin, LOW); | |
} | |
if (i == 2){ | |
digitalWrite(led3Pin, HIGH); | |
} else { | |
digitalWrite(led3Pin, LOW); | |
} | |
if (i == 3){ | |
digitalWrite(led4Pin, HIGH); | |
} else { | |
digitalWrite(led4Pin, LOW); | |
} | |
if (i == 4){ | |
digitalWrite(led5Pin, HIGH); | |
} else { | |
digitalWrite(led5Pin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment