My bad, here is my code:
#include <Servo.h>
// [Creates two Servo Objects]
// "servoTop" is for the upper motor and "servoBot" is for the lower motor.
Servo servoTop;
Servo servoBot;
// [Define Variable]
// Arduino Pin locations for the buttons that operates Upper Servo, Lower Servo, and Power.
int switchPinTop = 2;
int switchPinBot = 1;
int switchPinPow = 3;
int stepsSet = 0; // As of 040419, I'm not sure what this does. However, it's used in places, so please don't touch it.
// Arduino Pin locations for the LEDs.
int led1 = 7;
int led2 = 8;
int led3 = 9;
int led4 = 10;
int led5 = 11;
int led6 = 12;
// Power button timer
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 15000; // "period" is the amount of time (in miliseconds) we want the device to "be on" after the start button is pressed.
// Boolean varialbes to keep track of button and motor
boolean currentTop = LOW;
boolean currentBot = LOW;
boolean currentPow = LOW;
boolean last = LOW; // "current" and "last" track the state for the push button. ie: the state before "last" is LOW, the current state is HIGH.
boolean isRunning = false; // isRunning Checks the state of the motor. For off, it will be marked as false. For on, is will be marked as true
boolean lastGlobal = LOW;
boolean isRunningGlobal = false;
void setup() {
servoTop.attach(4); // Sets Top Servo to pin 1
servoBot.attach(5);
pinMode(switchPinTop, INPUT); // Push button is an input, so we define it with Pinmode
pinMode(switchPinBot, INPUT);
pinMode(switchPinPow, INPUT);
servoTop.write(0); // Set original locations for servos.
servoBot.write(0);
// Tell lightbulbs that they are in fact light bulbs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
// Keeps the button from being pressed under 30 seconds.
startMillis = 9999999999;
}
// [DEBOUNCE CODE FOR TOP BUTTON]
// Corrects push button bounce (not the best debounce function)
boolean debounceBot(boolean inLast){
boolean inCurrentBot = digitalRead(switchPinBot);
if(inLast != currentBot){
delay(5);
inCurrentBot = digitalRead(switchPinBot);
}
return inCurrentBot;
// The purpose here is to limit the instability when the button is shaking. So, we wait 5 micro seconds for the current to stableize, which enables a clean reading.
}
// [DEBOUNCE CODE FOR BOTTOM BUTTON]
// Corrects push button bounce (not the best debounce function)
boolean debounceTop(boolean inLast){
boolean inCurrentTop = digitalRead(switchPinTop);
if(inLast != currentTop){
delay(5);
inCurrentTop = digitalRead(switchPinTop);
}
return inCurrentTop;
}
// [DEBOUNCE CODE FOR POWER BUTTON]
// Corrects push button bounce (not the best debounce function)
boolean debouncePow(boolean inLast){
boolean inCurrentPow = digitalRead(switchPinPow);
if(inLast != currentPow){
delay(5);
inCurrentPow = digitalRead(switchPinPow);
}
return inCurrentPow;
// The purpose here is to limit the instability when the button is shaking. So, we wait 5 micro seconds for the current to stableize, which enables a clean reading.
}
// [GARAGEACTION CODE FOR TOP BUTTON]
void garageActionTop(float factor){ // Function that moves motor
stepsSet = factor; // The amount we want to motor to rotate
// [Servo speed here?];
servoTop.write(factor); // Activeates the motor. Tells the motor to move factor amount
if (isRunning == false){ // If the motor is moving and it was off before, wait three seconds
delay(3000);
} else if (isRunning == true) {
delay(5);
}
}
// [GARAGEACTION CODE FOR BOTTOM BUTTON]
void garageActionBot(float factor){ // Function that moves motor
stepsSet = factor; // The amount we want to motor to rotate
// [Servo speed here?]; // Activeates the motor. Tells the motor to move factor amount
servoBot.write(factor);
if (isRunning == false){ // If the motor is moving and it was off before, wait three seconds
delay(3000);
} else if (isRunning == true) {
delay(5);
}
}
// [GLOBAL CODE FOR POWER BUTTON]
void globalFunction(float factor){
if (isRunningGlobal == false){
// Stuff
} else if (isRunningGlobal == true) {
// stuff
}
}
// die Gehirne
void loop() {
currentTop = debounceTop(last); // Calls the debounce function on the last button: Checks low and the current state. If it's the same, it does nothing. if it's different, it will recheck to get a correct reading
currentBot = debounceBot(last);
currentPow = debouncePow(last);
// [CHECK BUTTON STATE - CODE FOR POWER BUTTON]
if(currentPow == HIGH && lastGlobal == LOW && isRunningGlobal == false){ // If the current state is high, the previous was low, and the motor is not moving, then rotate 180 degrees (clockwise)
// Turn the LEDs on
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
// Store the time when button was pressed
currentMillis = millis();
startMillis = millis();
// ENG MILLIS TRIAL
isRunningGlobal = !isRunningGlobal; // Change the current state to high.
}
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if ((currentMillis - startMillis) <= period){ //test whether the period has elapsed
// [CHECK BUTTON STATE - CODE FOR TOP BUTTON]
if(currentTop == HIGH && last == LOW && isRunning == false){ // If the current state is high, the previous was low, and the motor is not moving, then rotate 180 degrees (clockwise)
garageActionTop(180); //Location 93 Functions as zero.
isRunning = !isRunning; // Change the current state to high.
}
if(currentTop == HIGH && last == LOW && isRunning == true){ // if the current state is high, the previous state was low, and the motor is moving, rotate CCW 180 degrees.
garageActionTop(0);
delay(1900);
isRunning = !isRunning; // Then switches is back.
} // [END IF CODE FOR TOP BUTTON]
// [CHECK BUTTON STATE - CODE FOR BOTTOM BUTTON]
else if(currentBot == HIGH && last == LOW && isRunning == false){
garageActionBot(180);
isRunning = !isRunning; // Change the current state to high.
}
if(currentBot == HIGH && last == LOW && isRunning == true){
garageActionBot(0);
delay(1900);
isRunning = !isRunning;
} // [END IF CODE FOR BOT BUTTON]
// Or else time out
else if((currentMillis - startMillis) >= period){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
isRunningGlobal = !isRunningGlobal;
currentMillis = 99999999;
}
} // End milli if
} // End void loop()