Start/stop/read functions

Hello, I'm new here and so far it's been a great experience. Up until last friday I had never touched an arduino board, or soldered, or had much experience with C++.

I'm working on a project and so far I've been able to overcome all of my obstacles, but I'm really really stuck on this part.

I have an app that sends data to the serial monitor. The app has 3 slider buttons and needs to have a start/stop button.

Sider 1 sends a 1-10 to the serial monitor, slider 2 sends 20-30, slider 3 sends 40-50.

my code goes like this:

if(SerialBT.available()) 
{
state = SerialBT.readStringUntil('\n');
Serial.println(state);
newState = state.toInt();
}
if(newState > 0 && newState < 11) 
{
variableOne = newState;
}
else if(newState > 19 && newState < 31) 
{
variableTwo = newState;
}
else if(newState > 39 && newState < 51) 
{ 
variableThree = newState;
}
ledcWrite(0, variableOne);
delay(variableTwo);
ledcWrite(0, 0);
delay(variableTwo);
delay(variableThree);
}

I need a start/stop button though because right now if I run the app and start messing with sliders things don't go as planned.

like

if(SerialBT.read() == 'start')
{
read serial monitor for variables 1, 2, and 3. Do loop
}
else if(SerialBT.read() == 'stop')
{ 
return; wait for if(SerialBT.read() == 'start')
}

but I can't find a way that actually does that and I don't know how to make it read the 3 lines before the "start" button. Maybe I'm just thinking about it wrong. Any help would be greatly appreciated.

Your Serial.read statements ‘consume’ the buffer contents, so any following read call will return nothing, or the following content…

You need to read the data into a temporary string, then test that against your required values.

There are better ways, but this will get you on the road.

Is the BT receiving a character array containing 'start' or a String containing the word "start"?

This 's', a character, and this "s", a String, mean different things.

1 Like

Can you post your entire sketch please? That would be helpful.

When pushing the start button on the app it sends the word start to the arduino and prints it in the serial monitor. When stop is pressed it stops. My problem is, as written, when i upload the code and connect to my arduino via bluetooth, and adjust one of the sliders, the code starts before it has the vallue from all of the sliders. I need to make it wait until the start button is pressed, read the last 3 lines of the serial, which will read like:

5
22
45

And assign those values to variable 1, 2, and 3, and then finally execute the code.

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