Toggle timed output with analogRead

Hi

I'm having some difficulty understanding the millis() when trying to create an output to go high for X time, and adding in to the complication of using a analogRead as the trigger of that event. So, simply put, what I'm trying to accomplish is if the analog value goes above a certain value (500 in my case) I am tying to have a relay, attached to digital 22 turn on for, in this case, 750ms.

The AREF is external, and the reading of the analogRead is working just fine.

oid setup() {
Serial.begin(9600);
analogReference(EXTERNAL);
pinMode(A12, INPUT); // Mic In
pinMode(22, OUTPUT); //Trigger output


void loop() 

int m0 = analogRead(A15);
unsigned int Releaseperiod = 750;
unsigned long time_now = 0; 
unsigned int offAt = 750;
Serial.println(m0);
delay(1);
  
{
  (analogRead(m0) >= 500);
  digitalWrite(Release, HIGH);
  
 //Here is where I cannot figure out how to add in the millis to keep the relay on for 750ms  
 
  digitalWrite(Release, LOW);


loop()  }




Welcome

Here is an example I made for someone else : t940977.ino - Wokwi Arduino and ESP32 Simulator

When the slider is below a value, alarm is on for a few seconds, then stops, and the LED will turn on.

there were several changes needed just to make the code compilable though senseless

const byte anlgPin    = A0;
const byte releasePin = LED_BUILTIN;

void setup()
{
    Serial.begin(9600);
    analogReference(EXTERNAL);
    pinMode(anlgPin, INPUT); // Mic In
    pinMode(22, OUTPUT); //Trigger output
}

void loop()
{
    int m0 = analogRead(anlgPin);
    Serial.println(m0);
    delay(1);
    {
        (analogRead(m0) >= 500);
        digitalWrite(releasePin, HIGH);
        //Here is where I cannot figure out how to add in the millis to keep the relay on for 750ms
        digitalWrite(releasePin, LOW);
    }
    // loop() - why is loop() called within loop() ?
}

consider following which may do what you're attempting

const unsigned int ReleasePeriod = 750;

const byte anlgPin = A1;
const byte ledPin  = LED_BUILTIN;

unsigned long msecLst;

enum { Off = HIGH, On = LOW };

void setup ()
{
    Serial.begin (9600);

    pinMode (ledPin, OUTPUT);
    digitalWrite (ledPin, Off);
}

void loop ()
{
    unsigned long msec = millis ();

    if (On == digitalRead (ledPin) && (msec - msecLst) > ReleasePeriod)  {
        digitalWrite (ledPin, Off);
    }

    int m0 = analogRead (anlgPin);
    Serial.println (m0);

    if (500 < m0)  {
        msecLst = msec;
        digitalWrite (ledPin, On);
    }
}

My take on the solution:

// if the analog value goes above a certain value (500 in my case)
// I am tying to have a relay, attached to digital 22 turn on for,
// in this case, 750ms.

const byte AnalogPin = A0;
const byte RelayPin = 22;
const unsigned long RelayOnTime = 750;

void setup()
{
  digitalWrite(RelayPin, HIGH); // Relay off
  pinMode(RelayPin, OUTPUT);
}

void loop()
{
  // if the analog value goes above a certain value (500 in my case)
  if (analogRead(AnalogPin) > 500)
  {
    // have a relay, attached to digital 22 turn on
    digitalWrite(RelayPin, LOW);  // Relay on
    // for, in this case, 750ms.
    delay(RelayOnTime);
    digitalWrite(RelayPin, HIGH);  // Relay off
  }
}

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