Monday, November 21, 2011

Button Control Assignment (CIRC-07): PART 2

PURPOSE:

The purpose is to turn on the LED(s) corresponding to the number of times the user pushes one button after he/she pushes the other button.

EQUIPMENT:
  • 2 x Pushbuttons
  • 2 x 10k Ohm Resistor
  • 4 x 330 Ohm Resistor
  • 4 x LED
  • 10 x Wire (any colour)
  • 1 x Arduino Uno
  • 1 x Arduino Holder
  • 1 x Arduino Breadboard
  • 1 x Reference Sheet
PROGRAM DETAILS:
            In PART 2, we are asked to keep the same circuit but make a program that will turn on the LED corresponding to the number of times you pushed one button after you push the other button.

In other words:
-Button A will keep track of the number of times it is pushed
-Each time button A is pushed, it will go on for 0.5 seconds to acknowledge it has counted once
-Button B will light the corresponding LED according to how many times Button A was pressed
-After the LED is on for 0.5 (delay time) seconds, the program resets and you can push Button A to equal which LED you want on
-If Button A is pressed more than 4 times, all the lights wil go on
-If Button A is not pressed when Button B is pressed, no lights will go on, instead the program will wait the delay time (not allowed to output to an unused pin...you must program for a delay)

Example:
-Button A is pressed twice: (Button A is pressed, LED #1 goes on for 0.5 seconds, Button A is pressed again, LED #1 goes on for 0.5 seconds)
-Button B is pressed and LED #2 goes on for 0.5 seconds
-Button A is pressed once
-Button B is pressed and LED #1 goes on for 0.5 seconds

TIME TO COMPLETE: 0 minutes to build the circuit, 25 minutes to code.

RESULTS:
             At first, the program wasn’t compiling and it was only because of a syntax error (a closing curly bracket was missing). After I fixed it, the program ran fine.  

PHOTOS OF PROJECT:
Button Control Assignment: PART 2 Circuit
TIPS:
            Take your time writing out the code. Do not rush it or else your Arduino won’t do what you want it to do. Do not hesitate to ask your peers or Mr. Kee when you need help. Also, test and run your codes few times to see if it’s accurate.

FURTHER WORK:
            My further work would be completing the PART 3 of this Button Control Assignment.

PROGRAM MODIFICATIONS:
           My program looks similar to the one from here (Click me). However, I used 4 LEDs and the corresponding number of 330 Ohm resistors to build my circuit for this assignment. Also, I followed the instructions from tiny.cc/ArduinoGuide (CIRC-07 Button Control Assignment PART 2). Thus, there are few changes in my program.

PROGRAM (with comments):

// CIRC-07 PART 2

int buttonA = 2; //Button A will keep track of the number of times it is pushed
int buttonB = 3; // Button B will light the corresponding LED according to how many times Button A was pressed
int ledPin[] =  {13,12,11,10};    
int counter = 0;

void setup()
{
  for(int i = 0; i <4; i++) // Accessing all LED pins to set their Pin mode
{
    pinMode(ledPin[i], OUTPUT);
  } 
    pinMode(buttonA, INPUT);
    pinMode(buttonB, INPUT); 
}

void loop()
{
   int delayTime = 500; // Waiting period (0.5 seconds)

  buttonA = digitalRead(2); // Takes in input
  buttonB = digitalRead(3);

while (!((buttonA==HIGH)&&(buttonB==HIGH)))
{
if ((buttonA==HIGH)&&buttonB==HIGH))
{
counter = counter + 1; // Counter increasing  by 1
digitalWrite(ledPin[0], HIGH);  // LED 1 lighting up
delay(delayTime);
digitalWrite(ledPin[0], LOW); // LED 1 turning off
delay(delayTime);
}

else if ((buttonB==HIGH)&&(buttonA==LOW))
{
if (counter >4) // Button A is pressed more than 4 times, all the lights wil go on
{
// All LEDs lighting up
digitalWrite(ledPin[0], HIGH); 
digitalWrite(ledPin[1], HIGH); 
digitalWrite(ledPin[2], HIGH); 
digitalWrite(ledPin[3], HIGH); 
}

else if (counter == 0) //Button A is not pressed when Button B is pressed, no lights will go on, instead  the program will wait the delay time.
{
delay(delayTime); // Waiting period
}
else
{
counter--;
digitalWrite(ledPin[counter],HIGH);
delay(delayTime);
digitalWrite(ledPin[counter],LOW);
delay(delayTime);
    }
  }
}

REFERENCE:





No comments:

Post a Comment