Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DIY H-Shifter by modelleicher
- // Version 3.10
- // new:
- // completely new sketch easier to read and edit later with uniform variable names
- //different modes for the onOff, HighLow, Group lever
- // initialize Joystick
- JoyState_t joySt;
- // input pins
- int inp_gear1 = 3;
- int inp_gear2 = 4;
- int inp_gear3 = 5;
- int inp_gear4 = 6;
- int inp_gear5 = 7;
- int inp_gear6 = 8;
- int inp_gear7 = 9;
- int inp_onOff = 15;
- int inp_highLow = 2;
- int inp_groupFW = A0;
- int inp_groupBW = A1;
- // raw state variables
- int raw_gear1, raw_gear2, raw_gear3, raw_gear4, raw_gear5, raw_gear6, raw_gear7, raw_onOff, raw_highLow, raw_groupFW, raw_groupBW = LOW;
- // final state variables
- int final_onOff, final_highLow, final_highLow2, final_groupFW, final_groupBW = LOW;
- // variables needed to detect state changes for the times buttons
- int listener_onOff, listener_highLow, listener_groupFW, listener_groupBW = LOW;
- int timer_onOff, timer_highLow, timer_groupFW, timer_groupBW = 0;
- int timerState_onOff, timerState_highLow, timerState_groupFW, timerState_groupBW = false;
- // variables needed for the timer and debounce on the group buttons
- int temp_groupFW, temp_groupBW = LOW;
- int debTimer_groupFW, debTimer_groupBW = 0;
- int finalTemp_groupFW, finalTemp_groupBW = LOW;
- // setup mode variables
- int setupMode = false;
- int onOffMode = 0;
- int highLowMode = 1;
- int groupMode = 0;
- int setupModeTimerOn = false;
- int setupModeTimer = 0;
- int setupModeCnt = 0;
- int initiated = false;
- byte incoming = 0;
- // constant time variables
- byte highTimer = 100;
- void setup()
- {
- // pin Mode, all Input
- pinMode(inp_gear1, INPUT);
- pinMode(inp_gear2, INPUT);
- pinMode(inp_gear3, INPUT);
- pinMode(inp_gear4, INPUT);
- pinMode(inp_gear5, INPUT);
- pinMode(inp_gear6, INPUT);
- pinMode(inp_gear7, INPUT);
- pinMode(inp_onOff, INPUT);
- pinMode(inp_highLow, INPUT);
- pinMode(inp_groupFW, INPUT);
- pinMode(inp_groupBW, INPUT);
- // Variables for the joystick values, we only use buttons
- joySt.xAxis = 128;
- joySt.yAxis = 128;
- joySt.zAxis = 128;
- joySt.xRotAxis = 128;
- joySt.yRotAxis = 128;
- joySt.zRotAxis = 128;
- joySt.throttle = 128;
- joySt.rudder = 128;
- joySt.hatSw1 = 8;
- joySt.hatSw2 = 8;
- joySt.buttons = 0;
- }
- void loop()
- {
- if (setupMode == false)
- {
- // get the raw input values first
- raw_gear1 = digitalRead(inp_gear1);
- raw_gear2 = digitalRead(inp_gear2);
- raw_gear3 = digitalRead(inp_gear3);
- raw_gear4 = digitalRead(inp_gear4);
- raw_gear5 = digitalRead(inp_gear5);
- raw_gear6 = digitalRead(inp_gear6);
- raw_gear7 = digitalRead(inp_gear7);
- raw_onOff = digitalRead(inp_onOff);
- raw_highLow = digitalRead(inp_highLow);
- raw_groupFW = digitalRead(inp_groupFW);
- raw_groupBW = digitalRead(inp_groupBW);
- // different modes for the onOff Switch
- if (onOffMode == 0) // switch acts as switch, on/off
- {
- final_onOff = raw_onOff;
- }
- else if (onOffMode == 1) // switch acts as buttons, each time switched the button goes on for 100ms
- {
- if (raw_onOff != listener_onOff) // check listener if state has changed
- {
- if (timerState_onOff == false) //timer isn't active, start the button press
- {
- timerState_onOff = true;
- timer_onOff = millis();
- final_onOff = HIGH;
- listener_onOff = raw_onOff;
- }
- }
- if (timerState_onOff == true) // if timer is active
- {
- if (timer_onOff + highTimer < millis()) // check if 100ms are up, if so, stop the button press
- { // and reset all timer related variables
- timerState_onOff = false;
- timer_onOff = 0;
- final_onOff = LOW;
- }
- }
- }
- // onOff switch modes end
- // different modes for the highLow switch
- if (highLowMode == 0) // switch acts as switch, on/off
- {
- final_highLow = raw_highLow;
- }
- else if (highLowMode == 1) // switch acts as buttons, each time switched the button goes on for 100ms
- {
- if (raw_highLow != listener_highLow) // check listener if state has changed
- {
- if (timerState_highLow == false) //timer isn't active, start the button press
- {
- timerState_highLow = true;
- timer_highLow = millis();
- final_highLow = HIGH;
- listener_highLow = raw_highLow;
- }
- }
- if (timerState_highLow == true) // if timer is active
- {
- if (timer_highLow + highTimer < millis()) // check if 100ms are up, if so, stop the button press
- { // and reset all timer related variables
- timerState_highLow = false;
- timer_highLow = 0;
- final_highLow = LOW;
- }
- }
- }
- else if (highLowMode == 2) // switch acts as a two way switch sets a different button high on each state
- {
- final_highLow = raw_highLow;
- final_highLow2 = !raw_highLow;
- }
- // high low switch modes end
- // group button stuff..
- // with debounce and things :)
- // also, different modes... (why do i do that to myself?!)
- if (groupMode == 0) // the easy one first.. just simple 1:1 input to output
- {
- final_groupFW = LOW;
- final_groupBW = LOW;
- if (raw_groupFW == HIGH && raw_groupBW == HIGH)
- {
- final_groupBW = LOW;
- final_groupFW = HIGH;
- }
- else if (raw_groupBW == HIGH && raw_groupFW == LOW)
- {
- final_groupBW = HIGH;
- final_groupFW = LOW;
- }
- }
- else if (groupMode == 1 || groupMode == 2) // now the difficult one.. with debounce and stuff
- {
- // since the debounce is needed in mode 1 and 2, only needed once.
- // first button
- if (raw_groupBW == HIGH && raw_groupFW == LOW) // button 2 is pressed
- {
- if (temp_groupFW == LOW) // timer starts
- {
- temp_groupFW = HIGH;
- debTimer_groupFW = millis();
- }
- else if (temp_groupFW == HIGH)
- {
- if (debTimer_groupFW + 60 < millis())
- {
- finalTemp_groupFW = HIGH;
- }
- }
- }
- else if (raw_groupBW == LOW) // reset all as soon as button gets low
- {
- temp_groupFW = LOW;
- debTimer_groupFW = 0;
- finalTemp_groupFW = LOW;
- }
- // second button
- if (raw_groupBW == HIGH && raw_groupFW == HIGH) // button 1 is pressed
- {
- if (temp_groupBW == LOW) // timer starts
- {
- temp_groupBW = HIGH;
- debTimer_groupBW = millis();
- }
- else if (temp_groupBW == HIGH)
- {
- if (debTimer_groupBW + 60 < millis())
- {
- finalTemp_groupBW = HIGH;
- }
- }
- }
- else if (raw_groupFW == LOW) // reset all as soon as button gets low
- {
- temp_groupBW = LOW;
- debTimer_groupBW = 0;
- finalTemp_groupBW = LOW;
- }
- // debounce end
- // now the modes....
- if (groupMode == 1) // each button has its own relative button that gets pressed for 100ms
- {
- if (finalTemp_groupFW == HIGH) // timer stuff, same as with the other switches
- {
- if (timerState_groupFW == false)
- {
- timerState_groupFW = true;
- timer_groupFW = millis();
- final_groupFW = HIGH;
- }
- if (timerState_groupFW == true)
- {
- if (timer_groupFW + highTimer < millis()) {final_groupFW = LOW;}
- }
- }
- else if (finalTemp_groupFW == LOW)
- {
- timerState_groupFW = false;
- timer_groupFW = 0;
- }
- if (finalTemp_groupBW == HIGH) // timer stuff for the other button
- {
- if (timerState_groupBW == false)
- {
- timerState_groupBW = true;
- timer_groupBW = millis();
- final_groupBW = HIGH;
- }
- if (timerState_groupBW == true)
- {
- if (timer_groupBW + highTimer < millis()) {final_groupBW = LOW;}
- }
- }
- else if (finalTemp_groupBW == LOW)
- {
- timerState_groupBW = false;
- timer_groupBW = 0;
- }
- }
- if (groupMode == 2) // both buttons mapped to one button
- {
- if (finalTemp_groupFW == HIGH || finalTemp_groupBW == HIGH) // timer stuff, same as with the other switches
- {
- if (timerState_groupFW == false)
- {
- timerState_groupFW = true;
- timer_groupFW = millis();
- final_groupFW = HIGH;
- }
- if (timerState_groupFW == true)
- {
- if (timer_groupFW + highTimer < millis()) {final_groupFW = LOW;}
- }
- }
- else if (finalTemp_groupFW == LOW && finalTemp_groupBW == LOW)
- {
- timerState_groupFW = false;
- timer_groupFW = 0;
- }
- }
- }
- // now that all this stuff is done... send the data to the pc
- // first the gears.. here we send raw data
- bitWrite(joySt.buttons, 0, raw_gear1);
- bitWrite(joySt.buttons, 1, raw_gear2);
- bitWrite(joySt.buttons, 2, raw_gear3);
- bitWrite(joySt.buttons, 3, raw_gear4);
- bitWrite(joySt.buttons, 4, raw_gear5);
- bitWrite(joySt.buttons, 5, raw_gear6);
- bitWrite(joySt.buttons, 6, raw_gear7);
- // now the switches, here we use final data
- bitWrite(joySt.buttons, 8, final_onOff);
- bitWrite(joySt.buttons, 9, final_highLow);
- bitWrite(joySt.buttons, 10, final_highLow2);
- bitWrite(joySt.buttons, 11, final_groupBW);
- bitWrite(joySt.buttons, 12, final_groupFW);
- // and send the stuff...
- Joystick.setState(&joySt);
- }
- else if (setupMode == true)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement