Infinite loop problem

hey i'v got an issue with infinite loop thats my guess but i don't know where and how. Idea behind this is for button to act as on of switch and led light to switch via fotoresistor x->y green y->z red.
i'll be glad if some one could look at it.

Program:

int photocellPin = A2;
int photocellReading;
int photocellReading2;
int LEDpinC = 13;
int LEDpinZ = 12;
int LEDbrightnessC;
int LEDbrightnessZ;
int LEDwork;
int button;

void setup (){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(2, INPUT);
  pinMode(8, INPUT);

}

void loop ()
{
  Serial.print("Analog reading =");
  Serial.println(photocellReading);
 photocellReading = analogRead(photocellPin);
 photocellReading2= analogRead(photocellPin);
  button = digitalRead(8);
  LEDwork = digitalRead(2);
  delay(100);
 
  if(button == 1)
  {
    if(LEDwork == 0)
    {
      
      	digitalWrite(LEDpinC, 1);  
         digitalWrite(LEDpinZ, 0);
      delay(100);
       }
  }
  button = digitalRead(8);
  LEDwork = digitalRead(2);
    if(button == 0)
   {
     if(LEDwork == 1)
   {
       if(photocellReading<500)
           {
             LEDbrightnessC=1;
             digitalWrite(LEDpinC, LEDbrightnessC);
             
             LEDbrightnessZ=0;
             digitalWrite(LEDpinZ, LEDbrightnessZ);
             delay(100);
           }
           else if(photocellReading>500)
           {
             LEDbrightnessZ=1;
             digitalWrite(LEDpinZ, LEDbrightnessZ);
            
             LEDbrightnessC=0;
             digitalWrite(LEDpinC, LEDbrightnessC);
             delay(100);
           }
         }
       }
  button = digitalRead(8);
  LEDwork = digitalRead(2);
   if(button == 1)
  {
   if(LEDwork == 1)
      {
        digitalWrite(LEDwork, 0);
        digitalWrite(LEDpinZ, 0);
        digitalWrite(LEDpinC, 0);
        delay(100);
      }
  }
}

Board:

loop() is an infinite loop. Have you created another?

Why do you have digital pin 2 connected to GND like this?

i had to take signal from leds since i have 2 of them and they never work in the same time so i figured thats the way

the issue is im stuck in this part of the program and it does not let me out i dont know why since pin 2 is no longer low after it's been set to 1 by led

if(button == 1)
  {
    if(LEDwork == 0)
    {
      
      	digitalWrite(LEDpinC, 1);  
         digitalWrite(LEDpinZ, 0);
      delay(100);
       }
  }

That code may be right or wrong, but certainly is not a loop of any type.

ohh you mean this

image

i was trying to do something in order to check parts of my program and it stayed there by mystake but results are same

after i press button green led turns on for some reason and stays on it doesn't go to next parts of program where led can be switched by fotoresistor

You just proved that loop() is an infinite loop.

Hello madmixx
Clean up your sketch.
Don´t use magic numbers.
Read input pins once a loop and take action.

If you added a debugging statement like Serial.println(LEDwork); you would see how fast it gets in and out of your loop.

With an infinite ground-loop to pin 2, I'd bet a penny that you'd get 10Hz of zeros with button == 1, and as fast as 9600 baud prints "0\n" with button == 0.

Well guys thanks for help i just fixed it i was just dumb code is fine (almoust)

Is pin 2 still connected to the ground side of the LEDs?

yeah via resistor


like this

int photocellPin = A2;
int photocellReading;
int photocellReading2;
int LEDpinC = 13;
int LEDpinZ = 12;
int LEDbrightnessC;
int LEDbrightnessZ;
int LEDwork;
int button;

void setup (){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(2, INPUT);
  pinMode(8, INPUT);

}

void loop ()
{
  Serial.print("Analog reading =");
  Serial.println(photocellReading);
 photocellReading = analogRead(photocellPin);
 photocellReading2= analogRead(photocellPin);
  button = digitalRead(8);
  LEDwork = digitalRead(2);
  delay(100);
 
  if(button == 1)
  {
    if(LEDwork == 0)
    {
      	digitalWrite(LEDpinC, 1);  
         digitalWrite(LEDpinZ, 0);
      delay(100);
       }
  }
  button = digitalRead(8);
  LEDwork = digitalRead(2);
    if(button == 0)
   {
     if(LEDwork == 1)
   {
       if(photocellReading<500)
           {
             LEDbrightnessC=1;
             digitalWrite(LEDpinC, LEDbrightnessC);
             
             LEDbrightnessZ=0;
             digitalWrite(LEDpinZ, LEDbrightnessZ);
             delay(100);
           }
           else if(photocellReading>500)
           {
             LEDbrightnessZ=1;
             digitalWrite(LEDpinZ, LEDbrightnessZ);
            
             LEDbrightnessC=0;
             digitalWrite(LEDpinC, LEDbrightnessC);
             delay(100);
           }
         }
       }
  button = digitalRead(8);
  LEDwork = digitalRead(2);
   if(button == 1)
  {
   if(LEDwork == 1)
      {
        digitalWrite(LEDwork, 0);
        digitalWrite(LEDpinZ, 0);
        digitalWrite(LEDpinC, 0);
        delay(100);
      }
  }
}

there is code to it if you want to try and see

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