Got this code that is done on the Arduino application. Everything is working except that the motor turns on when the buttons is switched to on. However, when I try to turn off the switch, the motor keeps on running. However, when I ignite the second switch, the motor increases speed. Same goes for the third and final switch. Unfortunately, none of the switches turn the motor back off, this needs to happen! Need code to be fixed before Thursday!
Code:
#include <Servo.h>
//User configuration:
int percent = 0; //between -100 and 100, indicates how fast the motor
//will be moving when the arduino boots
int pins[] = {5}; //the signal output pins (as many as you'd like)
const int button1 = 6; //button for 35 percent
const int button2 = 7; //button for 65 percent
//const int button3 = 8; //button for 85 percent
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
bool onFlag1 = false;
bool onFlag2 = false;
//bool onFlag3 = false;
const int arraySize = sizeof(pins)/sizeof(int);
Servo controllers[arraySize];
void setup() {
//Serial.begin(9600);
//Serial.println("you called, master?\n");
for (int i=0; i<arraySize; i++)
controllers[i].attach(pins[i]); //associate the object to a pin
delay(1000);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(pins, OUTPUT);
//Serial.println("type in a percent, and I will output your PWM.\n");
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
/* button is high then toggle flag*/
if (buttonState1 == HIGH) {
onFlag1 = !onFlag1;
delay(250);
}
/*if flag high, led on, else off*/
if (onFlag1 == true){
digitalWrite(pins, HIGH);
percent = 40;
}
else {
digitalWrite(pins, LOW);
}
if (buttonState2 == HIGH) {
onFlag2 = !onFlag2;
delay(250);
}
if (onFlag2 == true){
digitalWrite(pins, HIGH);
percent = 60;
}
else {
digitalWrite(pins, LOW);
}
if (buttonState3 == HIGH) {
onFlag3 = !onFlag3;
delay(250);
}
if (onFlag3 == true){
digitalWrite(pins, HIGH);
percent = 60;
}
else {
digitalWrite(pins, LOW);
}
int PWMvalue = percent * 5 + 1500; //scale up to 1000-2000
for (int i=0; i<arraySize; i++)
controllers[i].writeMicroseconds(PWMvalue);
}