please help!

i have a code that will work with two serial inputs from an app to start and stop a pwm. but when i add a third input from the app it stops everything from working.. basically i have the letter c being sent when the thumb position on the slider is moved, and after c is received it should read the value and input it into my delay.

int mos_top = 3;
int mos_bot = 5;
int data = 0;
int Intens = 150;
int state = 0;

void setup() {

Serial.begin(9600);
pinMode(mos_top,OUTPUT);
pinMode(mos_bot,OUTPUT);
digitalWrite(mos_top,LOW);
digitalWrite(mos_bot,LOW);
}

void loop() {
delayMicroseconds(1);
data = Serial.read();

{
data = Serial.read();
}
while (data == 'a')
{
Serial.println("POWER: ON");
digitalWrite(mos_top, HIGH);
digitalWrite(mos_bot, LOW);
delay(Intens);
digitalWrite(mos_top, LOW);
digitalWrite(mos_bot, HIGH);
delay(Intens);
if(Serial.available()>0)
data = Serial.read();
if (data == 'b'){
break;
Serial.println("POWER; OFF");
}

}
if(Serial.available()>0)
data = Serial.read();
if (data == 'c');
Serial.println("Intens");
String In = Serial.readString();
Intens = 150 + In.toInt();
}

 data = Serial.read();

  {
  data = Serial.read();
  }

Why do you chuck the first value away?

Please remember to use code tags when posting code

Don't use WHILE in this

while (data == 'a')

instead use IF and allow loop() to do the repetition. That way you will only need

if(Serial.available()>0)
  data = Serial.read();

in one place for the entire program.

When you find yourself repeating lines of code it usually means the program design is wrong.

...R
Serial Input Basics - simple reliable ways to receive data.

AWOL:

 data = Serial.read();

{
  data = Serial.read();
  }


Why do you chuck the first value away?

Please remember to use code tags when posting code

sorry that was me trying anything i could to get the "a" value to read after i added the "c" value, here is a copy of the code working, all i want to do is vary the delay with the slider value but when i add it in it destroys the code.

int mos_top = 3;
int mos_bot = 5;
int data = 0;
int Intens = 0;
int state = 0;
void setup() {

Serial.begin(9600);
pinMode(mos_top,OUTPUT);
pinMode(mos_bot,OUTPUT);
digitalWrite(mos_top,LOW);
digitalWrite(mos_bot,LOW);
}

void loop() {
delayMicroseconds(1);
if(Serial.available()>0)

{
data = Serial.read();
}
while (data == 'a')
{
Serial.println("POWER: ON");
digitalWrite(mos_top, HIGH);
digitalWrite(mos_bot, LOW);
delay(200);
digitalWrite(mos_top, LOW);
digitalWrite(mos_bot, HIGH);
delay(200);
if(Serial.available()>0)
data = Serial.read();
if (data == 'b'){
break;
Serial.println("POWER; OFF");
}
}

}

AWOL:
Please remember to use code tags when posting code

Please read the "How to use this forum" post at the top of any board if you don't know what code tags means. Thumbing your nose at the forum rules will not make you a lot of friends.

You should read reply #2 again. There's some useful help there that you seem to have ignored. Why?

{
    data = Serial.read();
    }

Why the extra braces? Do you know what purpose the braces serve in a C or C++ program?

there is this bit of code

if(Serial.available()>0)
    data = Serial.read();
    if (data == 'b'){
    break;
    Serial.println("POWER; OFF");
    }

i Have no idea what the break; does here.
not only that there seems to be an opening '{' missing, actually the longer i look at that bit the less it makes sense.

On the other hand if you remove all the other test for Serial input and go for something like this

if(Serial.available()>0)  {
    char data = Serial.read(); // it should not be an int if you want to compare as a char
    switch (data) {
      case 'a': {
         // do the 'a' stuff
      }
      break;
      case 'b': {
         // do the 'b' stuff
      }
      break;
      case 'c': {
         // do the 'c' stuff
      }
      break;
    }
  }

you'll have a clean solution.

Delta_G:
Please read the "How to use this forum" post at the top of any board

Just seeing the topic name indicates the OP hasn't done this.

Deva_Rishi:
Just seeing the topic name indicates the OP hasn't done this.

Yes but now he has been instructed twice. So there's no excuse going forward.

If more people would bother to read the directions before they just dive right in then a lot less of them would be having issues like this OP.

Delta_G:
Please read the "How to use this forum" post at the top of any board if you don't know what code tags means. Thumbing your nose at the forum rules will not make you a lot of friends.

You should read reply #2 again. There's some useful help there that you seem to have ignored. Why?

{

data = Serial.read();
   }




Why the extra braces? Do you know what purpose the braces serve in a C or C++ program?

sorry for not following the rules of the forum, and no im not very clear on the reason for the braces... i have put this code together through alot of trial and error and what i could scramble together online... i tried using an if statement on the (data=="a") function but it ruined the way the loop breaks, every time i try to edit the way the braces are set i get stuck in a sh*t loop of reformatting and running through it again and again to have it not work as intended... im pretty stumped at this point and i didnt ignore response two. i actually found the page he linked to me last night (before i posted here ) wile troubleshooting and tried to implement what he said with no success...

Deva_Rishi:
there is this bit of code

if(Serial.available()>0)

data = Serial.read();
   if (data == 'b'){
   break;
   Serial.println("POWER; OFF");
   }



i Have no idea what the break; does here.
not only that there seems to be an opening '{' missing, actually the longer i look at that bit the less it makes sense. 

On the other hand if you remove all the other test for Serial input and go for something like this


if(Serial.available()>0)  {
   char data = Serial.read(); // it should not be an int if you want to compare as a char
   switch (data) {
     case 'a': {
        // do the 'a' stuff
     }
     break;
     case 'b': {
        // do the 'b' stuff
     }
     break;
     case 'c': {
        // do the 'c' stuff
     }
     break;
   }
 }



you'll have a clean solution.

to clear things up, break was the only way i could find to exit that loop, when "a" is seen on is pressed i want the loop to run when "b" is seen off is pressed and i want the break the loop. i know the code is abit of a mess because it took me alot of unorthodox scambling to get it to function as an on off. when "c" is seen i want it to take the serial and implement the value into delay wile the "a" function is still running and only turn off at "b" activating the break.

simon2123:
to clear things up, break was the only way i could find to exit that loop, when "a" is seen on is pressed i want the loop to run when "b" is seen off is pressed and i want the break the loop. i know the code is abit of a mess because it took me alot of unorthodox scambling to get it to function as an on off. when "c" is seen i want it to take the serial and implement the value into delay wile the "a" function is still running and only turn off at "b" activating the break.

wow i think its close ! thanks for you help! i dont know why but now the loop ends without seeing 'b' i want 'a' to trigger a loop with 'c' changing a value and 'b' to terminate everything.. any thoughts?

int mos_top = 3;
int mos_bot = 5;
char data;
int Intens = 0;
int state = 0;
void setup() {

Serial.begin(9600);
pinMode(mos_top,OUTPUT);
pinMode(mos_bot,OUTPUT);
digitalWrite(mos_top,LOW);
digitalWrite(mos_bot,LOW);
}

void loop() {
delayMicroseconds(1);
if(Serial.available()>0){
char data = Serial.read();
switch (data) {
{
case 'a':

Serial.println("POWER: ON");
digitalWrite(mos_top, HIGH);
digitalWrite(mos_bot, LOW);
delay(Intens);
digitalWrite(mos_top, LOW);
digitalWrite(mos_bot, HIGH);
delay(Intens);
}
{
case 'b':
break;
Serial.println("POWER; OFF");
}
{
case 'c':
Serial.println("Intens");
String In = Serial.readString();
Intens = In.toInt();
}
}
}
}

simon2123:
wow i think its close ! thanks for you help! i dont know why but now the loop ends without seeing 'b' i want 'a' to trigger a loop with 'c' changing a value and 'b' to terminate everything.. any thoughts?

int mos_top = 3;
int mos_bot = 5;
char data;
int Intens = 0;
int state = 0;
void setup() {

Serial.begin(9600);
pinMode(mos_top,OUTPUT);
pinMode(mos_bot,OUTPUT);
digitalWrite(mos_top,LOW);
digitalWrite(mos_bot,LOW);
}

void loop() {
delayMicroseconds(1);
if(Serial.available()>0){
char data = Serial.read();
switch (data) {
{
case 'a':

Serial.println("POWER: ON");
digitalWrite(mos_top, HIGH);
digitalWrite(mos_bot, LOW);
delay(Intens);
digitalWrite(mos_top, LOW);
digitalWrite(mos_bot, HIGH);
delay(Intens);
}
{
case 'b':
break;
Serial.println("POWER; OFF");
}
{
case 'c':
Serial.println("Intens");
String In = Serial.readString();
Intens = In.toInt();
}
}
}
}

its reading case 'b' without the input... and case 'a' wont loop

Case a doesn’t have a break, so case b runs right after it.

Switch case doesn’t loop. The loop function loops. You need to design so that your switch and case gets run over and over from loop, not to trap execution in one small part.

i actually found the page he linked to me last night (before i posted here )

And still refuses to post code in code tags.

evanmars:
And still refuses to post code in code tags.

Yeah, maybe about time to let this one die until OP can be bothered to read the guidelines. I reply #8 he is saying that he knows he is breaking the rules. But he doesn’t seem to care. We should care just as much about his doomed project.

evanmars:
And still refuses to post code in code tags.

Delta_G:
Case a doesn’t have a break, so case b runs right after it.

Switch case doesn’t loop. The loop function loops. You need to design so that your switch and case gets run over and over from loop, not to trap execution in one small part.

i was trying to use the case function as suggested before, but i didn't know they would all run in succession without breaks, how can i loop case 'a'. all i want is case 'a' looping and case 'b' to end the loop. if not the previously instructed switch function what would make this possible? im sorry for posting my code in a poor format and i do very much appreciate the responses, they have been helpful!

im sorry for posting my code in a poor format

Post some code properly and I’ll help. But last time you said sorry you just continued to do things the wrong way. So I don’t think you really are.

Your switch case can work if you code it right. But you don’t want one case to “loop”. You really just want the loop function to keep coming to that case. And you can have that. But you’ll have to write that correctly.

int mos_top = 3;
int mos_bot = 5; 
char data;
int Intens = 0;
int state = 0;
void setup() {  
  
  Serial.begin(9600);
  pinMode(mos_top,OUTPUT);
  pinMode(mos_bot,OUTPUT);
  digitalWrite(mos_top,LOW); 
  digitalWrite(mos_bot,LOW);
  }

  void loop() {
  delayMicroseconds(1);
  if(Serial.available()>0){
  char data = Serial.read();
  switch (data) {
  case 'a':{
  
  Serial.println("POWER: ON");
  digitalWrite(mos_top, HIGH);  
  digitalWrite(mos_bot, LOW);
  delay(Intens);
  digitalWrite(mos_top, LOW);
  digitalWrite(mos_bot, HIGH);
  delay(Intens);
  }
  case 'b':{
  Serial.println("POWER; OFF");
  }

  case 'c':  {
  Serial.println("In.toInt");
  String In = Serial.readString();
  Intens = In.toInt();
}
  }
  }
  }

my bad for the forum title... i haven't changed the code much since i started the switch data function, and i am unsure of how to have loop read the switch function every loop without reading the other cases thanks for your patience with me.

Each case needs a break statement. Otherwise all the cases run. Google “C++ switch case” and learn a little about how those work.

case 'a' from the on button will only be sent once though.... if it cycles again will it not require 'a' to be true still?