Hey folks, I've got a little sketch here wherein I'm controlling the speed of three motors with three pots. Under normal operation, it is just that. But I wanted to put an alternate routine in where I just feed constant speed values to all three motors when I flip a switch (Sort of a program mode). As long as the switch is flipped, the program cycles between 4 or 5 "scenes", separated by 3 second intervals. The problem is- the first "if" in the loop is being ignored and the switch doesn't work. No matter if I flip the switch or not, I can't get the sketch to goto the runPGM function. I'm no doubt doing this wrong, but I'm not getting it. Anyone have a suggestion?
Here is the code:
int speedSet1 = A0;
int speedSet2 = A1;
int speedSet3 = A2;
int motor1 = 9;
int motor2 = 10;
int motor3 = 11;
int speedVal1 = 0;
int speedVal2 = 0;
int speedVal3 = 0;
int motor1Speed = 0;
int motor2Speed = 0;
int motor3Speed = 0;
int motor1Limit = 35;
int motor2Limit = 35;
int motor3Limit = 35;
int LED1 = 5;
int LED2 = 6;
int LED3 = 7;
int PGM_SW = 4;
int val = 0;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(PGM_SW, INPUT);
Serial.begin(9600);
}
void loop() {
//Check to see if the PGM switch has been switched
val = digitalRead(PGM_SW);
if (val != 0) {
runPGM;
}
//Motor 1
speedVal1 = analogRead(speedSet1);
speedVal1 = analogRead(speedSet1);
motor1Speed = map(speedVal1, 0, 1023, 30, 255);
if (motor1Speed <= motor1Limit) //If not at lower speed limit..
{
digitalWrite(motor1, LOW); //Turn off LED and send 0 to motor (Stop)
digitalWrite(LED1, LOW);
}
else //Otherwise...
{
digitalWrite(LED1, HIGH); //Turn on LED and send PWM to motor
analogWrite(motor1, motor1Speed); //according to value from pot
}
//Motor 2
speedVal2 = analogRead(speedSet2);
speedVal2 = analogRead(speedSet2);
motor2Speed = map(speedVal2, 0, 1023, 30, 255);
if (motor2Speed <= motor2Limit)
{
digitalWrite(motor2, LOW);
digitalWrite(LED2, LOW);
}
else
{
digitalWrite(LED2, HIGH);
analogWrite(motor2, motor2Speed);
}
//Motor 3
speedVal3 = analogRead(speedSet3);
speedVal3 = analogRead(speedSet3);
motor3Speed = map(speedVal3, 0, 1023, 30, 255);
if (motor3Speed <= motor3Limit)
{
digitalWrite(motor3, LOW);
digitalWrite(LED3, LOW);
}
else
{
digitalWrite(LED3, HIGH);
analogWrite(motor3, motor3Speed);
}
//Serial.print("motor1Speed = " );
//Serial.print(motor1Speed);
//Serial.print(" Switch = ");
//Serial.println(val);
//delay(5);
}
void runPGM() {
do {
analogWrite(motor1, 90);
analogWrite(motor2, 50);
analogWrite(motor3, 140);
delay (3000);
analogWrite(motor1, 30);
analogWrite(motor2, 70);
analogWrite(motor3, 125);
delay (3000);
analogWrite(motor1, 130);
analogWrite(motor2, 190);
analogWrite(motor3, 200);
delay (3000);
analogWrite(motor1, 170);
analogWrite(motor2, 100);
analogWrite(motor3, 80);
delay (3000);
Serial.print("motor1Speed = " );
Serial.print(motor1Speed);
Serial.print("motor2Speed = " );
Serial.print(motor2Speed);
Serial.print("motor3Speed = " );
Serial.println(motor3Speed);
delay(5);
}
while (val == 1);
}