Hello again,
I apologize very much for my frequent and annoying questions, but I have not been successful with my many attempts and have become incredibly frustrated. So, I have resorted to asking yet another question.
Basically, I want to add a simple push button switch to power the Arduino and begin my sketch. I need to be able to hold down the button during the entire sketch. Once the sketch is finished, I will let go of the button and it will stop the sketch and power off the Arduino (I will know when the sketch ends).
I tried using a circuit to power up the Arduino and start the sketch without adding a code for the button and it worked, but it didn't start the sketch from the beginning.
I have tried everything to add the button into the circuit and the sketch, but nothing seemed to work right.
I'm sure if I was just using an LED, it wouldn't be so tough, but I'm using 2 motors. I don't know why I'm having such a hard time with this. All I want it to do is to begin the sketch when I hold it down and stop the sketch when I let go, and not start from a different part of the sketch when I press the button again.
Here is the code I'm using...without my attempts of adding a switch
// Use this code to test your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// ————————————————————————— Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin = 13; // LED connected to digital pin 13
// ————————————————————————— Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}
}
// ————————————————————————— Loop
void loop() {
drive_right_backward();
delay(200);
motor_stop();
Serial.println("1");
motor_stop();
delay(500);
motor_stop();
Serial.println("2");
drive_left_backward();
delay(200);
motor_stop();
Serial.println("3");
motor_stop();
delay(500);
motor_stop();
Serial.println("4");
drive_right_backward();
delay(200);
motor_stop();
Serial.println("5");
motor_stop();
delay(1500);
motor_stop();
Serial.println("6");
drive_right_forward();
delay(200);
motor_stop();
Serial.println("7");
motor_stop();
delay(500);
motor_stop();
Serial.println("8");
drive_left_forward();
delay(200);
motor_stop();
Serial.println("9");
motor_stop();
delay(500);
motor_stop();
Serial.println("10");
drive_right_forward();
delay(200);
motor_stop();
Serial.println("11");
motor_stop();
delay(500);
motor_stop();
Serial.println("12");
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
// ————————————————————————— Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void drive_left_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
}
void drive_left_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
}
void drive_right_forward(){
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_right_backward(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
Here is what I think the code partially looks like with the switch...I know its not much, but its all I'm sort of certain about.
// Use this code to test your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// ————————————————————————— Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin = 13; // LED connected to digital pin 13
int buttonPin = 4;
int buttonState = 0;
// ————————————————————————— Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
}
// ————————————————————————— Loop
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(motor_left[], HIGH);
digitalWrite(motor_right[], HIGH);
}
else {
digitalWrite(motor_left[], LOW);
digitalWrite(motor_right[], LOW);
}
drive_right_backward();
delay(200);
motor_stop();
Serial.println("1");
motor_stop();
delay(500);
motor_stop();
Serial.println("2");
drive_left_backward();
delay(200);
motor_stop();
Serial.println("3");
motor_stop();
delay(500);
motor_stop();
Serial.println("4");
drive_right_backward();
delay(200);
motor_stop();
Serial.println("5");
motor_stop();
delay(1500);
motor_stop();
Serial.println("6");
drive_right_forward();
delay(200);
motor_stop();
Serial.println("7");
motor_stop();
delay(500);
motor_stop();
Serial.println("8");
drive_left_forward();
delay(200);
motor_stop();
Serial.println("9");
motor_stop();
delay(500);
motor_stop();
Serial.println("10");
drive_right_forward();
delay(200);
motor_stop();
Serial.println("11");
motor_stop();
delay(500);
motor_stop();
Serial.println("12");
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
// ————————————————————————— Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void drive_left_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
}
void drive_left_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
}
void drive_right_forward(){
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_right_backward(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
I really need help I'm sorry to bother everyone.
I'm also not sure how to wire it anymore. I'm having major brain fart from all of the unsuccessful attempts I have had.
Thank you,
Matt