Joe Paul
Supporting Member
Hi Everyone,
So I was given part of this code, but I don't know what I'm doing incorrectly, but the state machine not working:
So, any ideas or suggestions appreciated.
Tried to test with the serial monitor, data is not getting through.
Thanks!
Take care, Joe.
So I was given part of this code, but I don't know what I'm doing incorrectly, but the state machine not working:
Code:
#define numChannels 2
#include <VarSpeedServo.h>
byte channelValues[numChannels];
uint8_t readstate = 0;
bool channelsOk = false;
VarSpeedServo servo1;
VarSpeedServo servo2;
void setup() {
Serial.begin(115200);
servo1.attach(2);
servo2.attach(3);
servo1.write(95,75);
servo2.write(95,75);
}
void loop() {
static uint8_t iChn;
switch (readstate) {
case 0: // waiting for a '~'
if( Serial.read() != '~') break;
readstate = 1;
// falls through
case 1: // Waiting for a '!'
if( Serial.read() != '!' ) {
readstate = 0;
break;
}
iChn = 0;
readstate = 2;
channelsOk = false; // don't update the servos while we are updating their positions
// falls through
case 2: // reading the channelVaues
byte channelValues[numChannels];
for(byte iChn = 0; iChn < numChannels; iChn++)
channelValues[iChn] = Serial.read(); //read channel value from serial data
if( ++iChn < numChannels ) break;
iChn = 0;
readstate = 0; // reset the read state for the next command stream.
channelsOk = true; // ok, now we can move the servos
servo1.write(map(channelValues[0], 0,255, 84, 120), 75); //Schroeder 90-110
servo2.write(map(channelValues[1], 0,255, 90, 110), 75); //Patty 90-110
}
}
So, any ideas or suggestions appreciated.
Tried to test with the serial monitor, data is not getting through.
Thanks!
Take care, Joe.
Last edited: