counter part 2

@larryd

thanks again for helping. I am having some issues. I went through the code and found a "(" missing and I had to go through and delete a bunch of spaces so that every thing that should be highlighted would be....... pinMode, digitalWrites, if, case, and so on.

I tried to load the code so I could test it and I'm getting several error codes.
pretty much a bunch of \stray 302 and \stray 240

sorry its been a while...I've been busy with work

Sounds like you didn't copy things properly.

In the code window in post #14, click 'select' then to copy the selected selection.
Then paste what you copied into a blank IDE sketch window.

.

I did that to the T. did it twice and still the same result.
I've had issues in the past with trying to copy code like that for some reason.

Attach the code you copied to your next post so we can see what the problem is.

Always show us your current compete sketch.
Use CTRL T to format the sketch.

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

I changed pin location from the original because i'm using a ProMini at the moment. I also removed the green and red leds from my project

#define Pressed HIGH
#define Released LOW

#define PinOn    HIGH
#define PinOff   LOW

// outputs
const byte relayPin = 12;
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 = 2; // on/off switch A value of 1 when high
const byte switchBpin = 3; // on/off switch B value of 2 when high
const byte switchCpin = 4; // on/off switch C value of 4 when high
const byte switchDpin = 5; // 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 = 9;  // 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
//======================================================================

a quick schematic if anyone wants to see it.

#define Pressed HIGH
#define Released LOW

//#define Pressed LOW  //debug
//#define Released LOW //debug

#define PinOn    HIGH
#define PinOff   LOW

// outputs
const byte relayPin = 12;
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 = 2; // on/off switch A value of 1 when high
const byte switchBpin = 3; // on/off switch B value of 2 when high
const byte switchCpin = 4; // on/off switch C value of 4 when high
const byte switchDpin = 5; // 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 = 9;  // 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);      //this had to be commented
  //digitalWrite(GledPin,PinOff);  //this had to be commented

  //pinMode(RledPin, OUTPUT);      //this had to be commented
  //digitalWrite(RledPin, PinOff); //this had to be commented

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

          //delayValueH   = 500; //debug
          //delayValueL   = 200; //debug

          Count = (digitalRead(switchDpin) * 8) +
                  (digitalRead(switchCpin) * 4) +
                  (digitalRead(switchBpin) * 2) +
                  (digitalRead(switchApin)); //corrected line
          //(digitalRead(switchApin);  <----<<<< you erased a ) therfore had an error

          //Count = 5; //debug

          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
//======================================================================

You need pull down resistors.

.

Note:
If the relay coil pulls more than 100ma, use an external power supply to power it.

.

ok cool thanks. I'm still having copy issues with the code. I'm gonna just split screen and type it out.
i'll get back to you when i'm done. again, thank you.

Okay, I attached the file with the sketch inside.

sketch_aug27a.ino (4.36 KB)

nice! works pretty good. seems like when the switches are set to zero (all in the off position) it continuously digitalWrites with what ever pulse rate is set to.

I guess I would add

if (digitalRead(switchApin)==LOW) && (digitalRead(switchBpin)==LOW) && (digitalRead(switchCpin)==LOW) && (digitalRead(switchDpin)==LOW) && (digitalRead(sensorPin)==LOW)
{
  digitalWrite(relayPin,LOW);
}

yes???

Try this addition to 'case startState'

    case startState:
      {
        if (digitalRead(sensorPin) == Pressed)
        {
          delayValueH   = analogRead(onTimePin);
          delayValueL   = analogRead(offTimePin);

          //delayValueH   = 500; //debug
          //delayValueL   = 200; //debug

          Count = (digitalRead(switchDpin) * 8) +
                  (digitalRead(switchCpin) * 4) +
                  (digitalRead(switchBpin) * 2) +
                  (digitalRead(switchApin)); //corrected line
          //(digitalRead(switchApin);  <----<<<< you erased a ) therfore had an error

          //Count = 5; //debug

          //ignore a value of zero
          if(Count == 0)
          {
            break;
          }
                  
          actionDelay   = delayValueL;
          actionMillis  = millis();
          digitalWrite(relayPin, PinOff);
          actionCounter = Count;
          ActionState   = lowState;
        }
      }
      break;

I added two "if statements" I was having issues with switching the sensor pin on and off with the other switches set to zero.

seems to work alright.
i'm using a LED right now for the relay. I'm getting a really small and fast flash on the LED.
and when I switch a counter switch on (no matter which one) then it will continuously write the relayPin HIGH.... I have to press the reset button to get it to work right.

#define Pressed HIGH
#define Released LOW

//#define Pressed LOW   //debug
//#define Released HIGH //debug

#define PinOn    HIGH
#define PinOff   LOW

// outputs
const byte relayPin = 12;
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 = 2; // on/off switch A value of 1 when high
const byte switchBpin = 3; // on/off switch B value of 2 when high
const byte switchCpin = 4; // on/off switch C value of 4 when high
const byte switchDpin = 5; // 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 = 9;  // 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);      //this had to be commented
  //digitalWrite(GledPin,PinOff);  //this had to be commented

  //pinMode(RledPin, OUTPUT);      //this had to be commented
  //digitalWrite(RledPin, PinOff); //this had to be commented

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

if ((digitalRead(switchApin)==LOW) && (digitalRead(switchBpin)==LOW) && (digitalRead(switchCpin)==LOW) && (digitalRead(switchDpin)==LOW) && (digitalRead(sensorPin)==HIGH))
{
  digitalWrite(relayPin,LOW);
}

if ((digitalRead(switchApin)==LOW) && (digitalRead(switchBpin)==LOW) && (digitalRead(switchCpin)==LOW) && (digitalRead(switchDpin)==LOW) && (digitalRead(sensorPin)==LOW))
{
  digitalWrite(relayPin,LOW);
}


  //***************************
  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);

          //delayValueH   = 500; //debug
          //delayValueL   = 200; //debug

          Count = (digitalRead(switchDpin) * 8) +
                  (digitalRead(switchCpin) * 4) +
                  (digitalRead(switchBpin) * 2) +
                  (digitalRead(switchApin)); //corrected line
          //(digitalRead(switchApin);  <----<<<< you erased a ) therfore had an error

          //Count = 5; //debug

          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
//======================================================================

that's what I tried before you responded with you last post

Do some debugging try removing the comment marks as shown here, see lines with <----<<<<
You will get 500 and 200 for the delays.
Note:
If the Pots are turned down too much, the flashing will be very fast.
To compensate you would have to add two bottom resistors to the pots as lower end limits.

    //***************************
    case startState:
      {
        if (digitalRead(sensorPin) == Pressed)
        {
          delayValueH   = analogRead(onTimePin);
          delayValueL   = analogRead(offTimePin);

          delayValueH   = 500; //debug    <----<<<< uncomment this line to see what happens
          delayValueL   = 200; //debug    <----<<<< it takes the pots out of the equation

          Count = (digitalRead(switchDpin) * 8) +
                  (digitalRead(switchCpin) * 4) +
                  (digitalRead(switchBpin) * 2) +
                  (digitalRead(switchApin)); //corrected line

          //Count = 5; //debug  

          //ignore a value of zero
          if(Count == 0)
          {
            break;
          }
                  
          actionDelay   = delayValueL;
          actionMillis  = millis();
          digitalWrite(relayPin, PinOff);
          actionCounter = Count;
          ActionState   = lowState;
        }
      }
      break;

Sorry, I had two lines of code in the above sketch segment that should not have been there.
Should work okay now.

.

ill try what you posted, but the flash isn't due to being turned down too much. I have both pots set to almost half.

when there is a count setting and sensorPin is in a HIGH state then the LED works normally (good solid light with proper delay times for both on and off)

the flash i'm referring to is like half a second, if that.
it comes on fast and dim then its off for the set off delay (set by the pot) but when the "pulse on" would normally occur under normal conditions then the issue occurs. (led fast dim flash)