Cannot read analog signal while checking for button-input

I don’t really know the norm in here, so I am just going to go straight to my problem.

I am trying to make a simple alarm consisting of two parts.

The first one is a photocell and a LASER pointer. My arduino reads the voltage from the photocell and prints it to serial monitor. This part works great on its own.

The other part is a button. It is wired with the normal pull-down resistor and the button itself seem to work perfectly. The problem is integrating the two components into one, and writing the code for it.

The idea is to be able to push the button and by doing that, turn on the alarm. As a visual marker, to tell me that the alarm is turned on, the LED should also give me a short blink. It should then start reading signals from the photocell, and send a signal to the LED when breached (in lack of better word). When the alarm is then triggered by lighting up the LED, I want to be able to press the button, and deactivate the alarm, until the button is pressed once again, and the process repeats itself.
It this stage, with the code and hopefully a fritzing sketch, what happens is that the LED is turned on when button is pressed, and it stays on. In serial monitor, the voltage is read and printed once. If I press again, the LED turns of, and the voltage is read and printed once more.

I am quite new to arduino and coding in general, so I would appreciate if you could, if not skip the most difficult term, at least explain them with a few words.

I am using the Arduino UNO R2.

Henrik

int switchpin = 4;
int alarm = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
int fotocelleinn = 0;

void setup()
{
  pinMode(switchpin, INPUT);
  pinMode(alarm, OUTPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchpin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchpin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn  = !ledOn;
    digitalWrite(alarm, HIGH);
    delay(100);
    digitalWrite(alarm, LOW);
    fotocelleinn = analogRead (A0);
    float voltage = fotocelleinn * (5.0/2023.0); 
    Serial.println(voltage);
    delay(10);
    
  }
 lastButton = currentButton;
 digitalWrite(alarm, ledOn);

}

Your serial output is inside the loop that only gets to operate when the switch changes.

At other times, the output is not accessed. Move it outside the "if" statement

Weedpharma

This is a pretty good read.... Most everything is on page 1.

Ray
My Projects

Hi,

The lower LED in your fritzy diagram has no current limit resistor, it is connected directly across the 5V supply.

Also check your LDR wiring, it should be in potential divider configuration.

Tom.... :slight_smile:

Thank you for good answers, I am currently reading up on it.

On what you Said TomGeorge - I forgot to say, but it is my LASER, pointing towards the photocell. I was not able to find a proper LASER-model in fritzing. I just made a small shortcut on the LASER, since I just thought it was not the problem.

I actually just watched a video on voltage dividers, but it does works perfectly fine as it is.

Henrik