Monday, November 21, 2011

Button Control Assignment (CIRC-07): PART 3

PURPOSE:
The purpose is to make the counter only go up if both Button A and Button B are pressed!

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 3, only modifying Part 2 was done in a way so that the counter only goes up if both Button A and Button B are pressed! No building or changing the Arduino circuit was necessary.

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

RESULTS:
Fortunately, there was no error and the program ran successfully!

PHOTOS OF PROJECT:
Assembled Button Control Circuit (PART 3)
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:
PART 3 of this Button Control Assignment ends here so there is no need for any further work. However, I would like to research more about Pushbuttons and other Arduino parts to enhance my Arduino knowledge.

PROGRAM MODIFICATIONS:
My program looks similar to the one from. 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 3). Thus, there are few changes in my program.

PROGRAM (codes): // Refer to Program codes in PART 2 for comments.

// CIRC-07 PART 3

int buttonA = 2;
int buttonB = 3;
int ledPin[] =  {13,12,11,10};    
int counter = 0;

void setup()
{
 for(int i = 0; i <4; i++)
{
    pinMode(ledPin[i], OUTPUT);
 } 
   pinMode(buttonA, INPUT);
   pinMode(buttonB, INPUT); 
}

void loop()
{
  int delayTime = 500;

  buttonA = digitalRead(2);
  buttonB = digitalRead(3);

while (!((buttonA==HIGH)&&(button==HIGH)))
{
   if (buttonA==HIGH)
{
   counter = counter + 1;
   digitalWrite(ledPin[0], HIGH); 
   delay(delayTime);
   digitalWrite(ledPin[0], LOW);
   delay(delayTime);
}
else if ((buttonB==HIGH)
{
if (counter >4)
   fourLED();
}

else if (counter == 0)
{
  delay(delayTime);
}
else
{
    counter--;
    digitalWrite(ledPin[counter],HIGH);
    delay(delayTime);
    digitalWrite(ledPin[counter],LOW);
    delay(delayTime);
 }
  }
}

void fourLED();
{
for(int i = 0; i <4; i++)
{
   digitalWrite(ledPin[counter],HIGH);
}
   delay(delayTime);

for(int i = 0; i <4; i++)
{
    digitalWrite(ledPin[counter],LOW);
}
   delay(delayTime);
}

REFERENCE:



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:





Button Control Assignment (CIRC-07): PART 1

PURPOSE: To make 4 LEDs light up based on the input from the pushbuttons.

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 this assignment, we are asked to make a circuit with 4 LEDs and 2 pushbuttons that does the following:
1. When one button is pressed, all even LED's will turn on for 0.5 seconds then go off.
2. When the other button is pressed, all odd LED's will turn on for 0.5 seconds then go  off.
          The main hardware component used is called the Pushbutton which is a device controlled by the user. When pressed, the pushbutton completes a circuit, and it can complete up to two different circuits. It looks like a small black square with four silver leads outat the bottom and a button on the top.

           One of the methods used to code the PART 1 of these assignments is pinMode(pin, INPUT) which declares pushbutton as input. Here, the Arduino takes in an input so we must use the digitalRead(pin) method, rather than digitalWirte(pin). The method digitalRead(pin) returns whether it is HIGH(pulled to +5volts) or LOW(pulled to ground).

           Putting this circuit together and wiring up the pushbuttons was fairly easy.  However, the component, pull up resistor can seem out of place at times. The pull up resistor is included in the circuit because the Arduino Uno doesn’t sense the same way humans do. Instead it requires checking the voltage on the pin and then it decides whether it is HIGH or LOW.

TIME TO COMPLETE: 10 minutes to build the circuit, 20 minutes to code.

RESULTS:
           In the first try, the LED was not turning on. The two pushbuttons are small squares and this is why it’s very easy to attach them to the breadboard in the wrong way. Since it was attached it in the wrong way, the LED didn’t blink so I gave it a 90 degree twist and then, it worked as it was supposed to.

PHOTOS OF PROJECT:
4 LEDs, 2 Pushbuttons Circuit (PART 1)
TIPS:
           The first tip I would give is when you press the pushbuttons, make sure you press them hard or else the circuit won’t be complete. Another tip is that you should be carefully choosing the Ohm Resistors. Since there are two different types of resistor (10k and 330) involved in this circuit, you might confuse yourself and choose the wrong one.

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

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

PROGRAM (with comments):

//CIRC-07 PART 1
int onButton = 2; // Initializes variables
int offButton = 3;
int ledPin[] =  {13,12,11,10}; // Keeps track of the LED pin number

void setup()
{
for(int i = 0; i <4; i++)
{
   pinMode(ledPin[i], OUTPUT); // Sets the Pin Mode of LED Pins to OUTPUT
}
   pinMode(onButton, INPUT); // Sets the Pin Mode of Button to INPUT
   pinMode(offButton, INPUT);
}

void loop() // Everything in the brackets will be looped
{
   int delayTime = 500; // Waiting period

if (digitalRead(onButton) == HIGH) // When Button 1 is pressed, the following will occur.
{
digitalWrite(ledPin[0], HIGH);
digitalWrite(ledPin[2], HIGH);
delay(delayTime);
digitalWrite(ledPin[0], LOW);
digitalWrite(ledPin[2], LOW);
}

else if (digitalRead(offButton) == HIGH); // When Button 2 is pressed, the following will occur.
{
digitalWrite(ledPin[1], HIGH);
digitalWrite(ledPin[3], HIGH);
delay(delayTime);
digitalWrite(ledPin[1], LOW);
digitalWrite(ledPin[3], LOW);
}

REFERENCE: