Help needed for my assignment

Hey, I am a beginner at programming and I'm stuck at this assignment where I have to change this code so when movement is detected the light bulb would turn on for a minute without delay() and then turn off again. I would appreciate any help I can get. And sorry if my English is rough it's not my native language.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
  lcd.begin(16, 2);
  pinMode(A5, OUTPUT);
  pinMode(A3, INPUT);
}

void loop() {
  byte mA3 = digitalRead(A3);
  String msg;
  if(mA3){
    msg = "Movement";
    digitalWrite(A5, HIGH);
  }
  else{
    msg = "No movement";
    digitalWrite(A5, LOW);
  }
  lcd.home();
  lcd.print(msg);
  lcd.print("                ");
}

You want someone just to write the code for you or are you looking for a hand with writing the code so you can learn something?

Looking for a hand so I can learn something

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 60000;
String msg = "No movement";

void setup() {
  lcd.begin(16, 2);
  pinMode(A5, OUTPUT);
  pinMode(A3, INPUT);
}

void loop() {
  byte mA3 = digitalRead(A3);
  String msg;
  if(mA3){
    msg = "Movement";
    digitalWrite(A5, HIGH);
    startMillis = millis();
  }
  else{
    currentMillis = millis();
    if (currentMillis - startMillis >= period){
      msg = "No movement";
      digitalWrite(A5, LOW);
    }
  }
  lcd.home();
  lcd.print(msg);
  lcd.print("                ");
}

Thanks for all the links, I updated my code using millis tutorial and it works perfectly now. Thanks for all the help.

1 Like