Adding case to switch time

Hi everyone, I'm working on a project where i need to switch the delay with four variables with a pushbutton. (2ms,3ms,4ms,5ms)...can anyone guide how to do that? here is my code

int count = 0;
int newcount = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,INPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
}
void loop()
{    
  
  if(digitalRead(9)==HIGH)
  {
  newcount=count+1;
     if(newcount!=count)
       {
        Serial.println(newcount);
        switch (newcount)
           {
              case 1: onepole();
                      digitalWrite(10,HIGH);
                      digitalWrite(11,LOW);
                      digitalWrite(12,LOW);
                      break;               
              case 2: twopole();
                      digitalWrite(11,HIGH);
                      digitalWrite(10,LOW);
                      digitalWrite(12,LOW);
                      break;
              case 3: fourpole();
                      digitalWrite(12,HIGH);
                      digitalWrite(10,LOW);
                       digitalWrite(11,LOW);
                      break; 
              default: digitalWrite(10,LOW);
                       digitalWrite(11,LOW);
                       digitalWrite(12,LOW);
                       newcount=0;
                       break;                
            }
       count=newcount;
       }
  }
  delay(100);
}
void onepole()
{
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);

  if(digitalRead(2)==HIGH)
  {
    delayMicroseconds(2000);
    digitalWrite(3,HIGH);
    if(digitalRead(3)==HIGH)
      digitalWrite(7,HIGH);
    else
      digitalWrite(8,HIGH);
    delay(3000);
  }
  else
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
}
void twopole()
{
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  
  if(digitalRead(2)==HIGH)
  {
    delayMicroseconds(2000);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    if((digitalRead(3)==HIGH) && (digitalRead(4)==HIGH))
      digitalWrite(7,HIGH);
    else
      digitalWrite(8,HIGH);
    delay(3000);
  }
  else
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
}
void fourpole()
{
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  
  if(digitalRead(2)==HIGH)
  {
    delayMicroseconds(2000);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    if((digitalRead(3)==HIGH) && (digitalRead(4)==HIGH) && (digitalRead(5)==HIGH) && (digitalRead(6)==HIGH))
      digitalWrite(7,HIGH);
    else
      digitalWrite(8,HIGH);
    delay(3000);
  }
  else
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
}

Well done posting formatted code in code tags! But....
Read this link: Arduino Forum

Your code would be tremendously more human readable if you defined some constants as to what all the pins are. I have no idea what pin 2 is, but I would have a much better idea if you had

const int myButton = 2; // button to select delay time

and then used that constant in your code.

As it stands, I really can't follow it.

Always comment your lines of code.

Rather that using the port pin numbers, give them good names.

if(digitalRead(10) == HIGH)


#define PUSHED HIGH
const byte startSwitch = 10;
. . .

if(digitalRead(startSwitch) == PUSHED)


Which of the above is self documenting ?

The various cases all seem to have identical delays.  Where and how do the different timings come into play?

This is curious:

    digitalWrite(3, HIGH);
    if (digitalRead(3) == HIGH)
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);

You set (3) HIGH then test to see if it's HIGH.  Why would it not be?

i hope i have corrected it sir...i havent add anything lines for another push button(to change delay state)

int count = 0;
int newcount = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT); //signal from coil//
  pinMode(3, INPUT); //1pole contact//
  pinMode(4, INPUT); //2pole contact//
  pinMode(5, INPUT); //3pole contact//
  pinMode(6, INPUT); //4pole contact//
  pinMode(7, OUTPUT); //pass led//
  pinMode(8, OUTPUT); //fail led//
  pinMode(9, INPUT); //push button to change pole state//
  pinMode(10, OUTPUT); //1 pole led//
  pinMode(11, OUTPUT); //2pole led//
  pinMode(12, OUTPUT); //4pole led//
}
void loop()
{

  if (digitalRead(9) == HIGH)
  {
    newcount = count + 1;
    if (newcount != count)
    {
      Serial.println(newcount);
      switch (newcount)
      {
        case 1: onepole();
          digitalWrite(10, HIGH);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          break;
        case 2: twopole();
          digitalWrite(11, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(12, LOW);
          break;
        case 3: fourpole();
          digitalWrite(12, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          break;
        default: digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          newcount = 0;
          break;
      }
      count = newcount;
    }
  }
  delay(100);
}
void onepole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if (digitalRead(3) == HIGH)
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}
void twopole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if ((digitalRead(3) == HIGH) && (digitalRead(4) == HIGH))
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}
void fourpole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if ((digitalRead(3) == HIGH) && (digitalRead(4) == HIGH) && (digitalRead(5) == HIGH) && (digitalRead(6) == HIGH))
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}

its supposed to be input sir...i have corrected it..thanks

Hello
Take some time and describe the desired function of the sketch in simple words and this very simple.
Have a nice day and enjoy coding in C++.

i have tried some code but in not sure how to apply them

int count = 0;
int newcount = 0;
int timecount = 0;
int newtimecount = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT); //signal from coil//
  pinMode(3, INPUT); //1pole contact//
  pinMode(4, INPUT); //2pole contact//
  pinMode(5, INPUT); //3pole contact//
  pinMode(6, INPUT); //4pole contact//
  pinMode(7, OUTPUT); //pass led//
  pinMode(8, OUTPUT); //fail led//
  pinMode(9, INPUT); //push button to change pole state//
  pinMode(10, OUTPUT); //1 pole LED//
  pinMode(11, OUTPUT); //2pole LED//
  pinMode(12, OUTPUT); //4pole LED//
  pinMode(13, INPUT); //pushbutton to change delay state//
  pinMode(14, OUTPUT); //2ms LED//
  pinMode(15, OUTPUT); //3ms LED//
  pinMode(16, OUTPUT); //4ms LED//
  pinMode(17, OUTPUT); //5ms LED//
}
void loop()
{

  if (digitalRead(9) == HIGH)
  {
    newcount = count + 1;
    if (newcount != count)
    {
      Serial.println(newcount);
      switch (newcount)
      {
        case 1: onepole();
          digitalWrite(10, HIGH);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          break;
        case 2: twopole();
          digitalWrite(11, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(12, LOW);
          break;
        case 3: fourpole();
          digitalWrite(12, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          break;
        default: digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          newcount = 0;
          break;
      }
      count = newcount;
    }
  }
  delay(100);
}
void ms()
{

  if (digitalRead(13) == HIGH)
  {
    newtimecount = timecount + 1;
    if (newtimecount != timecount)
    {
      Serial.println(newtimecount);
      switch (newtimecount)
      {
        case 1: delayMicroseconds(2000);
          digitalWrite(14, HIGH);
          digitalWrite(15, LOW);
          digitalWrite(16, LOW);
          digitalWrite(17, LOW);
          break;
        case 2: delayMicroseconds(3000);
          digitalWrite(14, LOW);
          digitalWrite(15, HIGH);
          digitalWrite(16, LOW);
          digitalWrite(17, LOW);
          break;
        case 3: delayMicroseconds(4000);
          digitalWrite(14, LOW);
          digitalWrite(15, LOW);
          digitalWrite(16, HIGH);
          digitalWrite(17, LOW);
          break;
        case 4: delayMicroseconds(5000);
          digitalWrite(14, LOW);
          digitalWrite(15, LOW);
          digitalWrite(16, LOW);
          digitalWrite(17, HIGH);
          break;
        default: digitalWrite(14, LOW);
          digitalWrite(15, LOW);
          digitalWrite(16, LOW);
          digitalWrite(17, LOW);
          newtimecount = 0;
          break;
      }
      timecount = newtimecount;
    }
  }
  delay(100);
}
void onepole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if (digitalRead(3) == HIGH)
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}
void twopole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if ((digitalRead(3) == HIGH) && (digitalRead(4) == HIGH))
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}
void fourpole()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);

  if (digitalRead(2) == HIGH)
  {
    delayMicroseconds(2000);

    if ((digitalRead(3) == HIGH) && (digitalRead(4) == HIGH) && (digitalRead(5) == HIGH) && (digitalRead(6) == HIGH))
      digitalWrite(7, HIGH);
    else
      digitalWrite(8, HIGH);
    delay(3000);
  }
  else
    digitalWrite(7, LOW);
  digitalWrite(8, LOW);
}

"Sir" is unnecessary, we are all good friends here, and some are ladies. :sunglasses:

Mostly. :pleading_face:

Can you image some logic where this if() statement would ever be false? I cannot. You take a variable, add 1 to it and then see if it is different from the original value ?!?!?!?

Something like this should get you close..

Question: How are your buttons wired up? Your code requires those buttons to have either a pullup or pulldown resistor installed. Are they installed? If not, the more common way to wire a button is one side to ground, the other to the arduino pin and then declare the pin as INPUT_PULLUP. It will read HIGH when not pressed and LOW when pressed.

Also, what arduino are you using?

int poleState = 0;
int delayState = 0;
unsigned long delayTime = 1000;

const byte pinCoil = 2;
const byte pinPole1 = 3;
const byte pinPole2 = 4;
const byte pinPole3 = 5;
const byte pinPole4 = 6;
const byte pinLEDPass = 7;
const byte pinLEDFail = 8;
const byte pinPoleButton = 9;
const byte pinLEDPole1 = 10;
const byte pinLEDPole2 = 11;
const byte pinLEDPole4 = 12;
const byte pinDelayButton = 13;
const byte pinDelay2ms = 14;
const byte pinDelay3ms = 15;
const byte pinDelay4ms = 16;
const byte pinDelay5ms = 17;

void setup()
{
  Serial.begin(115200);
  pinMode(pinCoil, INPUT); //signal from coil//
  pinMode(pinPole1, INPUT); //1pole contact//
  pinMode(pinPole2, INPUT); //2pole contact//
  pinMode(pinPole3, INPUT); //3pole contact//
  pinMode(pinPole4, INPUT); //4pole contact//
  pinMode(pinLEDPass, OUTPUT); //pass led//
  pinMode(pinLEDFail, OUTPUT); //fail led//
  pinMode(pinPoleButton, INPUT); //push button to change pole state//
  pinMode(pinLEDPole1, OUTPUT); //1 pole LED//
  pinMode(pinLEDPole2, OUTPUT); //2pole LED//
  pinMode(pinLEDPole4, OUTPUT); //4pole LED//
  pinMode(pinDelayButton, INPUT); //pushbutton to change delay state//
  pinMode(pinDelay2ms, OUTPUT); //2ms LED//
  pinMode(pinDelay3ms, OUTPUT); //3ms LED//
  pinMode(pinDelay4ms, OUTPUT); //4ms LED//
  pinMode(pinDelay5ms, OUTPUT); //5ms LED//
}


void loop()
{
  checkPole();
  checkDelay();
}

void checkPole()
{
  if (digitalRead(pinPoleButton) == HIGH)
  {
    poleState++;
    Serial.println(poleState);
    switch (poleState)
    {
      case 1:
        onepole();
        digitalWrite(pinLEDPole1, HIGH);
        digitalWrite(pinLEDPole2, LOW);
        digitalWrite(pinLEDPole4, LOW);
        break;
      case 2:
        twopole();
        digitalWrite(pinLEDPole1, LOW);
        digitalWrite(pinLEDPole2, HIGH);
        digitalWrite(pinLEDPole4, LOW);
        break;
      case 3:
        fourpole();
        digitalWrite(pinLEDPole1, LOW);
        digitalWrite(pinLEDPole2, LOW);
        digitalWrite(pinLEDPole4, HIGH);
        break;
      default:
        digitalWrite(pinLEDPole1, LOW);
        digitalWrite(pinLEDPole2, LOW);
        digitalWrite(pinLEDPole4, LOW);
        poleState = 0;
        break;
    }
    delay(100);
  }
}

void checkDelay()
{
  if (digitalRead(pinDelayButton) == HIGH)
  {
    delayState++;
    Serial.println(delayState);
    switch (delayState)
    {
      case 1:
        //delayMicroseconds(2000);
        delayTime = 2000;
        digitalWrite(pinDelay2ms, HIGH);
        digitalWrite(pinDelay3ms, LOW);
        digitalWrite(pinDelay4ms, LOW);
        digitalWrite(pinDelay5ms, LOW);
        break;
      case 2:
        //delayMicroseconds(3000);
        delayTime = 3000;
        digitalWrite(pinDelay2ms, LOW);
        digitalWrite(pinDelay3ms, HIGH);
        digitalWrite(pinDelay4ms, LOW);
        digitalWrite(pinDelay5ms, LOW);
        break;
      case 3:
        //delayMicroseconds(4000);
        delayTime = 4000;
        digitalWrite(pinDelay2ms, LOW);
        digitalWrite(pinDelay3ms, LOW);
        digitalWrite(pinDelay4ms, HIGH);
        digitalWrite(pinDelay5ms, LOW);
        break;
      case 4:
        //delayMicroseconds(5000);
        delayTime = 5000;
        digitalWrite(pinDelay2ms, LOW);
        digitalWrite(pinDelay3ms, LOW);
        digitalWrite(pinDelay4ms, LOW);
        digitalWrite(pinDelay5ms, HIGH);
        break;
      default:
        digitalWrite(pinDelay2ms, LOW);
        digitalWrite(pinDelay3ms, LOW);
        digitalWrite(pinDelay4ms, LOW);
        digitalWrite(pinDelay5ms, LOW);
        delayState = 0;
        delayTime = 1000;
        break;
    }
  }
  delay(100);
}

void onepole()
{
  digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);

  if (digitalRead(pinCoil) == HIGH)
  {
    delayMicroseconds(delayTime);

    if (digitalRead(pinPole1) == HIGH)
      digitalWrite(pinLEDPass, HIGH);
    else
      digitalWrite(pinLEDFail, HIGH);
    delay(3000);
  }
  else
    digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);
}

void twopole()
{
  digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);

  if (digitalRead(pinCoil) == HIGH)
  {
    delayMicroseconds(delayTime);

    if ((digitalRead(pinPole1) == HIGH) && (digitalRead(pinPole2) == HIGH))
      digitalWrite(pinLEDPass, HIGH);
    else
      digitalWrite(pinLEDFail, HIGH);
    delay(3000);
  }
  else
    digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);
}

void fourpole()
{
  digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);

  if (digitalRead(pinCoil) == HIGH)
  {
    delayMicroseconds(delayTime);

    if ((digitalRead(pinPole1) == HIGH) && (digitalRead(pinPole2) == HIGH) && (digitalRead(pinPole3) == HIGH) && (digitalRead(pinPole4) == HIGH))
      digitalWrite(pinLEDPass, HIGH);
    else
      digitalWrite(pinLEDFail, HIGH);
    delay(3000);
  }
  else
    digitalWrite(pinLEDPass, LOW);
  digitalWrite(pinLEDFail, LOW);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.