I have been working with this sketch - so far it is performing as I would like with one exception - the actuators "pulse" their way to their positions rather than moving smoothly.
The issue is it makes them slow.
I am thinking it has to do with the Serial.print I wrote into the sketch to troubleshoot it.
Just wondering if this is the case - I see several references to cautions about Serial.print in readxxx() situations.
If so - can I make things smoothly by increasing the baud rate to 115,200 or should I just remove the serial.prints altogether as now I don't really need them?
I can't include the entire sketch as it is too long, here are some snippets as well as the file:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
unsigned int configWord;
// Set pin modes
// Linear Actuator 1 - Lower Belt
// Boom switch input pins
pinMode(b1Pin, INPUT);
pinMode(b2Pin, INPUT);
pinMode(b3Pin, INPUT);
pinMode(b4Pin, INPUT);
pinMode(b5Pin, INPUT);
pinMode(b6Pin, INPUT);
//
pinMode(ss_m1Pin, OUTPUT); digitalWrite(ss_m1Pin, LOW); // HIGH = not selected
pinMode(ss_m2Pin, OUTPUT); digitalWrite(ss_m2Pin, LOW);
pinMode(ss_m3Pin, OUTPUT); digitalWrite(ss_m3Pin, LOW);
pinMode(ss_m4Pin, OUTPUT); digitalWrite(ss_m4Pin, LOW);
// L9958 DIRection pins
pinMode(dir_m1Pin, OUTPUT);
pinMode(dir_m2Pin, OUTPUT);
pinMode(dir_m3Pin, OUTPUT);
pinMode(dir_m4Pin, OUTPUT);
// L9958 PWM pins
pinMode(pwm_m1Pin, OUTPUT); digitalWrite(pwm_m1Pin, LOW);
pinMode(pwm_m2Pin, OUTPUT); digitalWrite(pwm_m2Pin, LOW); // Timer1
pinMode(pwm_m3Pin, OUTPUT); digitalWrite(pwm_m3Pin, LOW);
pinMode(pwm_m4Pin, OUTPUT); digitalWrite(pwm_m4Pin, LOW); // Timer0
// L9958 Enable for all 4 motors
pinMode(enable_motorsPin, OUTPUT);
digitalWrite(enable_motorsPin, HIGH); // HIGH = disabled
configWord = 0b0000010000001100;
SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE1); // clock pol = low, phase = high
// Motor 1
digitalWrite(ss_m1Pin, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(ss_m1Pin, HIGH);
// Motor 2
digitalWrite(ss_m2Pin, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(ss_m2Pin, HIGH);
// Motor 3
digitalWrite(ss_m3Pin, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(ss_m3Pin, HIGH);
// Motor 4
digitalWrite(ss_m4Pin, LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(ss_m4Pin, HIGH);
//Set initial actuator settings to pull at 0 speed for safety
dir1 = 0; dir2 = 0; dir3 = 0; dir4 = 0; // Set direction
pwm1 = 0; pwm2 = 0; pwm3 = 0; pwm4 = 0; // Set speed (0-255)
digitalWrite(enable_motorsPin, LOW);// LOW = enabled
} // End setup
void loop() {
readButton();
}
void readButton()
{
// All switches off - default position all actuators
// All switches on - default position all actuators
b1State = digitalRead(b1Pin);
Serial.print("B1:");
Serial.print(b1State);
b2State = digitalRead(b2Pin);
Serial.print(" B2:");
Serial.print(b2State);
b3State = digitalRead(b3Pin);
Serial.print(" B3:");
Serial.print(b3State);
b4State = digitalRead(b4Pin);
Serial.print(" B4:");
Serial.print(b4State);
b5State = digitalRead(b5Pin);
Serial.print(" B5:");
Serial.print(b5State);
b6State = digitalRead(b6Pin);
Serial.print(" B6:");
Serial.println(b6State);
// Check voltage on Switch 1, If it is HIGH, then Check Voltage on Switch 6
// Else If Switch 1 is HIGH and 6 is HIGH, then move all actuators to default position
if (b1State == HIGH && b6State == HIGH)
{
updateActuator1Default();
updateActuator2Default();
updateActuator3Default();
updateActuator4Default();
}
else if (b1State == HIGH && b6State == LOW && b5State == HIGH)
// Switch 1 and 5 are on, 6 is off, POSITION 6
{
updateActuator1Position6Right();
updateActuator2Position6Right();
updateActuator3Default();
updateActuator4Position6();
}
else if (b1State == HIGH && b6State == LOW && b5State == LOW && b4State == HIGH)
{
// Switch 1 is on, switches 5 and 6 are off, POSITION 5
updateActuator1Position5Left();
updateActuator2Position5Right();
updateActuator3Default();
updateActuator4Position5();
}
else if (b1State == HIGH && b6State == LOW && b5State == LOW && b4State == LOW)
{
// Switch 1 on, all switches on right side are off POSITION 4
updateActuator1Position4Right();
updateActuator2Position4Right();
updateActuator3Default();
updateActuator4Default();
}
and the states:
void updateActuator1Default()
{
Serial.print("A1Default: ");
Serial.print(act1Default);
Serial.print("A1NOW: ");
// move actuator to default position
pot1Current = analogRead(pot1Pin);
Serial.print(pot1Current);
if (act1Default > pot1Current && act1Default > pot1Current + coarse)
{
actuator1_Extending = true;
actuator1_Retracting = false;
dir1 = 1;
pwm1 = 255;
digitalWrite(dir_m1Pin, dir1);
analogWrite(pwm_m1Pin, pwm1);
Serial.print("A1 Extending");
}
else if (act1Default < pot1Current && act1Default < pot1Current + coarse)
{
actuator1_Retracting = true;
actuator1_Extending = false;
dir1 = 0;
pwm1 = 255;
digitalWrite(dir_m1Pin, dir1);
digitalWrite(pwm_m1Pin, pwm1);
Serial.print("A1 Retracting");
}
if (actuator1_Extending = true && pot1Current > act1Default)
{
dir1 = 1;
pwm1 = 0;
digitalWrite(dir_m1Pin, dir1);
digitalWrite(pwm_m1Pin, pwm1);
boolean actuator1_Extending = false;
Serial.print("A1 IDLE");
}
if (actuator1_Retracting = true && pot1Current < act1Default)
{
dir1 = 0;
pwm1 = 0;
digitalWrite(dir_m1Pin, dir1);
digitalWrite(pwm_m1Pin, pwm1);
boolean actuator1_Retracting = false;
Serial.print("A1 IDLE");
}
}
// -------------------------------------------------------------------------------------------------
void updateActuator2Default()
{
Serial.print("A2 Default: ");
Serial.print(act2Default);
Serial.print("A2NOW: ");
pot2Current = analogRead(pot2Pin);
Serial.print(pot2Current);
if (act2Default > pot2Current && act2Default > pot2Current + coarse)
{
actuator2_Extending = true;
actuator2_Retracting = false;
dir2 = 1;
pwm2 = 255;
digitalWrite(dir_m2Pin, dir2);
analogWrite(pwm_m2Pin, pwm2);
Serial.print("A2 Extending");
}
else if (act2Default < pot2Current && act2Default < pot2Current + coarse)
{
actuator2_Retracting = true;
actuator2_Extending = false;
dir2 = 0;
pwm2 = 255;
digitalWrite(dir_m2Pin, dir2);
digitalWrite(pwm_m2Pin, pwm2);
Serial.print("A2 Retracting");
}
if (actuator2_Extending = true && pot2Current > act2Default)
{
dir2 = 1;
pwm2 = 0;
digitalWrite(dir_m2Pin, dir2);
digitalWrite(pwm_m2Pin, pwm2);
boolean actuator2_Extending = false;
Serial.print("A2 IDLE");
}
if (actuator2_Retracting = true && pot2Current < act2Default)
{
dir2 = 0;
pwm2 = 0;
digitalWrite(dir_m2Pin, dir2);
digitalWrite(pwm_m2Pin, pwm2);
boolean actuator2_Retracting = false;
Serial.print("A2 IDLE");
}
}
}
There are no delay() 's the sketch
BeltdividerV4.ino (38.6 KB)
