Inactivity timeout

Hello all,

I've been trying for a long time to create an inactivity timeout to work. It seems so simple, but I can't get anything to work just right.

I have an audio signal that turns ledPin12 on and off via analogRead. I want to be able to write ledPin2 high if ledPin12 has been low for 2 seconds, but once pin 12 goes high, I want pin 2 to go low IMMEDIATELY and reset the timer.

The purpose of this is that I am controlling a device that has an auto power off function which I could not figure out how to disable locally, so I'm trying to defeat it with code. When ledPin2 goes high, it turns on a 4066 quad bilateral switch which slows the clock on the device down to a crawl so that it can't reach its auto power off cycle. When an event on the device is triggered, which I'm doing through ledPin12, it resets the device's auto off cycle. I want ledPin2 to go low at the same time so that it sets the device's clock back to normal speed by switching off the 4066.

This is the closest I've come, using elapsedMillis. What happens here is as ledPin 12 pulses, ledPin2 will go high for a while, then low for a while. Any suggestions? Should I use an interrupt instead?

#include <elapsedMillis.h>

int ledPin2 = 2;      // LED connected to digital pin 2
int ledPin3 = 3;      // LED connected to digital pin 3
int ledPin4 = 4;      // LED connected to digital pin 4
int ledPin5 = 5;      // LED connected to digital pin 5
int ledPin6 = 6;      // LED connected to digital pin 6
int ledPin7 = 7;      // LED connected to digital pin 7
int ledPin8 = 8;      // LED connected to digital pin 8
int ledPin9 = 9;      // LED connected to digital pin 9
int ledPin10 = 10;      // LED connected to digital pin 10
int ledPin11 = 11;      // LED connected to digital pin 11
int ledPin12 = 12;      // LED connected to digital pin 12
int ledPin13 = 13;      // LED connected to digital pin 13

int analogPin = 0;   // potentiometer connected to analog pin 0
int analogPin2 = 1;   // potentiometer connected to analog pin 1
int analogPin3 = 2;   // potentiometer connected to analog pin 2
int analogPin4 = 3;   //to select # of sequencer steps
int analogPin5 = 4;   //for random generator
int analogPin6 = 5;   //read Bendo signal

int val = 0;         // variable to store the read value from analogPin 0
int val2 = 0;        // variable to store the read value from analogPin 1
int val3 = 0;        // variable to store the read value from analogPin 2
int val5 = 9;        //variable to read the read value from analogPin 4
int val6;            //variable to assign seqArray or random values to demux
int val7;            //variable to turn ledPin 11 on and off

int pulseGenState = 0;         // current state of ledPin10
int lastpulseGenState = 0;     // previous state of ledPin10
int button_count = 0;
int buttonState = 0; // variable to read switch to select sequencer or random for demux source
int buttonState2 = 0; // variable to read switch to select sq wave gen or bendo signal to trigger sequencer

int seqArray[10] = {2,3,4,5,6,7,8,9,10,11};
int val4 = seqArray[0];

int randNumber;

elapsedMillis timer0;
#define interval 2000

void setup()

{
  
  pinMode(ledPin2, OUTPUT);  // sets the pin as output
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, INPUT);      // LED connected to digital pin 8
  pinMode(ledPin10, INPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);
  randomSeed(analogRead(4));
  Serial.begin(9600);
  ledPin11 = HIGH;
  pulseGenState;
  timer0 = 0; // clear the timer at the end of startup
  
 }



void loop()

{
  
  {
    val = analogRead(0); //selects values 2-27
    val2 = analogRead(1); //controls frequency of sq wave gen
    val3 = analogRead(2); //selects elements in seqArray[]
    val = map(val, 100, 1000, 2, 27);
    val3 = map(val3, 200, 1000, 0, 9);
    val5 = analogRead(3); //selects elements in seqArray[]
    val5 = map(val5, 0, 1000, 1, 10);
    val7 = analogRead(5);
    
    if (timer0 > interval)  {
    timer0 -= interval; //reset the timer
    int pulseGenState = digitalRead(ledPin12);
    // read the current state and write the opposite
    digitalWrite(ledPin2, !pulseGenState);
  }
    
    
    {
  
  // print a random number from 2 to 279
  randNumber = random(2, 28);
  delay(0);

}
  
  
  if (val7 > 325) {     
    // turn LED on:    
    digitalWrite(ledPin12, HIGH);   //HIGH   bendo signal turns ledPIN 12 ON
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin12, LOW);    //LOW bendo signal turns ledPIN 12 ON
  }

I have an audio signal that turns ledPin12 on and off via analogRead. I want to be able to write ledPin2 high if ledPin12 has been low for 2 seconds, but once pin 12 goes high, I want pin 2 to go low IMMEDIATELY and reset the timer.

Then what is all the rest of that stuff for? Deal with ONE problem.

Since pin 12 is an OUTPUT, you control when it goes LOW and when it goes HIGH. So, setting pin 2 LOW immediately should not be a problem. Noting when pin 12 goes LOW is easy. Noting how long the pin has been LOW is trivial.

  ledPin11 = HIGH;

Why are you changing the value in this variable, a pin number, to 1?

  pulseGenState;

This might as well be

  47;
void loop()

{
  
  {

Maybe you need some more curly braces.

    val = analogRead(0); //selects values 2-27

How?

    val = map(val, 100, 1000, 2, 27);

This does. The other statement does not.

    int pulseGenState = digitalRead(ledPin12);

Having global and local variables of the same name is just plain stupid. I quit reading at this point.

I am VERY new to all of this. I am really trying hard to understand what is going on, and I have a created challenging project for myself. I have gotten EVERYTHING to work, with the exception of this last bit, and I've mostly figured it out on my own. I have put in countless hours trying to figure this out. If I'm stupid, that is NOT a character flaw, but I feel attacked on this forum. It is not helpful and I will seek help elsewhere. I have forgotten stray bits of code here and there that will need to be cleaned up, and I apologize for leaving them in there. I wanted to post just the parts of the code that are relevant, but people seem to want everything, so I post everything and get flamed for that. There has got to be a better place to get help, where people are friendly and willing to help, rather than needing to feel superior by beating up a newcomer.

everything that Paul has said, although a bit rough, is help. fix those errors, and then if you still have issues, come back.

also, if one is superior at something, they don't have to feel superior. They just are.

for instance

ledPin11 = HIGH;

should be

digitalWrite(ledPin11, HIGH);

::

in your setup function,

pulseGenState;

doesnt do anything useful, and I'm not sure what your intention was...

finally, having local and global variables using the same name causes a lot of problems, which is why it is stupid. It is stupid. Learn from it and try to stop doing stupid things.

THIS FORUM SUCKS. I have NEVER gotten useful help here. YOU DON'T HAVE TO BE RUDE TO BE HELPFUL. I am proud of what I've accomplished. This will be all from me..I'm done with this.

must be an only son.....

JB8256:
I am VERY new to all of this. I am really trying hard to understand what is going on, and I have a created challenging project for myself. I have gotten EVERYTHING to work, with the exception of this last bit, and I've mostly figured it out on my own. I have put in countless hours trying to figure this out. If I'm stupid, that is NOT a character flaw, but I feel attacked on this forum. It is not helpful and I will seek help elsewhere. I have forgotten stray bits of code here and there that will need to be cleaned up, and I apologize for leaving them in there. I wanted to post just the parts of the code that are relevant, but people seem to want everything, so I post everything and get flamed for that. There has got to be a better place to get help, where people are friendly and willing to help, rather than needing to feel superior by beating up a newcomer.

not true. there are a lot of folks here that are helping a lot of other folks.

your question, while somewhat straightforward simply doesn't jive with the complexity you have in your code... that's all.

If you are attempting to do other things in your code... in addition to those in your question, it would be helpful to mention that.