This exercise is based on PDF page 30 (digital input)
This original exercise is making up circuit and program that turn on Green LED when the switch is pressed. I added a circuit and modified the program so that Red LED blink while the witch is not pressed.
Requirement
Arduino main board
Two different colored LED.
Resisters 10kΩ and around 1kΩ
Push Switch
Wires
Program
/*
This circuit use "if else" instead of "if" in oder to light a Red LED while switch is off.
*/
int ledPin = 13; // output pin for the LED
int inPin = 2; // input pin (for a switch)
int ledPin2 = 7;
int val;
void setup()
{
Serial.begin(9600);
Serial.println("program is started");
pinMode(ledPin, OUTPUT); //set up 13 pin
pinMode(inPin, INPUT); // set up 2 pin
pinMode(inPin, OUTPUT); // set up 7 pin
}
void loop()
{
val = digitalRead(inPin);
if ( val == 1) // check the button
{
digitalWrite(ledPin, HIGH); // turns the LED on
delay(500); // 0.5 second duration
digitalWrite(ledPin, LOW); // turns the LED off
delay(500); // 0.5 second duration
}
else
{
digitalWrite(ledPin2, HIGH); // turns the LED on
delay(500); // 0.5 second duration
digitalWrite(ledPin2, LOW); // turns the LED off
delay(500); // 0.5 second duration
}
}
Hardware setup
The key of the structure is to put a resistor between a switch and ground to work everything properly.
0 件のコメント:
コメントを投稿