Saturday, January 7, 2012

Lab Expansion: CIRC-03

PURPOSE: To collect the knowledge of circuit 7 and utilize it to modify circuit 3.
EQUIPMENT:
·         2 x Pushbutton
·         1 x Diode
·         1 x Toy Motor
·         1 x Transistor
·         1 x 3 Pin Header
·         1 x Arduino Uno
·         1 x Arduino Holder
·         3 x 10k Ohm Resistor
·         1 x Breadboard
·         1 x Breadboard Sheet (CIRC-03)
·         1 x Breadboard Sheet (CIRC-07)
·         Wires (any coulor)

PROGRAM DETAILS:

In this lab expansion, the circuit assembling and programming was much more difficult than circuit-03 or circuit-07, as it was a combination of two circuits. In part 1, two pushbuttons were added and in part 2, four LEDs were added.  The pushbutton is a device controlled by the user. When pressed, the pushbutton completes a circuit, and it can complete up to two different circuits. In this lab expansion, the pushbuttons were used to control the speed of the motor. In part 1, one of the pushbuttons is used to accelerate the speed of the motor, and the other one is used to decelerate the speed of the motor. No new programming concepts were introduced in part 1 of this lab expansion except for one new data type, boolean.  A boolean is a data type that contains only two values, true and false. A boolean was used in the line of code, boolean val = buttonCheck();. The return type of this buttonCheck method gave a boolean value, true or false. This type of methods can be very useful when the expected outcome is only two options.

In part 2 of this lab expansion, LED’s are introduced. There are 4 LEDs that act as indicators for the speed of the motor. Each of those 4 LEDs represents one specific increment. For example, LED 1 represents 25% speed, LED 2 represents 50% speed, and etc. In terms of coding, a new method called ledCheck() was created which simply turns on a LED based on the speed of the motor.

In part 3 of this lab expansion, although the circuit was identical to the circuit in part 2, the programming required a lot of changes. The program was modified in a way so that the speed always goes down by 25% every 0.5 seconds. The input of this circuit is the number of times Button A gets pressed. Button A can be pressed from 0 to 4 times. Each press increases the motor’s speed by 25% percent. For example, if Button A is pressed once the motor’s speed increases to 25%, if Button A pressed twice the motor’s speed increased to 50%, and etc. Afterwards, the motor’s speed is decreased by 25% every 0.5 seconds until the speed reaches 0%. In terms of coding, counter was needed to keep track of the number of times Button A gets pressed and a variable called force was created, which increased in increments of 25% based on the number of press.                                         
                  
TIME TO COMPLETE:

Part 1 - 8 minutes to build and 60 minutes to program.
Part 2 - 10 minutes to build and 40 minutes to program.
Part 3 - 0 minutes to build and 20 minutes to program.
    
RESULTS:
          
           In part 1, there were some programming syntax errors such as missing semi-colons, extra brackets, and etc. I copied and pasted the program in Ready to Program Java which highlighted the lines of errors in my code. This made it easier for me to identify and correct the syntax errors. After I corrected the errors and uploaded the program, it ran as it was supposed to. In part 2, there were some lose wiring problems in the Arduino circuit. After I changed few of the wires, the circuit functioned properly. Fortunately, in part 3, there was no technical issue. Thus, the circuit assembled and functioned successfully.

PHOTOS OF PROJECT:

Part 1 ----->

Circuit 1-Part 1 with codes


Part 2 ----->

Part 2 Circuit
Part 3 ----->

Functional Circuit - Part 3

TIPS:
·         Wire up the circuit properly
·         Resistor’s and LED’s metal should not touch
·         Run your program few times and make it error-free

FURTHER WORK:

            To improve my lab expansion, I could do some motor and push button research to enhance my knowledge about these things. I could also watch online videos and read online blogs to compare and contrast my work, and determine what can be done to make my lab expansion better.

PROGRAM MODIFICATION:

Two different circuits were created for part 1-3. The first circuit was for part 1 and the second circuit was for part one and two. In the second circuit, four LEDs were added to the breadboard. Part 1, part 2, and part 3 have major differences in terms of coding as well.

PROGRAM (with comments):
PART 1:

//Declaring variables and assigning pin numbers
int motorPin = 9;
int buttonA = 2,
    int buttonB = 3;
int force = 0;

void setup ()
{
    // Sets motorPin mode to output
    pinMode (motorPin, OUTPUT);
}

// Runs more than once
void loop ()
{
    //Waiting period
    delay (20);

    // Goes to buttonCheck method
    boolean val = buttonCheck ();

    //If the value is true...
    if (val == true)
    {
        // While speed of motor is greater than 0...
        while (force > 0)
        {
            //Speed decreases until it is less than 0
            force--;
        }
    }

    // If the value is false...
    else if (val == false)
    {
        // While speed of moto) is less than 255...
        while (force < 255)
        {
            // Speed increases until it is greater than 255
            force++;
        }
    }

    //Sends the speed value to the Arduino Uno
    analogWrite (motorPin, force);
} // Loop ends


// Checks if Button A or Button B is pressed
boolean buttonCheck ()
{
    boolean a;

    if ((digitalRead (buttonA) == LOW) && (digitalRead (buttonB) == HIGH))
    {
        a = true;
    }

    else if ((digitalRead (buttonA) == HIGH) && (digitalRead (buttonB) == LOW))
    {
        a = false;
    }

    return a;  // Returns a value to be read by the variable
}

PART 2:

int motorPin = 9;
int buttonA = 2
    int buttonB = 3;
int ledPins[] = {10, 11, 12, 13};
int force = 0;

//Setting motorPin and ledPins mode to output
void setup ()
{
    pinMode (motorPin, OUTPUT);
    for (int i = 0 ; i < 4 ; i++)
    {
        pinMode (ledPins [i], OUTPUT);
    }
}
void loop ()
{
    delay (20);

    boolean val = buttonCheck ();

    if (val == true)
    {
        while (force > 0)
        {
            force--;
        }
    }

    else if (val == false)
    {
        while (force < 255)
        {
            force++;
        }
    }

    analogWrite (motorPin, force);

    // Goes to ledCheck Method
    ledCheck ();
}

// Checks which button is pressed
boolean buttonCheck ()
{
    boolean a;

    // If button A is pressed and button B isn't pressed then return a = true
    if ((digitalRead (buttonA) == LOW) && (digitalRead (buttonB) == HIGH))
    {
        a = true;
    }


    // If button A isn't pressed and button B is pressed then return a = false
    else if ((digitalRead (buttonA) == HIGH) && (digitalRead (buttonB) == LOW))
    {
        a = false;
    }

    return a;
}

//Checking to if LED 1, 2, 3 or 4 should light up
void ledCheck ()
{
    // Sets all LEDs off
    for (int i = 0 ; i < 4 ; i++)
    {
        digitalWrite (ledPins [i], LOW);
    }

    // Led connected to pin 10 turns on if motor is at 25% or less speed
    if (force <= 64)
    {
        digitalWrite (10, HIGH);
    }

    else if ((power > 64) && (power <= 128))
    {
        digitalWrite (11, HIGH);
    }

    else if ((power > 128) && (power <= 192))
    {
        digitalWrite (12, HIGH);
    }

    // Led connected to pin 13 turns on if motor is greater than 75% speed
    else
    {
        digitalWrite (13, HIGH);
    }
}

PART 3:

int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int ledPins[] = {10, 11, 12, 13};
int power = 0;
int counter = 0;


void setup ()
{
    pinMode (motorPin, OUTPUT);
    for (int i = 0 ; i < 4 ; i++)
    {
        pinMode (ledPins [i], OUTPUT);
    }
}

void loop ()
{
    delay (50);

    // If buttonA is pressed...
    if (digitalRead (buttonA) == LOW)
    {
        // Counter goes up by 1
        counter++;
    }

    // If buttonB is pressed...
    else if (digitalRead (buttonB) == LOW)
    {
        timesPushed ();
    }

    ledCheck ();
}

// Outputs speed to motor based on the counter value
void timesPushed ()
{
    // Power increased by 25%
    force = 64 * counter;

    // Speed stays between 0 and 255
    if (force > 255)
    {
        force = 255;
    }

    else if (force < 0)
    {
        force = 0;
    }

    delay (500);

    // Speed decreases by 25% every 0.5 seconds until it reaches 0%
    while (force >= 0 && force <= 255)
    {
        force -= 64;
        delay (500);
    }

    if (force > 255)
    {
        force = 255;
    }

    else if (force < 0)
    {
        force = 0;
    }

    analogWrite (motorPin, force);
    // Counter is reset to 0 to restart loop
    counter = 0;
} // timesPushed method ends

void ledCheck ()
{

    for (int i = 0 ; i < 4 ; i++)
    {
        digitalWrite (ledPins [i], LOW);
    }

    if (force <= 64)
    {
        digitalWrite (10, HIGH);
    }

    else if ((force > 64) && (force <= 128))
    {
        digitalWrite (11, HIGH);
    }

    else if ((force > 128) && (force <= 192))
    {
        digitalWrite (12, HIGH);
    }

    else
    {
        digitalWrite (13, HIGH);
    }
} // Method ledCheck ends

REFERENCES:

4) The Sparkfun Inventor's Guide