ok, this is an extension question to an earlier post... basically I want to make a list of commands and label it "action" then I want it to be executed X amount of times depending on the "count" value..
larryd:
Make a function called Action.
Call this function Count number of times when 'sensor' is HIGH.
BUT, first get rid of all those delay()s as they freeze your code during the delay times.
Use the millis() technique found in the BWD (Blink Without Delay) example in the IDE.
.
if the sensor is high once then I need the relay to act as needed. the delay is to control time ON and time OFF..
both are adjustable with pots.
so if the sensor ever becomes high then I can potentially set the relay to switch from high to low 15 times with up to 10 seconds for each delay. the delay needs to be there. its not going to "slow down my project enough to matter... and when the relay gets energized there is no way for sensor to become HIGH at the same time
void loop()
{
int delayA = digitalRead(potA);
int delayB = digitalRead(potB);
byte value = 0;
for (int i = 0; i < 4; i++)
{
value |= (digitalRead(switchpin[i]) << i);
}
//if(sensor==HIGH) //<<<<<<<<<<<< what sensor?
doAction(value, delayA, delayB);
}
void doAction(int repeatQty, int delayOn, int delayOff)
{
for (int i = 0; i < repeatQty; i++)
{
digitalWrite(relayPin, HIGH);
delay(delayOn);
digitalWrite(relayPin, LOW);
delay(delayOff);
}
}
This should be very close, I don't have a Mega to test it.
There will be a test.
#define Pressed HIGH
#define Released LOW
#define PinOn HIGH
#define PinOff LOW
// outputs
const byte relayPin = 2;
const byte GledPin = 4;
const byte RledPin = 6;
const byte heartBeatLED = 13;
// pulse settings
const byte onTimePin = A0; // pot to determine digitalWrite HIGH
const byte offTimePin = A2; // pot to determin digitalWrite LOW
const byte switchApin = 22; // on/off switch A value of 1 when high
const byte switchBpin = 26; // on/off switch B value of 2 when high
const byte switchCpin = 30; // on/off switch C value of 4 when high
const byte switchDpin = 34; // on/off switch D value of 8 when high
unsigned long delayValueH; // delay high value
unsigned long delayValueL; // delay low value
unsigned long heartBeatMillis;
unsigned long actionMillis;
unsigned long actionDelay;
byte Count;
byte actionCounter;
// input sensors
const byte sensorPin = 7; // on/off sensor
enum STATE {startState, lowState, highState};
STATE ActionState = startState;
// s e t u p ( )
//**********************************************************************
void setup()
{
pinMode(heartBeatLED, OUTPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, PinOff);
pinMode(GledPin, OUTPUT);
digitalWrite(GledPin, PinOff);
pinMode(RledPin, OUTPUT);
digitalWrite(RledPin, PinOff);
pinMode(sensorPin, INPUT_PULLUP);
pinMode(switchApin, INPUT_PULLUP);
pinMode(switchBpin, INPUT_PULLUP);
pinMode(switchCpin, INPUT_PULLUP);
pinMode(switchDpin, INPUT_PULLUP);
} //END of s e t u p ( )
// l o o p ( )
//**********************************************************************
void loop()
{
//***************************
//HeartBeat LED, should toggle every 200ms 'if the code is nonblocking'
if (millis() - heartBeatMillis >= 200)
{
heartBeatMillis = millis(); //reset timing
//Toggle heartBeatLED
digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
}
//***************************
toggleAction();
} //END of l o o p ( )
//======================================================================
// F U N C T I O N S
//======================================================================
// t o g g l e A c t i o n ( )
//**********************************************************************
void toggleAction()
{
switch (ActionState)
{
//***************************
case startState:
{
if (digitalRead(sensorPin) == Pressed)
{
delayValueH = analogRead(onTimePin);
delayValueL = analogRead(offTimePin);
Count = (digitalRead(switchDpin) * 8) +
(digitalRead(switchCpin) * 4) +
(digitalRead(switchBpin) * 2) +
digitalRead(switchApin);
actionDelay = delayValueL;
actionMillis = millis();
digitalWrite(relayPin, PinOff);
actionCounter = Count;
ActionState = lowState;
}
}
break;
//***************************
case lowState:
{
if (millis() - actionMillis < actionDelay)
{
break;
}
digitalWrite(relayPin, PinOn);
actionDelay = delayValueH;
actionMillis = millis();
ActionState = highState;
}
break;
//***************************
case highState:
{
if (millis() - actionMillis < actionDelay)
{
break;
}
digitalWrite(relayPin, PinOff);
actionCounter--;
if (actionCounter == 0)
{
ActionState = startState;
break;
}
actionDelay = delayValueL;
actionMillis = millis();
ActionState = lowState;
}
break;
} //End of switch/case
}//End of t o g g l e A c t i o n ( )
//======================================================================
// E N D O F C O D E
//======================================================================
Edit
sensorPin should be handled as 'change in state code'.
Sorry, misread. I realized you were asking about the "there will be a test" I haven't got done building it yet...still waiting on some pots to come in. I appreciate that you took the time to quiz me and help me learn. I will get back to you with what I got ASAP