trim() removes leading and trailing white space newlines carriage returns tabs etc
Actually Arduino String toFloat() is fine with the sylvacRad.trim(); statement
As Bob says remove the float from the FloatRad = sylvacRad.toFloat();
Last programming question and I am done here! Thank you everyone for your help!
I need to use case switch statements to trigger different void codes at specific measurement values.
I want the product to do this in order : {MoveUp();--- TopDownBacklash();--- WaitTop();---MoveDown();---BottomUpBacklash();----WaitBottom();}
And repeat this forever, currently with my code it MovesUp(); without moving to TopDownBacklash(); this is far from the loop I'm seeking. I am using a global int (num) to trigger what switch case to use. I may be doing this wrong, help is again much appreciated thank you all;
int enA = 9;
int in1 = 8;
int in2 = 7;
String sylvacRad;
float FloatRad;
const float Hthreshold = 25.2245;
const float HthresholdOS = 25.2255;
const float Lthreshold = 24.8853;
const float LthresholdOS = 24.8845;
int num = 1;
void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void loop() {
ReadSerial();
switch (num) {
case 1: //Read Serial should change value to 1 to start moveup
MoveUp(); //at end of moveup value changes to 2
case 2:
TopDownBacklash(); //changes to 3 "Wait();"
case 3:
WaitTop();
case 4:
MoveDown();
case 5:
BottomUpBacklash(); //num = 3 "Wait();"
case 6:
WaitBottom();
}
}
void ReadSerial() {
sylvacRad = Serial.readString();
delay(500);
FloatRad = sylvacRad.toFloat();
Serial.println(FloatRad, 4);
}
void MoveUp () {
if (FloatRad < Hthreshold) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 255);
}
num = 2;
}
void TopDownBacklash() {
if (FloatRad > HthresholdOS) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 100);
}
num = 3;
}
void MoveDown() {
if (FloatRad > Lthreshold) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 255);
}
num = 5;
}
void BottomUpBacklash() {
if (FloatRad < LthresholdOS) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 100);
}
num = 6;
}
void WaitTop() {
if (FloatRad > Hthreshold && FloatRad < HthresholdOS) {
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
analogWrite(enA, 180);
}
num = 4;
}
void WaitBottom() {
if (FloatRad < Lthreshold && FloatRad > LthresholdOS) {
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
analogWrite(enA, 180);
}
num = 1;
}
Also is anyone having trouble posting code since the update? I have to add four space before each line of code to get it to post looking right, thanks everyone!