Arduino Leonardo

Hi,

I have made this program for NoLimits2/ridesims. All it does is when you press a push button the board sends a key stroke. I have just uploaded it to my Leonardo but it made my Mac go funny and it the screen started jumping around.

I have a button called ‘endofday’ which in real life is used to disable the panel but I want to use it to start/stop control over the keyboard. Here is the code for the button;

// End Of Day function control
if (endofdayPOS == LOW){
  Keyboard.end();} // Keyboard disabled
if (endofdayPOS == HIGH){
  Keyboard.begin();}  // Key strokes can be sent to PC

I did have a variable for it and had each button check to see if it was true/false but I decided to change it to disable/enable keyboard to make it easier.

Here is the rest of the code:

#include <Keyboard.h>

//Set Pin Values for buttons
int endofday = 1;
int reset = 2;
int switch1 = 3;
int switch2 = 4;
int switch3 = 5;
int switch4 = 6;
int sstop = 7;
int preshow = 8;
int estop = 9;
int gates = 10;
int restraints = 11;
int tilt = 12;
int stationselect = 13;
int launch = 14;
int dispatch = 15;

// Initial variable states
int gateHX;
int restraintHX;
int selectorHX;
int estopHX;
int tiltHX;
int rcsenable;
int stationstopmode = 0; // S Stop is off
int disrdy = false; //dispatch ready is false

void setup() {

// Set buttons pinmodes
  pinMode(endofday, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(sstop, INPUT_PULLUP);
  pinMode(preshow, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(gates, INPUT_PULLUP);
  pinMode(restraints, INPUT_PULLUP);
  pinMode(tilt, INPUT_PULLUP);
  pinMode(stationselect, INPUT_PULLUP);
  pinMode(launch, INPUT_PULLUP);
  pinMode(dispatch, INPUT_PULLUP);}


void loop() {
// Set variables for button states

int endofdayPOS = digitalRead(endofday);
int resetPOS = digitalRead(reset);
int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);
int sstopPOS = digitalRead(sstop);
int preshowPOS = digitalRead(preshow);
int estopPOS = digitalRead(estop);
int gatePOS = digitalRead(gates);
int restraintPOS = digitalRead(restraints);
int tiltPOS = digitalRead(tilt);
int selectorPOS = digitalRead(stationselect);
int launchPOS = digitalRead(launch);
int dispatchPOS = digitalRead(dispatch);

// RCS MODE: Enabled

// sets the system to enabled RCS Mode anytime RCSenable variable is high and estop is high
if (restraintPOS == HIGH){
  disrdy = false;}
else if (gatePOS == HIGH){
  disrdy = false;}
else
  disrdy = true;

// End Of Day function control
if (endofdayPOS == LOW){
  Keyboard.end();} // Keyboard disabled
if (endofdayPOS == HIGH){
  Keyboard.begin();}  // Key strokes can be sent to PC

// Station Stop function control
if (sstopPOS == LOW){
  stationstopmode = 1;} // Station Stop is ON
if (sstopPOS == HIGH){
  stationstopmode = 0;} // Station stop is off

// Restraint function control
if (restraintHX != restraintPOS){
  delay (100);}
  if (restraintPOS == LOW){         // closed pos
    Keyboard.press(KEY_DOWN_ARROW);
    delay (100);
    Keyboard.releaseAll();
    restraintHX = restraintPOS;}
  else{
    Keyboard.press(KEY_UP_ARROW); // Open pos
    delay (100);
    Keyboard.releaseAll();
    restraintHX = restraintPOS;}

// Gate function control
if (gateHX != gatePOS){
  delay (100);}
  if (gatePOS == LOW){ // If switch is in the closed position
    Keyboard.press(KEY_LEFT_ARROW);
    delay (100);
    Keyboard.releaseAll();
    gateHX = gatePOS;}
  else{
    Keyboard.press(KEY_RIGHT_ARROW);
    delay (100);
    Keyboard.releaseAll();
    gateHX = gatePOS;}

// Selector function control
if (selectorHX != selectorPOS){
  delay (100);}
  if (selectorPOS == LOW){ // If switch is in the 'one' position
    Keyboard.press(KEY_LEFT_ARROW);
    delay (100);
    Keyboard.releaseAll();
    selectorHX = selectorPOS;}
  else{
    Keyboard.press(KEY_RIGHT_ARROW); // If switch is in the 'two' position
    delay (100);
    Keyboard.releaseAll();
    selectorHX = selectorPOS;}

// Dispatch function control
if (dispatchPOS == LOW && stationstopmode == 0 && disrdy == true);{
  Keyboard.press(' ');
  delay (100);
  Keyboard.releaseAll();}

// Auto-stop when dispatch not ready
if (disrdy == false){
  if (dispatchPOS == LOW)
    Keyboard.press('s');
    delay (100);
    Keyboard.releaseAll();
    estopHX = LOW;
    rcsenable = LOW;}

// RCS Mode: E-Stop
if (estopPOS == LOW){
  rcsenable = LOW;}

 if (estopPOS == LOW && estopHX == HIGH){
  Keyboard.press('s');
  estopHX = estopPOS;
  delay (100);
  Keyboard.releaseAll();}

// RCS Mode: Reset
if (resetPOS == LOW && estopPOS == HIGH && rcsenable == LOW){
  rcsenable = HIGH;
  estopHX = estopPOS;
  Keyboard.press('r');
  delay (100);
  Keyboard.releaseAll();}

//Preshow doors/start function
if (preshowPOS == LOW){
  Keyboard.press(KEY_RETURN);
  delay (100);
  Keyboard.releaseAll();}

// Switch functions
if (switch1POS == LOW){
  Keyboard.press('1');
  delay (100);
  Keyboard.releaseAll();}

if (switch2POS == LOW){
  Keyboard.press('2');
  delay (100);
  Keyboard.releaseAll();}

if (switch3POS == LOW){
  Keyboard.press('3');
  delay (100);
  Keyboard.releaseAll();}

if (switch4POS == LOW){
  Keyboard.press('4');
  delay (100);
  Keyboard.releaseAll();}

// launch function
if (launchPOS == LOW && stationstopmode == 0){
  Keyboard.press('l');
  delay (100);
  Keyboard.releaseAll();}

// Tilt function
if (tiltPOS == HIGH && tiltHX == LOW && stationstopmode == 0){
  Keyboard.release(KEY_RETURN);
  tiltHX = tiltPOS;}
else{
  Keyboard.press(KEY_RETURN);}

  
}

I was wondering if you could help me sort it out, thanks.

Start with a much simpler program that just sends a single keystroke. When you can do that reliably you can extend the program.

Are you sending the keystrokes too frequently?

...R

I can’t do a more simple one haha it has to be like this for the control panel to actually be a control panel…

I did not press any buttons which concerned me, nothing is plugged into the arduino.

mxttymitch: I can't do a more simple one.

Yes you can. Just for testing and proving the concept of having your Leonardo pretend to be a keyboard.

I did not press any buttons which concerned me, nothing is plugged into the arduino.

So what might the Leonardo be doing to upset your MAC? - that's why you need a simple program to start with. Have you tried the keyboard demo program?

...R

So do you suggest I do a very simple program. For example press 'R' when pushbutton position is LOW? Do I do that for every pin?

Just do it for one pin first, and then slowly expand what your code does.