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
}