Equipment:
- Arduino x 1
- Breadboard x 1
- Arduino Holder x 1
- CIRC -03 Breadboard Sheet x 1
- CIRC -07 Breadboard Sheet x 1
- Pushbutton x 2
- Toy Motor x 1
- 10k ohm resistor x 3
- Diode x 1
- Transistor x 1
- 3 pin header x 1
- Wire x 10
References:
http://milindarduino.blogspot.com/2011/10/circuit-03-spin-motor-spin.html
http://milindarduino.blogspot.com/2011/10/circuit-07-push-buttons.html
Program Details:
The circuit is setup very differently than circuit 3. Two pushbuttons are added for part 1 and four led's for part 2. The pushbuttons are used to control the speed of the motor. In part 1, one button is used to increase the speed of the motor and the other button is used to decrease the speed of the motor. No real new programming concepts are introduced in this program except for a new data type, boolean. A boolean has only two values, true and false. A boolean is used in this line of code boolean value = buttonCheck();. So this means that the return type of the buttonCheck method must also be a boolean for this to work and that the value is either true or false. This is best used for when there are only two options as utilized later in the program.
Part 2 is very similar to Part 1 except for led's are introduced into the circuit to act as indicators for the speed of the motor. Each led represents an increment of 25% of the motor's speed. In terms of the program, a new method called void ledCheck() is created to turn the led's on and off which is accessed by the line ledCheck();. This method simply turns on an led based on the speed of the motor, so if the motor is between 25% - 50% led 2 will turn on, if the motor is between 50-75% led 3 will turn on, etc.
Part 3 is quite different than part 1 and part 2 in terms of programming; the circuit looks the exact same though. In part 3 button A is pressed a number of times (up to 4) and each press increases the motor's speed to 25% times the number of presses. After that, the speed is decreased by 25% every 0.5 seconds until it reaches 0%. In terms of programming, first the counter is calculated, then when button B is pressed the method timesPushed is executed. In timesPushed, the variable power is given a value, then confirmed to be withing the range and then decreased in increments of 64 (25%) every 0.5 seconds until it reaches 0. Then the counter is reset after which the process is repeated.
Time to complete:
10 mins to assemble
50 mins to program
Results:
The first time we ran it, there were many problems but after we fixed a few wires and some parts of the program everything ran perfectly fine.
Photo:
Tips:
- Make sure your program logic works
- Make sure the push buttons, the transistor and the diode are setup the right way
- Make sure the motor is properly connected with the breadboard
Program Modifications:
There were major modifications from circuit 3. Two push buttons were added, four more LEDS were used, and the programs are very different as can be seen below.
Program(s):
(Part 1)
//Sets buttons to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int power = 0;
void setup()
{
pinMode(motorPin, OUTPUT); // assigns motorPin as an output
}
void loop()
{
delay(20);
boolean value = buttonCheck(); // Goes to buttonCheck method
if (value == true) // if value is true then...
{
while (power > 0) // when power(speed of motor) is greater than 0...
{
power--; // decrease the speed until it is less than 0
}
}
else if (value == false) // if value is false then...
{
while (power < 255) // when power(speed of motor) is less than 255..
{
power++; // increase the speed until it is greater than 255
}
}
analogWrite(motorPin, power); // sends the speed to the arduino
}// end loop
// 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; // the entire method returns a value to be read by the variable
}// end buttonCheck
(Part 2)
//Sets variables to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int ledPins[] = {10,11,12,13};
int power = 0;
void setup()
{
pinMode(motorPin, OUTPUT); // assigns motorPin as an output
for(int i = 0; i < 4; i++)
{
pinMode(ledPins[i], OUTPUT); // sets leds on pins 10 - 13 as outputs
}
}
void loop()
{
delay(20);
boolean value = buttonCheck(); // Reads the value returned by buttonCheck method
if (value == true) // if value is true then...
{
while (power > 0) // when power(speed of motor) is greater than 0...
{
power--; // decrease the speed until it is less than 0
}
}
else if (value == false) // if value is false then...
{
while (power < 255) // when power(speed of motor) is less than 255..
{
power++; // increase the speed until it is greater than 255
}
}
analogWrite(motorPin, power); // sends the speed to the arduino
ledCheck(); // Goes to ledCheck Method
}// end loop
// 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; // the entire method returns a value to be read by the variable
}// end buttonCheck
//Checks if an led should light up
void ledCheck()
{
// sets all led's 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 (power <= 64)
{
digitalWrite(10, HIGH);
}
// led connected to pin 11 turns on if motor is between 25% and 50% speed
else if ((power > 64) && (power <= 128))
{
digitalWrite(11, HIGH);
}
// led connected to pin 12 turns on if motor is between 50% and 75% speed
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);
}
}// end ledCheck
(Part 3)
//Sets buttons to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int ledPins[] = {10,11,12,13};
int power = 0, counter = 0;
void setup()
{
pinMode(motorPin, OUTPUT); // assigns motorPin as an output
for(int i = 0; i < 4; i++)
{
pinMode(ledPins[i], OUTPUT); // sets leds on pins 10 - 13 as outputs
}
}
void loop()
{
delay(50);
if(digitalRead(buttonA) == LOW) // if buttonA is pressed...
{
counter++; // counter goes up by 1
}
else if(digitalRead(buttonB) == LOW) // if buttonB is pressed...
{
timesPushed(); // go to timesPushed method
}
ledCheck(); // Goes to ledCheck Method
}// end loop
// Method used to output speed to motor depending on the counter
void timesPushed()
{
power = 64 * counter; // Power increased in increments of 25%
// Makes sure the speed stays between 0 and 255
if(power > 255)
{
power = 255;
}
else if(power < 0)
{
power = 0;
}
delay(500);
// Power/Speed goes down by 25% every 0.5 seconds until it reaches 0
while(power >= 0 && power <= 255)
{
power -= 64;
delay(500);
}
//Makes sure the speed stays between 0 and 255
if(power > 255)
{
power = 255;
}
else if(power < 0)
{
power = 0;
}
analogWrite(motorPin, power); // sends the speed to the arduino
counter = 0; // counter is reset to 0 to restart loop
}// end timesPushed
//Checks which leds should light up
void ledCheck()
{
// sets all led's 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 (power <= 64)
{
digitalWrite(10, HIGH);
}
// led connected to pin 11 turns on if motor is between 25% and 50% speed
else if ((power > 64) && (power <= 128))
{
digitalWrite(11, HIGH);
}
// led connected to pin 12 turns on if motor is between 50% and 75% speed
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);
}
}// end ledCheck