Timer Question

Hi guys.

I have a question for a timer. Essentially, when the switch is operated, the buzzer needs to come on for 500ms and then go off. I could use a delay but I really don’t want to for obvious reasons. I am not familiar with any other timer method so could somebody please shed some light :slight_smile:

If someone could please also explain to me how a message can be shown on an LCD but only whilst the state for it to be on is high, and then the message to dissapear when it is low I would be very thankful! I am aware of the clear function but I do not want to clear the whole screen. The part of code I am talking about for the LCD is at the message “RIDE STOPPED”.

Here is the part of the code I am talking about and the buzzer is “buzzerPin”. It is currently implementing a delay for a timer.

 pressurePlateState = digitalRead(pressurePlate);

   if(pressurePlateState != lastPressurePlateState){
    if(pressurePlateState == LOW){
      digitalWrite(ledGreenPin, HIGH);
      digitalWrite(ledRedPin, LOW);
      digitalWrite(buzzerPin, HIGH);
      delay(200);
      digitalWrite(buzzerPin, LOW);
    }else{
      digitalWrite(ledRedPin, HIGH);
      digitalWrite(ledGreenPin, LOW);
    }
    lastPressurePlateState = pressurePlateState;
   }

And here is the full code (unfinished)

#include <LiquidCrystal.h>

const int reedPin = 10;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int safetySwitch = 9;
const int ledRedPin = 14;
const int ledGreenPin = 15;
const int ledPin = 8;
const int buzzerPin = 16;
const int pressurePlate = 18;


int reedState = 0;
int lastReedState = 0;
int reedCounter = 0;
int minuteCounter = 0;
int safetySwitchState = 0;
int lastSafetySwitchState = 0;
int pressurePlateState = 0;
int lastPressurePlateState = 0;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

  pinMode(reedPin, INPUT);
  pinMode(safetySwitch, INPUT);
  
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Minute wait:");
  lcd.setCursor(12,0);
  lcd.print(minuteCounter);
  lcd.setCursor(2,1);
  lcd.print("RIDE OPEN");

  digitalWrite(ledGreenPin, HIGH);
  
}

void loop() {


reedState = digitalRead(reedPin);

 if(reedState != lastReedState) {
  if(reedState == LOW){
   reedCounter++;


   if(reedCounter % 6 == 0) {
    minuteCounter++; 
    lcd.setCursor(12,0);
    lcd.print(minuteCounter); 
   }
  }
  delay(1000);
 }
 
   lastReedState = reedState;
 safetySwitchState = digitalRead(safetySwitch);
 if(safetySwitchState != lastSafetySwitchState){
  if(safetySwitchState == HIGH){
    digitalWrite(ledGreenPin, HIGH);
    digitalWrite(ledRedPin, LOW);
    lcd.setCursor(2,1);
    lcd.print("RIDE STOPPED");
    }else{
      digitalWrite(ledGreenPin, LOW);
      lcd.setCursor(2,1);
      lcd.print("RIDE OPEN   ");
    }
   
 }
  delay(100);
  lastSafetySwitchState = safetySwitchState;
  
  pressurePlateState = digitalRead(pressurePlate);

   if(pressurePlateState != lastPressurePlateState){
    if(pressurePlateState == LOW){
      digitalWrite(ledGreenPin, HIGH);
      digitalWrite(ledRedPin, LOW);
      digitalWrite(buzzerPin, HIGH);
      delay(200);
      digitalWrite(buzzerPin, LOW);
    }else{
      digitalWrite(ledRedPin, HIGH);
      digitalWrite(ledGreenPin, LOW);
    }
    lastPressurePlateState = pressurePlateState;
   }
 }

Thanks very much in advance.
Marcus

Do you have a grasp on the first five demos in IDE->file/examples/digital ? Take a look too at 'Demonstration code for doing several things at the same time'.

Marcusarduino:
If someone could please also explain to me how a message can be shown on an LCD but only whilst the state for it to be on is high, and then the message to dissapear when it is low I would be very thankful!

Once you understand state change detection you can do this easily. It also means you can print to the LCD when some boolean state *goes * high/low and erase when it goes low/high.

To print:

   lcd.setCursor(2,1);
    lcd.print("RIDE STOPPED");

To clear:

   lcd.setCursor(2,1);
    lcd.print("            "); // 12 spaces

@OP

To use TC1 of the MCU for generating 'time delay', you may consult this post and also this post.