Void loop it gets stuck at the end of the program(help)

I use bluetooth and bluetooth electronics app on phone. When I press a button and then another in the application I have a loop in the last part of the code:

int gaisma;
float lights;
bool manual = 0;
int n;
char k;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 
}

void loop() {
  // put your main code here, to run repeatedly:
gaisma = 1023 - analogRead (A0);
//gaisma = map(gaisma, 0,1023,0,100);
lights = gaisma * 0.09775171;
Serial.println("*I"+String(lights)+"%*");
Serial.println(lights, 1);

 if(Serial.available() > 0) 
    {k = Serial.read();}
  
 
   if ( k == 'N')
    {
      manual = 1;
       }
      
      if ( k == 'K' )
    {
      manual = 0;
      }
      


if (lights <= 30 && manual == 0 )
{Serial.print("*MR255G255B255*");}

if (lights <= 80, lights >= 41 && manual == 0)
{Serial.print("*MR150G150B150*");}
if (lights <= 100,lights >= 81 && manual == 0)
{Serial.print("*MR0G0B0*");}
 
 if ( k == 'O' && manual == 1 )
    {
      n = Serial.parseInt();
      Serial.print("*MR250G00B00*");
    Serial.println(n);}
    return;

}

In this part there is a constant cycle but sometimes it comes back as needed

 
 if ( k == 'O' && manual == 1 )
    {
      n = Serial.parseInt();
      Serial.print("*MR250G00B00*");
    Serial.println(n);}
    return;

P.S. Maybe there is a video with this program and how to program it. Thanks.

This is probably not doing what you think

if (lights <= 80, lights >= 41 && manual == 0)

Maybe you mean

if (lights <= 80 && lights >= 41 && manual == 0)

Fix all your similar misuses of the comma operator.

HTH

a7

It will also help you if you formatted your code and got the indentations sorted.

What is the purpose of the return statement near the end of loop?

Hello iljas123

Welcome to the best Arduino forum ever :smile:

I have done a code review.
In addition to the errors already mentioned, there is also a design error in the programming of the serial interface in the sketch.

There is a conflict at

 if(Serial.available() > 0) 

and

 n = Serial.parseInt();

which steal each other's characters from the interface.

Rethink the overall design again.

1 Like

Thank so much!!!

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