Two input signals, on/off and mode change programming

Hi,

I am trying to make a system where with two input signals, could be two pushbuttons or two keys in a 4x4 keypad or any other, I have one for moving three servos from 0 to 180 (as an open/close button), and a mode change (depending on the times pressed) signal so I decide depending on the mode, which servo moves and which one doesn't. Example:

mode 1:

  • servo1 moves with switch button
  • servo2 moves with switch button
  • servo3 moves with switch button

mode 2:

  • servo1 stays in same position no matter the switch
  • servo2 moves with switch button
  • servo3 moves with switch button

mode 3:

  • servo1 moves with switch button
  • servo2 moves with switch button
  • servo3 stays in same position no matter the switch

I am really not good for coding, so even though I think it's a relatively simple system, I've been having some issues with the programming side of it.

I would appreciate any help, Thanks!

Rewrite those conditions per servo.

For instance servo1 moves with the switch if the mode is not 2.

Consider the following psuedo code:

mode = 0
loop()
   read mode_input
   read control_input
   if mode_input==valid
      mode=mode+1
      if mode==4
         mode=1

   if mode==1
      move servo_1
      move servo_2
      move servo_3
   if mode==2
      move servo_2
      move servo_3
   if mode==3
      move servo_1
      move servo_2

Thanks! So I tried to work with the code and this is what I came up with (still not working though), any comments?

** P.S. I am currently working on a virtual simulator by autodesk called Tinkercad, with:

  • Arduino
  • 4x4 Keypad
  • 3 servos
  • LCD screen

Code:

#include <Keypad.h>

#include <LiquidCrystal.h>

#include <Servo.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

//LCD digits for state report
long D1 = 1;
long D2 = 1;

//switch on/off botón 1
long time = 0;
long debounce = 200;
int state = 1;
int reading;
int previous = 0;

//Mode
int mode = 0;
int mode_input;
int control_input;
int reading2;

//servos
int pos = 0;
Servo servo_T; // Servo 1
Servo servo_Ind; // Servo 2
Servo servo_Pu; // Servo 3

//keypad
char customKey;
const byte ROWS = 4;
const byte COLS = 4;

// Key 1 used for on/off switch as a value 1
// Key 2 used for mode change as a value 2

char keys[ROWS][COLS] = {
{1,2,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {

//define pin for servo
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A3, OUTPUT);

//conect servo
servo_T.attach(A5);
servo_Ind.attach(A4);
servo_Pu.attach(A3);

// start lcd for state report
// first digit (D1) will show open/close (1/0)
// second digit (D2) will show mode (1/2/3)
lcd.begin(16, 2);
for(int i=0;i<=3;i++);
lcd.setCursor(0,0);
lcd.print(“V1.0”);
lcd.setCursor(0,1);
lcd.print(“Simulator”);
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);

}

void loop() {

// on/off switch
mode_input = customKey;
control_input = state;

reading = customKey;
if (reading == 1 && previous == 0 && millis() - time > debounce) {
if (state == 1)
state = 0;
else
state = 1;

D1 = state;

if(state==1)
pos=180;
else
pos=0;

time = millis(); }

// modes
reading2 = customKey;

if (mode_input == 2){
mode=mode+1;
}
if (mode==4){
mode=1;
}

if (mode==1){
servo_T.write(pos);
servo_Ind.write(pos);
servo_Pu.write(pos);
D2 = 1;
}

if (mode==2){
servo_T.write(pos);
servo_Ind.write(0);
servo_Pu.write(pos);
D2 = 2;
}
if (mode==3){
servo_T.write(180);
servo_Ind.write(pos);
servo_Pu.write(pos);
D2 = 3;
}

// print in LCD for state report
lcd.setCursor(0,0);
lcd.print(D1);
lcd.setCursor(1,0);
lcd.print(D2);
delay(100);
lcd.clear();
delay(100);

}

Please use code tags to post your code in