How to split a sketch based on switch high or low

Hi,

I am building a sketch to control the radar dish on a display aircraft. The dish is located in a housing that either raises the dish into the fuselage or lowers to working height. I have a manual control box that will control motors and direction but also want to use an automated sketch that will lower, rotate dish for a period of time, raise and repeat after a period of time. There is a switch on the manual control box for manual and auto. Question is how to I split the sketch so that on power on it does whatever the manual / auto switch reads. I have been looking at sub loops based on switchstate of manual / auto switch. Could someone point me in the right direction.

Fairey Gannet Radar Control

  Two motors. 
  Motor 1 12v Controls the rotation of the radar dish. Normal two wire motor.
  Motor 2 28v controls the raise / lower of the radar dish mechanism. Two poistive wires, one for up and one for down, single negative.

  Two limit switches. Limit switch Up, upper travel of radar dish mechanism. Limit switch Dwn, lower travel of radar dish mechanism.

  Caution: The radar dish will hit the ground on full travel. Travel to be restricted with lower travel limit switch.

  Two circuits. 
  1. Auto. From any position.
    Lowers mechanism until lower limit switch activated.
    Rotate the radar dish for xxx milliseconds.
    Raise mechanism until upper limit switch activated.
    Hold in upper position for xxx milliseconds, then repeat.
  2. Manual. From any position.
    Rocker switch on hand control box to activate manual or automatic mode. 
    Note. Raise and lower motor to be controlled at end stops by limit swicthes to prevent damage.
  Circuit selection from rocket switch high for auto and low for manual. To be selected before power up.
  Dish rotate motor and Arduino power supply from 12v dc, raise and lower motor power requirements from seperate transformer.

  
*/

// constants used to set pin numbers:
const int upperLimit = 3;    // upper limit switch assigned to pin 3, switch to ground pull up
const int lowerLimit = 4;    // lower limit switch assigned to pin 4, switch to ground pull up
const int modeSelect = 7;    // manual or auto mode rocker switch assigned to pin 7, switch to ground pull up
const int motorDishenR = 15; // dish rotate motor rotate cw enable pin 3. digital output
const int motorDishenL = 2;  // dish rotate motor rotate ccw enable pin 5. digital output
const int motorDishR = 9;    // dish rotate motor speed control. PWM output pin 9
const int motorDishL = 10;   // dish rotate motor speed control. PWM output pin 10
const int motorMechU = 5;    // dish mechanism raise. 3v Signal to SSR DC at pin 5
const int motorMechL = 6;    // dih mechanism lower. 3v Signla to SSR Dc at pin 6
const int uP = 7;            // manual button up
const int lwR = 11;          // manual button down
const int cW = 12;           // manual button clockwise
const int ccW = 13;          // manual button counter-clockwise

// variables will change:
int bsupplimit = 0;    // button state for upper limit switch
int bslowlimit = 1;    // button state for lower limit switch
int bsmodeselect = 0;  // button state for mode select switch

void setup() {
  // initialize following pins as input:
  pinMode(upperLimit, INPUT_PULLUP);
  pinMode(lowerLimit, INPUT_PULLUP);
  pinMode(modeSelect, INPUT_PULLUP);
  pinMode(uP, INPUT_PULLUP);
  pinMode(lwR, INPUT_PULLUP);
  pinMode(cW, INPUT_PULLUP);
  pinMode(ccW, INPUT_PULLUP);
  // initialize following pins as an output:
  pinMode(motorDishenR, OUTPUT);
  pinMode(motorDishenL, OUTPUT);
  pinMode(motorDishR, OUTPUT);
  pinMode(motorDishL, OUTPUT);
  pinMode(motorMechU, OUTPUT);
  pinMode(motorMechL, OUTPUT);

  
}

void loop() { //The following to run if modeSelect button is high
  int buttonState1 = digitalRead(uP);
  int buttonState2 = digitalRead(lwR);
  int buttonState3 = digitalRead(cW);
  int buttonState4 = digitalRead(ccW);
  int buttonState5 = digitalRead(upperLimit);
  int buttonState6 = digitalRead(lowerLimit);
  int buttonState7 = digitalRead(modeSelect);

  if (buttonState1 == HIGH and buttonState2 == LOW and buttonState3 == LOW and buttonState4 == LOW and buttonState5 == LOW and buttonState6 == LOW) {
    digitalWrite(motorMechU, HIGH);
  } else {
      digitalWrite(motorMechU, LOW); {
      }
  
  }

  if (buttonState2 == HIGH and buttonState1 == LOW and buttonState3 == LOW and buttonState4 == LOW and buttonState5 == LOW and buttonState6 == LOW) {
    digitalWrite(motorMechL, HIGH);
  } else {
      digitalWrite(motorMechL, LOW); {
      }
  
  }

if (buttonState3 == HIGH and buttonState1 == LOW and buttonState2 == LOW and buttonState4 == LOW and buttonState5 == LOW and buttonState6 == LOW) {
    digitalWrite(motorDishenR, HIGH);
    analogWrite(motorDishR, 200); //speed to be set on build
  } else {
      digitalWrite(motorDishenR, LOW); {
      }
  
  }

if (buttonState4 == HIGH and buttonState1 == LOW and buttonState2 == LOW and buttonState3 == LOW and buttonState5 == LOW and buttonState6 == LOW) {
    digitalWrite(motorDishenL, HIGH);
    analogWrite(motorDishL, 200); //speed to be set on build
  } else {
      digitalWrite(motorDishenL, LOW); {
      }
  
  }

if (buttonState5 == HIGH) {
    digitalWrite(motorMechU, LOW);   
  
  }

if (buttonState6 == HIGH) {
    digitalWrite(motorMechL, LOW);   

  }

 
  // The following to run if modeSelect button is low
 //int buttonState1 = digitalRead(uP);
 //int buttonState2 = digitalRead(lwR);
 if (buttonState1 == HIGH); {
  digitalWrite(motorMechU, LOW);

 }  
 if (buttonState2 == HIGH);{
    digitalWrite(motorMechL, LOW);
 }
    digitalWrite(motorMechL, HIGH);

    digitalWrite(motorDishenR, HIGH);
    analogWrite(motorDishR, 200); //speed to be set on build

    //dwell with Millis here while dish rotates

    digitalWrite(motorDishenR, LOW);

    digitalWrite(motorMechU, HIGH);

    //dwell with Millis here for a period before dish goes down, reads from line 129

    


  }

Start with putting a begin-comment at the top of your sketch...

/*

You also have pins defined with more than one purpose.

For your buttons, read about debouncing for reliable button reading.

https://docs.arduino.cc/built-in-examples/digital/Debounce/

This will use a timing routine... "start the timer... if the timer is at a value start an automation... reset the timer"

In "setup" also read these switches and run the necessary functions, then in "loop" continue with the main part of the sketch.

Here's a sketch you can check out that shows one way to switch between modes. It's just using the millis timer for its input, yours will be the manual switch or whatever input, to keep checking and going into a switch/case control arrangement.
I think of it like a video game: switch between case 0-4 (or whatever) to enter level 1, level 2, whatever you need.
Hopefully this helps and doesn't add confusion.

byte level; // you could have 0 - 255 levels if you wanted.
unsigned long currentTime;
unsigned long lastTimeAround;

void setup() {
  Serial.begin(115200);
  Serial.println(F("millisTimerLeveledEvents"));
  delay(500);
  Serial.println();
  level = 0; // start at level 0
  currentTime = 0;
  lastTimeAround = 0;
}

void loop() {
  // start free running timer
  currentTime = millis();
  // set conditional statement whose condition is to check clock and report every 5 seconds
  if (currentTime - lastTimeAround > 5000) {
    switch (level) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
    }
    // synch the time marker to the free running clock
    lastTimeAround = currentTime;
  }
}
void zero() {
  Serial.println("5 sec");
  level += 1; // increment level by one, to level 1
}
void one() {
  Serial.println("10 sec");
  level += 1; // exit to level 2
}
void two() {
  Serial.println("15 sec");
  level += 1;
}
void three() {
  Serial.println("20 sec");
  level += 1;
}
void four() {
  Serial.println("25 sec");
  level = 0; // reset game to level 0
}

looking at the pins, are there really separate pins to raise/lower and rotate cw/ccw as well as separate pins for speed, or are there pins the control the direction of motor

should there be a pin to en/disable a motor to raise/lower and a direction pin to set whether the motor raises or lowers.

similarly, there just needs to be a direction pin to rotate cw/ccw and a PWM pin that controls speed as well as stopping rotation

look this over
it changes the pins to address my previous post

/* Fairey Gannet Radar Control

Two motors.
Motor 1 12v Controls the rotation of the radar dish. Normal two wire motor.
Motor 2 28v controls the raise / lower of the radar dish mechanism. Two poistive wires, one for up and one for down, single negative.

Two limit switches. Limit switch Up, upper travel of radar dish mechanism. Limit switch Dwn, lower travel of radar dish mechanism.

Caution: The radar dish will hit the ground on full travel. Travel to be restricted with lower travel limit switch.

Two circuits.
    1. Auto. From any position.
        Lowers mechanism until lower limit switch activated.
        Rotate the radar dish for xxx milliseconds.
        Raise mechanism until upper limit switch activated.
        Hold in upper position for xxx milliseconds, then repeat.

    2. Manual. From any position.
        Rocker switch on hand control box to activate manual or automatic mode.
        Note. Raise and lower motor to be controlled at end stops by limit
        swicthes to prevent damage.  Circuit selection from rocket switch high
        for auto and low for manual. To be selected before power up.  Dish
        rotate motor and Arduino power supply from 12v dc, raise and lower
        motor power requirements from seperate transformer.

*/

// pin numbers:
const int PmotorEn      = 15;  // raise/lower motor enable
const int PmotorDir     = 2;   // raise/lower motor direction
const int PmotorSpd     = 10;  // dish rotate motor speed control

const int PupperLimit   = 3;   // upper limit switch
const int PlowerLimit   = 4;   // lower limit switch

const int PmanualMode   = 7;   // manual or auto mode rocker switch

const int Pup           = 6;   // manual button up
const int Pdown         = 11;  // manual button down
const int Pcw           = 12;  // manual button clockwise
const int Pstop         = 13;  // manual button stop rotation

enum { Sw_Off = LOW, Sw_On  = HIGH };
enum { M_Off  = LOW, M_On   = HIGH };
enum { D_Up   = LOW, D_Down = HIGH };
enum { R_Cw   = LOW, R_Ccw  = HIGH };

const int Spd = 200;

// -----------------------------------------------------------------------------
void
lower ()
{
    while (Sw_Off == digitalRead (PlowerLimit))  {
        digitalWrite (PmotorDir, D_Down);
        digitalWrite (PmotorEn,  M_On);
    }
    digitalWrite (PmotorEn , M_Off);
}

// -------------------------------------
void
raise ()
{
    while (Sw_Off == digitalRead (PupperLimit))  {
        digitalWrite (PmotorDir, D_Up);
        digitalWrite (PmotorEn,  M_On);
    }
    digitalWrite (PmotorEn , M_Off);
}

// -------------------------------------
void
rotate (
    int   spd )
{
    digitalWrite (PmotorSpd, spd);
}

// -----------------------------------------------------------------------------
void manMode ()
{
    if (Sw_On == digitalRead (Pdown))
        lower ();

    else if (Sw_On == digitalRead (Pcw))
        rotate (Spd);

    else if (Sw_On == digitalRead (Pstop))
        rotate (0);

    else if (Sw_On == digitalRead (Pup))
        raise ();
}

// -----------------------------------------------------------------------------
void autoMode ()
{
    lower ();
    rotate (Spd);
    delay (2000);

    rotate (0);
    raise ();
    delay (2000);
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (Sw_On == digitalRead (PmanualMode))
        manMode ();
    else
        autoMode ();
}


// -----------------------------------------------------------------------------
void setup ()
{
    pinMode (PupperLimit,   INPUT_PULLUP);
    pinMode (PlowerLimit,   INPUT_PULLUP);
    pinMode (PmanualMode,   INPUT_PULLUP);
    pinMode (Pup,           INPUT_PULLUP);
    pinMode (Pdown,         INPUT_PULLUP);
    pinMode (Pcw,           INPUT_PULLUP);
    pinMode (Pstop,         INPUT_PULLUP);

    pinMode (PmotorDir,     OUTPUT);
    pinMode (PmotorEn,      OUTPUT);
    pinMode (PmotorSpd,     OUTPUT);
}

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