Hello everyone.
Ive been building on a quadropod for some time and i have got alot of help from this forum. But once again im stuck
This time its in the function part of the code.
Here is the function void, ive been using it in demo mode so i know it works.
void forward() {
servo11.write(50); // Only runs
servo12.write(50); // this part
delay(2000); // Doesnt get pass this
servo11.write(100); // Never gets here :(
servo12.write(100);
delay(2000);
servo11.write(30);
servo12.write(30);
}
But i dont think the problem lies there. See, im using multiple buttons on one analog pin, and i think its something wrong with the code handling that part. Or maybe the loop void. Im totaly stuck on this part.
But the thing is that the buttons work, so there is nothing wrong with the wiring or the “base” code handling them.
Its something that prevents the function void to “loop” and not get pass the delay.
And also, when i increase the delay, its just like im constantly pushing the button for that amount of time, even if im not. I want it to run the function void only when im pushing the button.
Here is the whole code. Im not getting any error.
#include <Servo.h>
Servo servo11; // Defines how many servos that are connected, also tells their names
Servo servo12;
Servo servo13;
Servo servo21;
Servo servo22;
Servo servo23;
Servo servo31;
Servo servo32;
Servo servo33;
Servo servo41;
Servo servo42;
Servo servo43;
const int buttonPin = 0; // the number of the pushbutton pin
const int BUTTON1 = 1; // Creates a virtual button on pin 0
const int BUTTON2 = 2;
const int BUTTON3 = 3;
const int BUTTON4 = 4;
const int BUTTON5 = 5;
const int BUTTON6 = 6;
const int BUTTON1LOW = 820; //Tells low cap for button listening
const int BUTTON1HIGH = 860; //Tells high cap for button listening
const int BUTTON2LOW = 970; //Tells low cap for button listening
const int BUTTON2HIGH = 982; //Tells high cap for button listening
const int BUTTON3LOW = 925; //Tells low cap for button listening
const int BUTTON3HIGH = 935; //Tells high cap for button listening
const int BUTTON4LOW = 690; //Tells low cap for button listening
const int BUTTON4HIGH = 701; //Tells high cap for button listening
const int BUTTON5HIGH = 1020; //Tells high cap for button listening
const int BUTTON5LOW = 1025; //Tells low cap for button listening
const int BUTTON6HIGH = 1000; //Tells high cap for button listening
const int BUTTON6LOW = 1005; //Tells low cap for button listening
void setup()
{
servo11.attach(13); // Tells what pin the different servos are connected to
servo12.attach(12);
servo13.attach(11);
servo21.attach(10);
servo22.attach(9);
servo23.attach(8);
servo31.attach(7);
servo32.attach(6);
servo33.attach(5);
servo41.attach(4);
servo42.attach(3);
servo43.attach(2);
pinMode(buttonPin, INPUT); //Sets the buttonpin(0) to input
}
void loop() {
// read the state of the switch into a local variable:
int reading = analogRead(buttonPin); //tells the buttonpin to read analog input
if(reading>BUTTON6LOW && reading<BUTTON6HIGH){//Read switch 6
rotRight(); //execute the rotRight function
}else if(reading>BUTTON5LOW && reading<BUTTON5HIGH){
//Read switch 5
rotLeft();
}else if(reading>BUTTON4LOW && reading<BUTTON4HIGH){
//Read switch 4
turnRight();
}else if(reading>BUTTON3LOW && reading<BUTTON3HIGH){
//Read switch 3
turnLeft();
}else if(reading>BUTTON2LOW && reading<BUTTON2HIGH){
//Read switch 2
reverse();
}else if(reading>BUTTON1LOW && reading<BUTTON1HIGH){
//Read switch 1
forward();
}else{
//No button is pressed;
wait();
}
}
void wakeup() {
servo11.write(90);
servo12.write(90);
servo13.write(90);
servo21.write(90);
servo22.write(90);
servo23.write(90);
servo31.write(90);
servo32.write(90);
servo33.write(90);
servo41.write(90);
servo42.write(90);
servo43.write(90);
}
void reverse() {
servo31.write(50);
servo32.write(50);
}
void forward() {
servo11.write(50); // Only runs
servo12.write(50); // this part
delay(2000); // Doesnt get pass this
servo11.write(100); // Never gets here :(
servo12.write(100);
delay(2000);
servo11.write(30);
servo12.write(30);
}
void turnRight() {
servo11.write(0);
servo12.write(80);
}
void turnLeft() {
servo11.write(180);
servo12.write(0);
delay(1000);
servo11.write(0);
servo12.write(180);
delay(5000);
}
void rotRight() {
servo11.write(180);
servo12.write(0);
delay(1000);
servo11.write(0);
servo12.write(180);
delay(5000);
}
void rotLeft() {
servo11.write(180);
servo12.write(0);
delay(1000);
servo11.write(0);
servo12.write(180);
delay(5000);
}
void stopRobot() {
servo11.write(80);
servo12.write(80);
}
void wait() {
servo11.write(90);
servo12.write(90);
servo13.write(90);
servo21.write(90);
servo22.write(90);
servo23.write(90);
servo31.write(90);
servo32.write(90);
servo33.write(90);
servo41.write(90);
servo42.write(90);
servo43.write(90);
}