Arduino Uno, L298N Motor Shield, 2 Photo Resistors, Push Button, Two Small DC Motor (Out of a broken helicopter not sure of the voltage or draw, but pretty small).
const int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1
int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON
void setup()
{
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(button, INPUT); // button is an input
}
void loop()
{
val = digitalRead(button); // read input value and store it
//check if there was a transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}
old_val = val; // val is now old, store it
// check if button is pressed (HIGH)
// and chage the state
if (state == 1) {
rVal = analogRead(righteye); // Read value from right eye sensor
lVal = analogRead(lefteye); // Read value from left eye sensor
analogWrite(pwm_a, rVal/4); //set both motors to run at one quarter of light level
analogWrite(pwm_b, lVal/4);
} else {
analogWrite(pwm_a, 0); //set both motors to OFF
analogWrite(pwm_b, 0);
}
}
To get some feedback on the code, start a thread for the project. I'm still waiting for the wheels tracks and gearbox. First project with Arduino that's all.
Thanks Rob, I appreciate the input. The only qualm I was having with button debouncing was that I cant have a delay using this method of motor control.
const int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1
int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
Serial.begin(9600); // open the serial port
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(button, INPUT); // button is an input
}
void loop()
{
val = digitalRead(button); // read input value and store it
//check if there was a transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}
old_val = val; // val is now old, store it
// check if button is pressed (HIGH)
// and chage the state
if (state == 1) {
rVal = analogRead(righteye); // Read value from right eye sensor
lVal = analogRead(lefteye); // Read value from left eye sensor
analogWrite(pwm_a, rVal/4); //set both motors to run at one quarter of light level
analogWrite(pwm_b, lVal/4);
}
else {
analogWrite(pwm_a, 0); //set both motors to run at one quarter of light level
analogWrite(pwm_b, 0);
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Serial.println(rVal);
Serial.println(lVal);
}
}
I suppose I could use a similar routine as I use there for the serial output to have the button debouncing on a delay of 100. Would that work?