3 buttons combined

hi,

i have an indicator switch from a truck. and i am trying to find a way to set up the wiper switches. it has 3 modes > interval, 1 and 2 (those are the wiper speeds, as 2 is the fastest) now am i trying to find a way that if the switch (interval) is pressed it does an action like blink a LED. now comes the tricky part. when switch 1 is pressed, it goes via interval. but i want it that when interval is released and 1 is being pressed in a certain amound of time it blinks the LED twice.

is this doable? i haven't got any code for it.

best regards

devon

Yes, it's probably feasible. Your explanation could be a bit clearer. We usually expect some effort at coding here, what have you tried? Have you succeeded in connecting the switches and testing them with a simple test sketch?

Please post a wiring diagram for the switch - it will make your description much easier to understand. A photo of a simple pencil diagram will be fine. See this Simple Image Posting Guide

…R

got everything wired up from every function of the switch and they all work
this is my code so far but i can’t find a way to make it as i would like to (i left unnecessary parts of the code away):

#include <Keyboard.h>
#define COMMON_ANODE


int led_EngineBrake_red = 11;
int led_EngineBrake_green = 10;
int led_EngineBrake_blue = 9;
int count = 0;
int count1 = 0;
int countHorn = 0;
int countWiper = 0;
int countEngineBrake = 0; 

const byte switchPinA = 8;   // Indicator Left
const byte switchPinB = 2;   // Indicator Right
const byte switchPinC = 6;   // Flash High Beams
const byte switchPinD = 5;   // High Beams
const byte switchPinE = A0;  // Horn Mode
const byte switchPinF = A4;  // Horn
const byte switchPinG = A5;  // Wiper Mode 2
const byte switchPinH = 4;   // Wiper Mode 1
const byte switchPinI = 3;   // Wiper Mode Interval
const byte switchPinJ = 12;  // Engine Brake
const byte switchPinK = A3;  // Wiper Washer

byte oldSwitchStateA = HIGH; 
byte oldSwitchStateB = HIGH; 
byte oldSwitchStateC = HIGH;  
byte oldSwitchStateD = HIGH; 
byte oldSwitchStateE = HIGH; 
byte oldSwitchStateF = HIGH; 
byte oldSwitchStateG = HIGH; 
byte oldSwitchStateH = HIGH;
byte oldSwitchStateI = HIGH;
byte oldSwitchStateJ = HIGH;
byte oldSwitchStateK = HIGH;

const unsigned long debounceTime = 10;  
const unsigned long debounceTime1 = 50;
const unsigned long debounceTime2 = 250; 


void setup ()
{
  Serial.begin (115200);
  pinMode (led_EngineBrake_red, OUTPUT);
  pinMode (led_EngineBrake_green, OUTPUT);
  pinMode (led_EngineBrake_blue, OUTPUT);
  pinMode (switchPinA, INPUT_PULLUP);
  pinMode (switchPinB, INPUT_PULLUP);
  pinMode (switchPinC, INPUT_PULLUP);
  pinMode (switchPinD, INPUT_PULLUP);
  pinMode (switchPinE, INPUT_PULLUP);
  pinMode (switchPinF, INPUT_PULLUP);
  pinMode (switchPinG, INPUT_PULLUP);
  pinMode (switchPinH, INPUT_PULLUP);
  pinMode (switchPinI, INPUT_PULLUP);
  pinMode (switchPinJ, INPUT_PULLUP);
  pinMode (switchPinK, INPUT_PULLUP);
  analogWrite(led_EngineBrake_red, 255);
  analogWrite(led_EngineBrake_green, 255);
  analogWrite(led_EngineBrake_blue, 255);
  Keyboard.begin();
}

void loop ()
{
  // Wiper Mode 2
  byte switchStateG = digitalRead (switchPinG);
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;
    delay (debounceTime);
    if (countWiper == 2 && switchStateG == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
      countWiper = 3;
    }
    else if (switchStateG == HIGH)
    {
    }
  }



  // Wiper Mode 1
  byte switchStateH = digitalRead (switchPinH);
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;
    delay (debounceTime);
    if (countWiper == 0 && switchStateH == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
      delay(50);
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
      countWiper = 2;
    }
    else if (countWiper == 3 && switchStateH == LOW)
    {
      Keyboard.press(';');
      delay(10);
      Keyboard.release(';');
      countWiper = 2;
    }
  }



  // Wiper Mode Interval
  byte switchStateI = digitalRead (switchPinI);
  if (switchStateI != oldSwitchStateI)
  {
    oldSwitchStateI =  switchStateI; 
    delay (debounceTime);
    if (countWiper == 0 && switchStateI == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
      countWiper = 1;
    }
    else if (countWiper == 2 && switchStateI == LOW)
    {
      Keyboard.press(';');
      delay(10);
      Keyboard.release(';');
      countWiper = 1;
    }
    else if (countWiper == 1 && switchStateI == HIGH)
    {
      Keyboard.press(';');
      delay(10);
      Keyboard.release(';');
      countWiper = 0;
    }
  }
}
[\code]

mgysgtdevon:
got everything wired up from every function of the switch and they all work

You know how the switch works but it's a bit difficult to help you if you won't share the info with us.

...R

Write down a simple logic in plain words of what you are trying to achieve. Do it for each switch separately As if you were coding in plain words. Often you can then see what the code needs to be.

here is a drawing of the wiring of the switch

(--- is interval)
each mode (---, 1 and 2) acts as a push button. there is no button at O.
i want it to be like this. when turning the knob from O to --- it serial prints --- (on the press of ---) and when i turn it back to O it serial prints O (on the release of ---) -> now here is the problem since there is a release of --- when i turn the knob to 1. but i want to tell arduino that if there is another button pressed in a certain amount of time it serial prints the number when the knob is being turned into. if there is no button being pressed in that amound of time it serial prints O.
hope this makes it a bit understandable

mgysgtdevon:
but i want to tell arduino that if there is another button pressed in a certain amount of time it serial prints the number when the knob is being turned into. if there is no button being pressed in that amound of time it serial prints O.

Time to read the Using millis() for timing beginner’s guide.

And learn about IDE -> file/examples/digital/statechangedetection.

When the interval switch opens start a timer. If one of the other switches is closed before the timer times out do thing A else revert to O position.

  // Wiper Mode 2
  byte switchStateG = digitalRead (switchPinG);
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;
    delay (debounceTime);
    if (switchStateG == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
    }
    else if (switchStateG == HIGH)
    {
    }
  }



  // Wiper Mode 1
  byte switchStateH = digitalRead (switchPinH);
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;
    delay (debounceTime);
    if (switchStateH == LOW)
    {
      oneButtonPressed = false;
    }
    else if (switchStateH == HIGH)
    {
      oneButtonPressed = true;
    }
  }



  // Wiper Mode Interval
  currentMillis = millis();
  byte switchStateI = digitalRead (switchPinI);
  if (switchStateI != oldSwitchStateI)
  {
    oldSwitchStateI =  switchStateI; 
    delay (debounceTime);
    if (switchStateI == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
    }
    else if (currentMillis - startMillis <= period && switchStateI == HIGH)
    {
      if (oneButtonPressed == false);
      {
        Serial.println("c");
        startMillis = currentMillis;
      }
    }
  }

this is what i got so far but i can’t get it to work. when going from — to O it does the same thing as when it goes from — to 1

Go through it line by line and think about why.

You need to convert your code to plain words. Use // to annotate your code. Don’t forget the loop so when you read your code don’t just read it once. Where there is an input read it through for each potential state and see what it does.

I noticed that when i am trying to add another else or else if it gives me an error that i forgot a } before else.
This is the example of what i am trying to achieve.

  // Wiper Mode Interval
  currentMillis = millis();
  byte switchStateI = digitalRead (switchPinI);
  if (switchStateI != oldSwitchStateI)
  {
    oldSwitchStateI =  switchStateI;
    delay (debounceTime);
    if (switchStateI == LOW)
    {
      Keyboard.press('u');
      delay(10);
      Keyboard.release('u');
    }
    else if (currentMillis - startMillis <= period && switchStateI == HIGH)
    {
      if (oneButtonPressed == false);
      {
        Serial.println("c");
        startMillis = currentMillis;
      }
     else if (oneButtonPressed == true);
     {
        Serial.println("d");
        startMillis = currentMillis;
      }
    }
  }

It’s the last (else if (oneButtonPressed == true); ) that gives me an error. Isn’t it possible to add “if” and “else if” in another “if”?

Can't manage to get this working. I'll post my code this afternoon of what i got so far.

When i do the multiple option for a button i use a timer and counter.

If button is high. // pressed
If ( millis() - then > 500
Count++
Then = millis()

If count > 1
If count < 4
Redled = low
If count > 4. And < 8
Redled = high
GreenLed = low
If count > 8
Redled = high
GreeLed = high
} // end if(count > 1

DigitalWrite(LED_RED, Redled)
DigitalWrite( LED_green, greenLed)
//these are always active and show changes

If button== low
DigitalWrite(relayOne,redLed)
DigitalWrite(relayTwo,greenLed)
Count = 0

This is not actual code.
Just a simplistic concept.
Mistakes abound

this is what i got so far but it ain’t working like i want it to. so i might just give up on this since it’s way to hard for me to do it.

  // Wiper Mode 1
  byte switchStateH = digitalRead (switchPinH);
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;
    delay (debounceTime);
    if (switchStateH == LOW)
    {
      oneButtonPressed = true;
    }
    else if (switchStateH == HIGH)
    {
    }
  }



  // Wiper Mode Intervaluuuuuuuu
  currentMillis = millis();
  byte switchStateI = digitalRead (switchPinI);
  if (switchStateI != oldSwitchStateI)
  {
    oldSwitchStateI =  switchStateI; 
    delay (debounceTime);
    if (switchStateI == LOW)
    {
      Keyboard.press('-');
      delay(10);
      Keyboard.release('-');
    }
    else if (switchStateI == HIGH)
    {
      if (currentMillis - startMillis <= period)
      {
        if (oneButtonPressed == false)
        {
          Keyboard.press('1');
          delay(10);
          Keyboard.release('1');
          startMillis = currentMillis;
        }
        else if (oneButtonPressed == true)
        {
          Keyboard.press('O');
          delay(10);
          Keyboard.release('O');
          oneButtonPressed = false;
          startMillis = currentMillis;
        }
      }

This does not address the OP’s request as he has 3 switches
this is one switch, multiple outputs.

I put together a simple sketch with 2 relays
and posted a simple youtube One Button, 2 Relays

// 
unsigned long then ;
int Count ;

const int button1 = 2 ;
const int RedLed = 3;
const int YellowLed = 4;
const int BlueLed = 5 ;
const int relayOne = 6 ;
const int relayTwo = 7 ;
const int relayThree = 8 ;
int LED_RED, LED_Blue, button ;


void setup() {
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(BlueLed, OUTPUT);
  // pinMode(ledPin, OUTPUT);
  // pinMode(ledPin, OUTPUT);

  digitalWrite(relayOne, HIGH);
  digitalWrite(relayTwo, HIGH);
  digitalWrite(RedLed, LOW);
  digitalWrite(BlueLed, LOW);
  delay(2000);
}


void loop() {

  button = digitalRead(button1);

  if (button == HIGH) {// pressed
    if (millis() - then > 500 ) {
      Count++;
      then = millis();
    }
  }
  if (Count >= 1) {
    if (Count <= 4) {
      LED_RED = LOW ;
      LED_Blue = LOW ;
    }
    if (Count >= 4 && (Count <= 6)) {
      LED_RED = LOW ;
      LED_Blue = HIGH;
    }
    if (Count >= 6 && (Count <= 8)) {
      LED_RED = HIGH ;
      LED_Blue = LOW;
    }
    if (Count >= 8) {
      LED_RED = LOW;
      LED_Blue = LOW;
    }
  } //    end if(count > 1


// relays have common ground, power from pin to resistor to relay to ground
// LED's fed from 5v through resistor to pin. pin pulls to ground
// LED outputs are inverted

  digitalWrite(RedLed, !LED_RED);
  digitalWrite(BlueLed, !LED_Blue);


  //these are always active and show changes

  if (button == LOW) {
    digitalWrite(relayOne, LED_RED);
    digitalWrite(relayTwo, LED_Blue);
    Count = 0;
  }
} // end  loop

I tried to understand the flow, but it eludes me.

taking out the extra words, here is the summation from the two posts :

=========================
truck indicator switch
wiper switches. 3 modes > interval, 1(low) and 2(Hi)wiper speeds [ should be 4 : 0, interval, 1 and 2 ]
if the switch (interval) is pressed = blink a LED.
switch 1 is pressed = interval.
interval released and 1 pressed -
blinks the LED twice.

switch states
0 = open
--- = interval
1 = low
2 = high

O to --- it serial prints ---
back to O it serial prints O
from --- to 1 = serial prints the number

it would seem the switch moves from from any one position to any other position.
it must rotate from 0 to interval, to 1 then to 2
in reverse, it would do the opposite.

the way I read this, move the switch from 0 to interval, and then blink the LED one time
move it from interval to 1 (low), blink the LED two times.

every time the switch is placed on any position, there should be a serial print showing that position.

I get the feeling that there is something more, like pause on interval, then move to 1, and the wipers would run on an interval.
but, if you fast spun to 1, they would move in the standard slow speed.

it seems that a deeper description is needed to understand what is desired.

mgysgtdevon:
it has 3 modes > interval, 1 and 2

Technically, it has *four *modes if you include OFF.

Chew on this - Work up some code such that if any switch position *changes * reset a (debounce) timer. When the timer completes, that is, the switch has been stable for X milliseconds, update the current states of the switches. Whichever one is on do that function. If none are on you know it's in the OFF position.

that is exactly what i am trying to achieve but with still no succes. i can't manage to get the timer and the rest working

guess nobody knows how this is possible to achieve?