Controlling Different Output Combinations by Pressing Particular Button.

Dear All,
I am working on a project to control several stepper motors and 4 relays in certain combinations on press of a switch, program for same is attached in text file.

As you can see i have several modes depending upon switch pressed and depending upon modes i'm enabling certain output.

Now i want if someone accidentally press more than one switch, it either stop all the program or just keep running the mode that was first pressed.

Also i want that after one On and OFF cycle of switch all switch become disable for at least 20 sec. and after that only one can again able to give particular mode from the switch.

please help to achieve above tasks.

Program.txt (12.2 KB)

When you find yourself naming variables like this

const int stepPin3=22;
const int dirPin3=23;


const int stepPin4=24;
const int dirPin4=25;

const int stepPin5=26;

it is time to learn about arrays. It will allow you to make the program very much shorter - and less prone to typing errors.

I presume this

if someone accidentally press more than one switch

means that only one of Mode1 ... Mode8 should be HIGH at any one time.

If that's correct then you should check for that before you start checking for any of the individual modes. If you had the modes in an array it would be as simple as this

byte modeCount = 0
for (byte n = 1; n <= 8; n++) {
  if (mode[n] == HIGH) {
    modeCount += 1;
  }
}
if (modeCount > 1) {
   // stop everything
}

Is it achievable using Switch statement if so how? and how i give some delay on one ON OFF cycle completion.

m7nab:
Is it achievable using Switch statement if so how? and how i give some delay on one ON OFF cycle completion.

You have not commented on my Reply #1 - have I interpreted the requirement correctly? Do you have an objection to the solution I suggested?

I can't see how a Switch/Case statement would be useful

...R

Yes you have interpreted the requirement correctly, however i tried using modecount in my existing code (without using naming variable in array) and disable all output when modecount>1 but when i test the code in proteus it doesn't work as required. It keep changing between modes if two modes switch is pressed.
Code for same is attached.

Program(2).txt (13.4 KB)

m7nab:
Yes you have interpreted the requirement correctly, however i tried using modecount in my existing code

But you did not do it where I told you it needs to be done.

You have to calculate ModeCount BEFORE you start any of the if(ModeX== HIGH) tests

...R

PS ... if you need more help please make the program short enough (but still complete and compilable) so you can include it in your Post using the Code button </> I don't want to have to download more versions cluttering up my hard disc.

I have reduced the code size, also tried keeping ModeCount before reading mode but no help actually when i tried this code modes keep switching between them randomly if two mode switch is pressed.

#define F_CPU 1600000
bool Mode1 = 0;
bool Mode2 = 0;
bool Mode3 = 0;

//......Defining Step Pin and Direction Pin for Stepper.....//


const int stepPin1 = 0;
const int dirPin1 = 1;

const int stepPin2 = 2;
const int dirPin2 = 3;

const int stepPin3 = 22;
const int dirPin3 = 23;

int ModeCount = 0;

void setup() {
  // put your setup code here, to run once:

  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
 

  pinMode(14, INPUT);
  pinMode(15, INPUT);
  pinMode(16, INPUT);
  

  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(stepPin3, OUTPUT);
  pinMode(dirPin3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  Mode1 = digitalRead(14);
  Mode2 = digitalRead(15);
  Mode3 = digitalRead(16);


  if (ModeCount > 1)
  {
    digitalWrite(stepPin1,  LOW);
    digitalWrite(stepPin2,  LOW);
    digitalWrite(stepPin3,  LOW);
    digitalWrite(dirPin1,  LOW);
    digitalWrite(dirPin2,  LOW);
    digitalWrite(dirPin3,  LOW);
    digitalWrite(8,  LOW);
    digitalWrite(9,  LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  //.......lOGIC FOR MODE-1........//

  if (Mode1 == HIGH)
  {
    ModeCount++;
    digitalWrite(8, HIGH);
    digitalWrite(dirPin1, LOW);
    digitalWrite(dirPin2, HIGH);

    for (int x = 0; x < 300; x++)
    {
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(2000);
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(2000);
    }

  }
  
  //.......lOGIC FOR MODE-2........//

  if (Mode2 == HIGH)
  {
    ModeCount++;
    digitalWrite(9, HIGH);
    digitalWrite(dirPin1, LOW);
    digitalWrite(dirPin3, HIGH);

    for (int x = 0; x < 200; x++)
    {
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(2000);
      digitalWrite(stepPin3, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin3, LOW);
      delayMicroseconds(2000);
    }

  }
  
  //.......lOGIC FOR MODE-3........//
if (Mode3 == HIGH)
  {
    ModeCount++;
    digitalWrite(10, HIGH);
    digitalWrite(dirPin2, LOW);
    digitalWrite(dirPin3, HIGH);

    for (int x = 0; x < 200; x++)
    {
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(2000);
      digitalWrite(stepPin3, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin3, LOW);
      delayMicroseconds(2000);
    }

  }

}

With this code

void loop() {
  // put your main code here, to run repeatedly:

  Mode1 = digitalRead(14);
  Mode2 = digitalRead(15);
  Mode3 = digitalRead(16);


  if (ModeCount > 1)

how can ModeCount be higher than 0. You have not added the values from the modes together

You need something like this

void loop() {
  // put your main code here, to run repeatedly:

  Mode1 = digitalRead(14);
  Mode2 = digitalRead(15);
  Mode3 = digitalRead(16);


 ModeCount = Mode1 + Mode2 + Mode3;

  if (ModeCount > 1)

Successful programming requires thinking before typing :slight_smile:

...R