Equipment:
- Arduino x 1
- Breadboard x 1
- Arduino Holder x 1
- CIRC-07 Breadboard Sheet x 1
- Pushbutton x 2
- 330 ohm resistor x 4
- 10k ohm resistor x 2
- LED x 4
- Wire x 10
References: A more advanced circuit than the one below
http://milindarduino.blogspot.com/2011/10/circuit-07-push-buttons.html
Program details:
The circuit setup is almost the same as circuit 7; there are 4 led's rather than just 1 led. The programming sector however is quite different. In Part 1 no new programming concepts were introduced.
In Part 2, a new concept is introduced; a method. A method contains a group of programming that has a function and may be used multiple times. Methods are created to make the programming neater and to save some space and time if it is used multiple times because a programmer won't have to type the same code again. A method does not activate as part of the code until it is "called." It is called by putting the name of the method then brackets and the parameters inside the brackets. In the code, timesPushed(); is used to call the method void timesPushed(). Since this method has no parameters, no parameters are used when calling it. But if it did have parameters, the same type of parameter must be used to call it. So for example, if the method was void timesPushed(int num), the code to call it would be timesPushed(int a); or timesPushed(7);.
Part 3 is almost the same as Part 2, however there was one new concept introduced, multiple conditions in if statements. Inside an if statement, multiple conditions can be given by adding "&&" or "||". "&&" means that both or all conditions must be met for the program to run what is inside the if statement. "||" means that any one of the conditions must be met for the program to run what is inside the if statement.
Time to complete: 10 mins to assemble
45 mins to program
Results:
The first time we ran it, only two of the led's were blinking. We replaced the two that weren't blinking but then they were all on when the buttons weren't even pressed.We changed up some parts of the code and then it worked. For part 2, our buttons weren't registering anything but that was because we had used HIGH instead of LOW. Once that was fixed the circuit ran perfectly. Part 3 was very easy as only two lines of code were changed so it worked perfectly the first time.
Videos:
Part 1
Part 2
Part 3
- When putting the resistors in for the push buttons, make sure to connect the resistors to the positive flow of electricity.
- Check your code for logic errors as that is the most common problem
- Try to make your code as concise as possible but make it still work
A lot of modifications were made from circuit 7. In part 1, led's light up according to which button is pressed. If the first button is pressed led's connected to pin 9 and 11 light up whereas if the second button is pressed, led's connected to pin 10 and 12 light up. In part 2 the first push button is used to control which led lights up. The second push button is used to activate that led. So if the first button was pressed once and then the second button was pressed led connected to pin 9 would light up, if the first button was pressed twice, then the led connected to pin 10 would light up etc.... If the first button was pressed more than 4 times, all the led's would light up. Part 3 is the same as part 2 except that both buttons control which led light up and then the second button is used to register that count.
Program(s):
(part 1)
int led [] = {9,10,11,12}; // set led pins
int buttonA = 2, buttonB = 3; // set pushButton pins
void setup()
{
for(int i = 0; i < 4; i++) // intializes all leds as outputs
{
pinMode(led[i], OUTPUT);
}
pinMode(buttonA, INPUT); // intializes buttons to inputs
pinMode(buttonB, INPUT);
}
void loop()
{
if(digitalRead(buttonA) == LOW) // if buttonA is pressed
{
digitalWrite(led[0], HIGH); // led at pins 9 and 11 will blink
digitalWrite(led[2], HIGH);
delay(500);
digitalWrite(led[0], LOW);
digitalWrite(led[2], LOW);
}
else if(digitalRead(buttonB) == LOW) // if buttonB is pressed
{
digitalWrite(led[1], HIGH); // led at pins 10 and 12 will blink
digitalWrite(led[3], HIGH);
delay(500);
digitalWrite(led[1], LOW);
digitalWrite(led[3], LOW);
}
}
(part 2)
int led [] = {9,10,11,12}; // set led pins
int buttonA = 2, buttonB = 3; // set pushButton pins
int counter = 0; // counter used to store button presses
void setup()
{
for(int i = 0; i < 4; i++) // initializes all leds as outputs
{
pinMode(led[i], OUTPUT);
}
pinMode(buttonA, INPUT); // intializes buttons to inputs
pinMode(buttonB, INPUT);
}
void loop()
{
if(digitalRead(buttonA) == LOW) // if buttonA is pressed...
{
counter++; // counter goes up by 1
digitalWrite(led[0], HIGH); // led at pin 9 blinks
delay(500);
digitalWrite(led[0], LOW);
}
else if(digitalRead(buttonB) == LOW) // if buttonB is pressed...
{
timesPushed(); // go to timesPushed method
}
}
void timesPushed()
{
if(counter == 1) // if the counter is at 1...
{
digitalWrite(led[0], HIGH); // led at pin 9 blinks
delay(500);
digitalWrite(led[0], LOW);
}
else if(counter == 2) // if the counter is at 2...
{
digitalWrite(led[1], HIGH); // led at pin 10 blinks
delay(500);
digitalWrite(led[1], LOW);
}
else if(counter == 3) // if the counter is at 3...
{
digitalWrite(led[2], HIGH); // led at pin 11 blinks
delay(500);
digitalWrite(led[2], LOW);
}
else if(counter == 4) // if the counter is at 4...
{
digitalWrite(led[3], HIGH); // led at pin 12 blinks
delay(500);
digitalWrite(led[3], LOW);
}
else if(counter > 4) // if the counter is above 4...
{
for(int i = 0; i < 4; i++)
{
digitalWrite(led[i], HIGH);// All four led's blink
}
delay(500);
for(int i = 3; i >= 0; i--)
{
digitalWrite(led[i], LOW);
}
}
else // if the counter is less than 1
{
delay(500); // there is a half-second delay
}
counter = 0; // counter is reset to 0 to restart loop
}
(part 3)
int led [] = {9,10,11,12}; // set led pins
int buttonA = 2, buttonB = 3; // set pushButton pins
int counter = 0; // counter used to store button presses
void setup()
{
for(int i = 0; i < 4; i++) // initializes all leds as outputs
{
pinMode(led[i], OUTPUT);
}
pinMode(buttonA, INPUT); // intializes buttons to inputs
pinMode(buttonB, INPUT);
}
void loop()
{
if(digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW)
// if buttonA and buttonB are pressed...
{
counter++; // counter goes up by 1
digitalWrite(led[0], HIGH); // led at pin 9 blinks
delay(500);
digitalWrite(led[0], LOW);
}
else if(digitalRead(buttonB) == LOW && digitalRead(buttonA) == HIGH) // if only buttonB is pressed...
{
timesPushed(); // go to timesPushed method
}
}
void timesPushed()
{
if(counter == 1) // if the counter is at 1...
{
digitalWrite(led[0], HIGH); // led at pin 9 blinks
delay(500);
digitalWrite(led[0], LOW);
}
else if(counter == 2) // if the counter is at 2...
{
digitalWrite(led[1], HIGH); // led at pin 10 blinks
delay(500);
digitalWrite(led[1], LOW);
}
else if(counter == 3) // if the counter is at 3...
{
digitalWrite(led[2], HIGH); // led at pin 11 blinks
delay(500);
digitalWrite(led[2], LOW);
}
else if(counter == 4) // if the counter is at 4...
{
digitalWrite(led[3], HIGH); // led at pin 12 blinks
delay(500);
digitalWrite(led[3], LOW);
}
else if(counter > 4) // if the counter is above 4...
{
for(int i = 0; i < 4; i++)
{
digitalWrite(led[i], HIGH);// All four led's blink
}
delay(500);
for(int i = 3; i >= 0; i--)
{
digitalWrite(led[i], LOW);
}
}
else // if the counter is less than 1
{
delay(500); // there is a half-second delay
}
counter = 0; // counter is reset to 0 to restart loop
}
No comments:
Post a Comment