| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 10 - Zoetrope |
| 4 |
|
| 5 |
This sketch is written to accompany Project 10 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- two 10 kilohm resistors |
| 9 |
- two momentary pushbuttons |
| 10 |
- one 10 kilohm potentiometer |
| 11 |
- motor |
| 12 |
- 9V battery |
| 13 |
- H-Bridge |
| 14 |
|
| 15 |
created 13 Sep 2012 |
| 16 |
by Scott Fitzgerald |
| 17 |
Thanks to Federico Vanzati for improvements |
| 18 |
|
| 19 |
https://store.arduino.cc/genuino-starter-kit |
| 20 |
|
| 21 |
This example code is part of the public domain. |
| 22 |
*/ |
| 23 |
|
| 24 |
const int controlPin1 = 2; // connected to pin 7 on the H-bridge |
| 25 |
const int controlPin2 = 3; // connected to pin 2 on the H-bridge |
| 26 |
const int enablePin = 9; // connected to pin 1 on the H-bridge |
| 27 |
const int directionSwitchPin = 4; // connected to the switch for direction |
| 28 |
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off |
| 29 |
const int potPin = A0; // connected to the potentiometer's output |
| 30 |
|
| 31 |
// create some variables to hold values from your inputs |
| 32 |
int onOffSwitchState = 0; // current state of the on/off switch |
| 33 |
int previousOnOffSwitchState = 0; // previous position of the on/off switch |
| 34 |
int directionSwitchState = 0; // current state of the direction switch |
| 35 |
int previousDirectionSwitchState = 0; // previous state of the direction switch |
| 36 |
|
| 37 |
int motorEnabled = 0; // Turns the motor on/off |
| 38 |
int motorSpeed = 0; // speed of the motor |
| 39 |
int motorDirection = 1; // current direction of the motor |
| 40 |
|
| 41 |
void setup() { |
| 42 |
// initialize the inputs and outputs |
| 43 |
pinMode(directionSwitchPin, INPUT); |
| 44 |
pinMode(onOffSwitchStateSwitchPin, INPUT); |
| 45 |
pinMode(controlPin1, OUTPUT); |
| 46 |
pinMode(controlPin2, OUTPUT); |
| 47 |
pinMode(enablePin, OUTPUT); |
| 48 |
|
| 49 |
// pull the enable pin LOW to start |
| 50 |
digitalWrite(enablePin, LOW); |
| 51 |
} |
| 52 |
|
| 53 |
void loop() { |
| 54 |
// read the value of the on/off switch |
| 55 |
onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin); |
| 56 |
delay(1); |
| 57 |
|
| 58 |
// read the value of the direction switch |
| 59 |
directionSwitchState = digitalRead(directionSwitchPin); |
| 60 |
|
| 61 |
// read the value of the pot and divide by 4 to get a value that can be |
| 62 |
// used for PWM |
| 63 |
motorSpeed = analogRead(potPin) / 4; |
| 64 |
|
| 65 |
// if the on/off button changed state since the last loop() |
| 66 |
if (onOffSwitchState != previousOnOffSwitchState) { |
| 67 |
// change the value of motorEnabled if pressed |
| 68 |
if (onOffSwitchState == HIGH) { |
| 69 |
motorEnabled = !motorEnabled; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// if the direction button changed state since the last loop() |
| 74 |
if (directionSwitchState != previousDirectionSwitchState) { |
| 75 |
// change the value of motorDirection if pressed |
| 76 |
if (directionSwitchState == HIGH) { |
| 77 |
motorDirection = !motorDirection; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// change the direction the motor spins by talking to the control pins |
| 82 |
// on the H-Bridge |
| 83 |
if (motorDirection == 1) { |
| 84 |
digitalWrite(controlPin1, HIGH); |
| 85 |
digitalWrite(controlPin2, LOW); |
| 86 |
} else { |
| 87 |
digitalWrite(controlPin1, LOW); |
| 88 |
digitalWrite(controlPin2, HIGH); |
| 89 |
} |
| 90 |
|
| 91 |
// if the motor is supposed to be on |
| 92 |
if (motorEnabled == 1) { |
| 93 |
// PWM the enable pin to vary the speed |
| 94 |
analogWrite(enablePin, motorSpeed); |
| 95 |
} else { // if the motor is not supposed to be on |
| 96 |
//turn the motor off |
| 97 |
analogWrite(enablePin, 0); |
| 98 |
} |
| 99 |
// save the current on/off switch state as the previous |
| 100 |
previousDirectionSwitchState = directionSwitchState; |
| 101 |
// save the current switch state as the previous |
| 102 |
previousOnOffSwitchState = onOffSwitchState; |
| 103 |
} |