counter part 2

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..

int Count = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 4) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);


int Action =  digitalWrite(relayPin,HIGH); delay(delayValueH * 10); digitalWrite(relayPin,LOW); delay(delayValueL * 10);

something like this... I don't really know how to set it up...

if sensor is HIGH, then do Action * Count;

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.

.

int Count = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 4) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);

I'd just read the four pins into a byte to get you numbers from 0 to 16:

byte switchpin[] = {4,5,6,7};

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

void loop()
{
  byte value = 0;
  for (int i = 0; i < 4; i++)
  {
     value |= (digitalRead(switchpin[i]) << i);
  }
  doAction(value);
}

void doAction(int repeatQty)
{
  // do action stuff here
}

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

BulldogLowell:

int Count = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 4) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);

I'd just read the four pins into a byte to get you numbers from 0 to 16:

byte switchpin[] = {4,5,6,7};

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

void loop()
{
  byte value = 0;
  for (int i = 0; i < 4; i++)
  {
    value |= (digitalRead(switchpin[i]) << i);
  }
  doAction(value);
}

void doAction(int repeatQty)
{
  // do action stuff here
}

its 0 to 15 because zero is a setting... 0 to 15 makes 16 settings

Not saying you shouldn't have a delay, just don't use delay()

See:

LandonW:
its 0 to 15 because zero is a setting... 0 to 15 makes 16 settings

right!

something like this:

byte switchpin[] = {4,5,6,7};

const byte relayPin = 3;

const byte potA = A0;
const byte potB = A1;

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

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);
  }
}

BulldogLowell:
something like this:

byte switchpin[] = {4,5,6,7};

const byte relayPin = 3;

const byte potA = A0;
const byte potB = A1;

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

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);
  }
}

int delayA = digitalRead(potA); ??
analogRead()

larryd:
int delayA = digitalRead(potA); ??
analogRead()

an imperfect headstart

BulldogLowell:
an imperfect headstart

It's the weekend.

.

this is what I have so far.

 // outputs
const byte relayPin = 2;
const byte GledPin = 4; 
const byte RledPin = 6;
//  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
long delayValueH; // delay high value
long delayValueL; // delay low value
// input sensors
const byte sensorPin = 7; // on/off sensor 

void setup() {
pinMode(relayPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(RledPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(switchApin, INPUT);
pinMode(switchBpin, INPUT);
pinMode(switchCpin, INPUT);
pinMode(switchDpin, INPUT);
}

void loop() {
delayValueH = analogRead(onTimePin);
delayValueL = analogRead(offTimePin);

int Count = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 4) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);
}

What does this mean?
"if sensor is HIGH . . ."

.

the sensor is producing a signal. (the controller is getting a signal from sensor) HIGH

the sensor is either on or off, its essentially a switch

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'.

LandonW:
the sensor is producing a signal. (the controller is getting a signal from sensor) HIGH

the sensor is either on or off, its essentially a switch

Test question #1

@LandonW
With the code in post #14.
What happens if the binary number read form the switches was zero.
How would you fix this?

.

Obvious the OP was here for code and nothing else, oh well.

One more person not to help again, list is getting big.

.

Sorry, haven't been on I am while, I don't get on every day. If the switches were to be set to zero then don't do anything. Run the pulse 0 times

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

Again thank you very much

Have you tried the sketch I posted #14?
It should be very close.

It has no accommodation for a number of 0.

Here is a test for you:
Tell us what you would add or change so a value of zero is handled properly (as you want).

.