wipers

hi,

i am trying to find a code for my Arduino pro micro to cycle through the wiper modes in ETS2.
in ETS2 the wipers cycle from 0 to 1 to 2 and back down it cycles from 2 straight to 0.

my idea is

mode 2) when cycled from 1 it presses A once and when its cycled to 1 it presses A twice but ignores the button press on 1

mode 1) when cycled from 0 it presses A once and when its cycled to 0 it presses A twice

mode 0) does nothing since its no button press

but i can't find a way to figure this out for a code

in ETS2 the wipers cycle from 0 to 1 to 2 and back down it cycles from 2 straight to 0.

So the modes go 0 1 2 0 1 2 and so on

Start with wiperMode = 0

when actioned, increment wiperMode
if wiperMode equals 3
  wiperMode = 0
end if
do stuff according to the current value of wiperMode

UKHeliBob:
So the modes go 0 1 2 0 1 2 and so on

Start with wiperMode = 0

when actioned, increment wiperMode

if wiperMode equals 3
  wiperMode = 0
end if
do stuff according to the current value of wiperMode

yes 0 1 2 0 1 2 but i have a 3 way switch with 0 being off and 1 and 2 being on

but the problem is the game doesn't see my arduino as a controller but as a HID device so i'm using it with keyboard.print command

the modes 0 to 1 and 1 to 2 is simple enough to put a keyboard.print command on every press of the switch but when i need it to go from 2 to 1 it should do keyboard.print on the release of 2 and ignore the keyboard.print on the press of 1

Delta_G:
What exactly should it print in each case?

It's easy enough to have a variable that when you change states you store the old state in. Then you can say, if the new state is 1 and the last state was 0 then do one thing. If the new state is 1 but the last state was 2 then do something else.

Is that what you're after?

just a simple letter so it can be bound in game.

what you just described is exactly what i am looking for but i cant find a way to make the Arduino think that the last state was 2 or 0

Delta_G:
You must be over thinking it. This is simple.

   if(someEventHappened()){

theOldValue = theValue;
      theValue = someNewValue;

i cant seem to understand this part of the code.

i made this code for my pro micro

#include <Keyboard.h>

const byte switchPinG = 8;
const byte switchPinH = 9;
byte oldSwitchStateG = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateH = HIGH;  // assume switch OFF because of pull-up resistor

void setup ()
{
  Serial.begin (115200);
  pinMode (switchPinG, INPUT_PULLUP);
  pinMode (switchPinH, INPUT_PULLUP);
  Keyboard.begin();
}  // end of setup

void loop ()
{
  // see if switch G is OFF or ON
  byte switchStateG = digitalRead (switchPinG);

  // has it changed since last time?
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateG == LOW)
    {
      Keyboard.print("g");
    }  // end if switchStateG is LOW
  }  // end of state change

  // see if switch H is OFF or ON
  byte switchStateH = digitalRead (switchPinH);

  // has it changed since last time?
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateH == LOW)
    {
      Keyboard.print("h");
    }  // end if switchStateH is LOW
  }  // end of state change
}

Delta_G:
OK, it's three exceedingly simple lines. You actually have already used the same concept in your code:

if (switchStateH != oldSwitchStateH)

{
    oldSwitchStateH =  switchStateH;  // remember for next time




So you're going to have to do a little better than "I can't seem to understand it". What part don't you understand?

Do you know how an if statement works? I just stuck some random condition in there to indicate that you would put whatever you might need in that if. 

Do you understand the assignment of a value to a variable?

What part is confusing to you?

If theOldValue = theValue and theValue = someNewValue then theOldValue should be someNewValue?

Do you know how an if statement works? i somehow understand it but im a newbie to coding
Do you understand the assignment of a value to a variable? i don't understand this at all
The code I'm using I've got that from the internet

   if(someEventHappened()){
       theOldValue = theValue;
       theValue = someNewValue;

If i use this then it doesn't tell arduino that the last switchState was G?
switchStateG = mode 2
switchStateH = mode 1

Delta_G:
How do you figure?

Let's say old is 0, theValue is 1, and the new value is 2.

So we say theOldValue = theValue. So that puts 1 into theOldValue.

Then AFTER that we say theValue = theNewValue, so that puts 2 into theValue. It doesn't do anything to theOldvalue, theOldValue isn't even mentioned on that line. It's not going to go back a line and do something. Code runs from top to bottom, line by line.

What part of the if statement confuses you? Don't ever tell me you don't understand something without either asking a question or explaining what part you don't get. Just, "I don't understand" is a useless and worthless statement. If you tell someone WHAT you don't understand then they can help you.

If I write, someVariableName = someNumber;

then that assigns whatever value is on the right side of that equals to whatever is on the left. In this case if someNumber is 5 then this will set someVariableName to 5. It is really quite simple. So again, you're going to have to elucidate what part confuses you.

If i use this then it doesn't tell arduino that the last switchState was G?

switchStateG = mode 2
switchStateH = mode 1




I don't know what you mean. Arduino doesn't just keep up with stuff you don't tell it to. 
These lines say that switchStateG is now whatever value was in mode2 and switchStateH is whatever value was in mode1 at the time these lines were called. That's all that says.

the part of the IF statement i don't understand is if you ad multiple ELSE after each other, does that work or am i limited to just an IF and ELSE?

i have been searching the web and reading allot of stuff and i think i understand the variables now

i came up with a code with a fake pin so i could change the variable of it but it does't always detect the change of the variable

#include <Keyboard.h>

const byte switchPinG = 8;
const byte switchPinH = 9;
byte oldSwitchStateG = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateH = HIGH;  // assume switch OFF because of pull-up resistor
int switchState = LOW;  // current state of the switch
const byte switchPin = 11;

void setup ()
{
  Serial.begin (115200);
  pinMode (switchPinG, INPUT_PULLUP);
  pinMode (switchPinH, INPUT_PULLUP);
  Keyboard.begin();
}  // end of setup

void loop ()
{
  // see if switch G is OFF or ON
  byte switchStateG = digitalRead (switchPinG);
  switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateG == LOW)
    {
      Keyboard.print("g");
      Keyboard.print("g");
      digitalWrite(switchPin, HIGH);
    }  // end if switchStateG is LOW
  }  // end of state change

  // see if switch H is OFF or ON
  byte switchStateH = digitalRead (switchPinH);
  switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchState == HIGH)
    {
      Keyboard.print("");
      digitalWrite(switchPin, LOW);
    }  // end if switchStateH is LOW
    else if (switchState == LOW &&  switchStateH == LOW)
    {
      Keyboard.print("h");
    }
    else if (switchState == LOW &&  switchStateH == HIGH)
    {
      Keyboard.print("h");
      Keyboard.print("h");
    }
  }  // end of state change
}

in this code i use switchStateH for mode 1 and switchStateG for mode 2
when i cycle from 0 to 1 it sends out the letter H. when i cycle from 1 to 2 it sends out the letter H twice. when it is on mode 2 it sends out the letter G twice. and when i cycle it from 2 to 1 it does nothing. but here is the problem when i cycle it from 1 to 0 it should send out the letter H twice but it doesn't detect the change in the variable it should have done when the switch returned to mode 1

    {
      Keyboard.print("");
      digitalWrite(switchPin, LOW);
    }  // end if switchStateH is LOW

here it should change the variable back to LOW but it doesn't read it after that

Delta_G:
Wait, I don't understand. What is connected to pin 11? This "switchPin". If it is a switch then why are you writing voltages to it?

nothing is connected to pin 11. i use it so i could change the variable of it. basically when the loop for switchStateG starts it changes the variable of switchState to HIGH so then in the loop for switchStateH i could use that variable

Delta_G:
No, you don't use a pin for that. It isn't the "variable of a pin". A variable is just a number stored in memory. That's all it is. HIGH and LOW are just numbers. They're 1 and 0 for now. We give names to numbers all the time.

You just need a regular old int variable to hold that state (actually before the pedants get here it's better as a byte to save memory but let's save that lesson for tomorrow).

digitalWrite isn't saving any variable. That is physically writing a voltage to a pin. You don't need to do that to save a number.

Go have a look at the "State Change Example". It uses a variable to keep up with an old pin state.

Really, you may be in a little over your head here. It may do you some good to shelve this project for a minute and work on some simpler examples to learn the basics.

  // see if switch G is OFF or ON
  byte switchStateG = digitalRead (switchPinG);

  // has it changed since last time?
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateG == LOW)
    {
      Keyboard.print("g");
      delay (debounceTime2);
      Keyboard.print("g");
      count++;
      count++;
    }  // end if switchStateG is LOW
    else
    {
      Keyboard.print("g");
    }
  }  // end of state change

  // see if switch H is OFF or ON
  byte switchStateH = digitalRead (switchPinH);

  // has it changed since last time?
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;  // remember for next time
    delay (debounceTime);   // debounce
    if (count == 2)
    {
      Keyboard.print("h");
      count = 1;
    }  // end if switchStateH is LOW
    else if (count == 0 &&  switchStateH == LOW)
    {
      Keyboard.print("h");
      count++;
    }
    else if (count == 1 &&  switchStateH == HIGH)
    {
      Keyboard.print("h");
      delay (debounceTime2);
      Keyboard.print("h");
      count = 0;
    }
  }  // end of state change

i re writed the code and added count values in it and now its working like i wanted to. thanks for all your time and help :slight_smile: