To control the speed of a motor via two pushbuttons. Then adding extensions to the program such as a series of LED's to tell the speed of the motor, and deceleration over time.
Equipment:
- Breadboard x1
- Arduino x1
- Buttons x2
- Motor x 1
- 10k ohm resistor x3
- Diode x1
- Transistor x1
- Wire x10
References:
Program Details:
The building itself is not very complicated, as a picture is provided at [1]. Furthermore we have already finished a very similar circuit layout before [2]. The only exception being that there are now 6 more independent circuits, w of which are to get button input, and another 4 output to the LEDs.
In terms of programming, Part 1 was rather simple. What the program in Part 1 does is access a custom method called buttonCheck(); which does as its name implies. After checking which button is being press, and that two buttons are not being press at once, it goes through one of two if statements. Depending on which button is pressed, the program will either accelerate or decelerate the motor. The while loops are in place to insure the value being supplied to the motor isn't greater than 255 or less than 0, as those are the motors max and min values.
Part 2 is exactly like Part 1, except there is now another step added to the end called ledCheck(). This custom method checks the value for power (current speed) and lights the corresponding LED. Each LED is on its own circuit. The final part adds just one more line. This line reduces the power of the LED by 25% every half second, and is meant to simulate deceleration. It happens at the end of each loop().
Tips:
http://arduino-faiyaz.blogspot.com/2011/10/circ-03-spin-motor-spin.html. Same tips apply.
Further Work:
You could make it so double tapping a button causes either maximum speed or brings the motor to a halt.
Program:
//PART 1
int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
delay(100);
int value = buttonCheck();
if (value == 1)
{
while (power > 0)
{
power--;
}
}
else if (value == 2)
{
while (power < 255)
{
power++;
}
}
analogWrite(motorPin, power);
}
int buttonCheck()
{
int a;
if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
{
a = 1;
}
else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
{
a = 2;
}
return a;
}
//PART 2
int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop()
{
delay(100);
int value = buttonCheck();
if (value == 1)
{
while (power > 0)
{
power--;
}
}
else if (value == 2)
{
while (power < 255)
{
power++;
}
}
analogWrite(motorPin, power);
ledCheck();
}
int buttonCheck()
{
int a;
if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
{
a = 1;
}
else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
{
a = 2;
}
return a;
}
void ledCheck();
{
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
if (power <= 64)
{
digitalWrite(10, HIGH);
}
else if ((power > 64) && (power <= 128))
{
digitalWrite(11, HIGH);
}
else if ((power > 128) && (power <= 192))
{
digitalWrite(12, HIGH);
}
else
{
digitalWrite(13, HIGH);
}
}
//PART 3
int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop()
{
delay(100);
int value = buttonCheck();
if (value == 1)
{
while (power > 0)
{
power--;
}
}
else if (value == 2)
{
while (power < 255)
{
power++;
}
}
analogWrite(motorPin, power);
power -= 5; //Power decreases by 5(1/5 of 25 as delay = 1/5 0.5)
ledCheck();
}
int buttonCheck()
{
int a;
if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
{
a = 1;
}
else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
{
a = 2;
}
return a;
}
void ledCheck();
{
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
if (power <= 64)
{
digitalWrite(10, HIGH);
}
else if ((power > 64) && (power <= 128))
{
digitalWrite(11, HIGH);
}
else if ((power > 128) && (power <= 192))
{
digitalWrite(12, HIGH);
}
else
{
digitalWrite(13, HIGH);
}
}
No comments:
Post a Comment