Arduino Code for CIM Motor via Victor SPX speed controller

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);

  
}

You don’t have any code that sets percent to 0. You need to add some code which checks for whatever you want to stop the motor and have it set percent to 0. Or potentially set percent to 0 at the start of loop()?

I was initially using the serial monitor to control the motor percentage (in this case speed) but I needed to switch my whole code to control the speeds manually. Here is part of the “serial” code where I was able to stop the motor when inputting zero! Any suggestion on how to include this part of coding to my code displayed above?

Code:
if (Serial.available() > 1) {
long proposedValue = Serial.parseInt();
if (proposedValue == 0) {
Serial.println(“ah… stop! yes, right on it.”);
percent = 0;
} else if (proposedValue >= -100 && proposedValue <= 100) {
percent = proposedValue;
Serial.print(“of course. value set to “);
Serial.print(percent);
Serial.println(”%”);
} else {
Serial.println(“oh dear. that won’t do.”);
}
}
}

You want both button control and serial control?

Negative, just button control!

Can you express simply what each button is supposed to do?

Each button is assigned to a different speed by the percent listed. However, they should all be able to turn on and off thr motor.

So: button 1 press turns on at say 50%, and a second press would turn it off, but another button would change the speed?

Assuming these are not momentary switches, then your code seems to be too complicated. Start off with simple. Like others said:
at the start of the loop set percent = 0;
read buttons
If buttonState1 is high then set percent = xx; also set your led
same for buttonState2 and buttonState3.

This way your percent output to the motor is only set when a switch is turned on. if all the switches are off, then percent will be 0.

The flags and delays should only be necessary if you are using momentary push buttons. If that is the case you need to check for buttonStateX != HIGH and clear your flag and led.
Check for all 3 flags being off would set the percent to 0.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.