Master Arm Switch

I'm Currently Building an F-16 flight sim Pit for Falcon 4 BMS. I've purchased Both an UNO & MICRO. The First circuit I'm working on is the "Master Arm Switch".
I want to use a Push-Button Switch and 3 LEDs'. I plan to use the UNO to Control the LEDs' in the Pit and the Micro to send the Keyboard Command "SHIFT A" to the Computer to Cycle it in the Game.I have a whole bunch of Push-Button Switches(I found a couple of Key-Boards from the 70's with REAL Switches).

In the Game the Default(Starting Position) is OFF. The 1rst KeyPress Turns it to "SIMULATE"(SIM), The 2nd Keypress Turns it to "ARM" and the 3rd KeyPress Turns it "OFF". Continuing to Press the Key will Repeat the Cycle.

I Plan on Using the 5V Feed from the UNO to the Switch. I put a 10K pull-down resistor on the backside of the Switch and Want Make the PIN2 on both the UNO and the MICRO the INPUT From it.

The MICRO will read the INPUT as "HIGH" and OUTPUT "SHIFT A" to the Computer.

The UNO will READ the 'FIRST' Button Press as "HIGH" and OUTPUT "HIGH" to PIN3 and Turn "ON" the YELLOW LED(Set the Game to "SIMULATE").
The UNO will THEN READ the 'SECOND' Button Press as "HIGH" and OUTPUT "LOW" to PIN3 and "HIGH" to PIN4, thus Turning "OFF" the YELLOW LED and Turning "ON" the GREEN LED(Set the Game to "ARM").
The UNO will THEN READ the 'THIRD' Button Press as "HIGH" and OUTPUT "LOW" to PIN4 and "HIGH" to PIN5, thus Turning "OFF" the GREEN LED and Turning "ON" the RED LED(Set the Game to "OFF").
Then the Cycle REPEATS itself.
I know there Must be a De-Bounce in there as Well.

I can devise circuits and build them, But I am a TOTAL NOOB at Writing the Program, so Any Help would Be Appreciated. Perhaps Someone out there has already written something like That I have Enclosed both a Panel Picture and a Diagram.

I also have a few ideas for OTHER Types of Switches, and as I fix HONDA CARS for a living I have found a PLETHORA of useable Parts in them, and I'm willing to share Them with the Entire Community.

Thank You for Your Time....Dave (A.K.A Gandalf)

MASTER ARM.JPG

I think you forgot to connect ground to the Arduino Micro.

jeffmorris wrote:

I think you forgot to connect ground to the Arduino Micro.

I Did....& Fixed

So I started the Program by Modifying the "StateChangeDetection" Sketch in the Example folder.
I added another Output

const int ledPinY = 3; // the pin that the YellowLED is attached to &
const int ledPinR = 4; // the pin that the RedLED is attached to

Then I Modified the "if" statement to

digitalWrite(ledPinY, HIGH);
digitalWrite(ledPinR, LOW);

and the "else" statement to

digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinY, LOW);

Thus I wound up with

 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPinY = 3;       // the pin that the YellowLED is attached to
const int ledPinR = 4;       // the pin that the RedLED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LEDs as an output:
  pinMode(ledPinY, OUTPUT);
   pinMode(ledPinR, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPinY, HIGH);
    digitalWrite(ledPinR, LOW);
  } else {
   digitalWrite(ledPinR, HIGH);
   digitalWrite(ledPinY, LOW);
  }
  
}

I ran the compiler and uploaded it to the Uno, and the Starting state is Y-LED is "ON". Pushing the Button 1 time turns the R-LED on, and the Y-LED Off. Push it again and it turns Y On & R Off & keeps repeating(This will work for several other Switches in the Cockpit!!). I Think I'm on the Right Track. I think now I have to Add "const int ledPinG = 5;" and add this into "if" as "digitalWrite(ledPinG, LOW);" & I'll also have to change the Counter to 3?
My Question is Now can I add a second set of Functions to the "else" statement to allow for the Green to work?? Or a Second "Else" Statement??

if (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPinY, HIGH);
    digitalWrite(ledPinR, LOW);
    digitalWrite(ledPinG, LOW);
  } else {
   digitalWrite(ledPinR, HIGH);
   digitalWrite(ledPinY, LOW);
   digitalWrite(ledPinG, LOW);
   } else {
   digitalWrite(ledPinG, HIGH);
   digitalWrite(ledPinY, LOW);
   digitalWrite(ledPinR, LOW);
   }
  }

Based on My Experimenting so Far, I'll have to change the LED's around a little bit, Make Yellow the Red(Since it's the default Cockpit setting), but I'm flexible. I still need to Impliment it into the Micro to get the Keypress, But 1 step @ a Time.

A second else as you have it there makes no sense - as the compiler will tell you. A switch statement will do it though:

switch (buttonPushCounter % 3)
 {
 case 0: 
     digitalWrite(ledPinY, HIGH);
     digitalWrite(ledPinR, LOW);
     digitalWrite(ledPinG, LOW);
     break;
  case 1:
    digitalWrite(ledPinR, HIGH);
    digitalWrite(ledPinY, LOW);
    digitalWrite(ledPinG, LOW);
    break;
  case 2:
    digitalWrite(ledPinG, HIGH);
    digitalWrite(ledPinY, LOW);
    digitalWrite(ledPinR, LOW);
    break;
  }

If Red LED is supposed to be on by default, why not put this in void setup()?

    digitalWrite(ledPinR, HIGH);

wildbill said:

A second else as you have it there makes no sense - as the compiler will tell you. A switch statement will do it though:

Thank You for the required code. I wasn't sure How it should be written. As I said I'm TOTALLY NEW at this.

1ChicagoDave said:

If Red LED is supposed to be on by default, why not put this in void setup()?

Actually I was expecting it to be OFF when Started and was TOTALLY Surprised to see it ON! I didn't think it would be on until I started pressing the Key. I'll try adding that In.

Again, THANK YOU Both for your Assistance. :grin:

Another Question I have is Can I upload More than 1 sketch to the Arduino, that way I could add More switches & functions? Or would I have to ADD them in to this one?

Daves_not_here:
Another Question I have is Can I upload More than 1 sketch to the Arduino, that way I could add More switches & functions? Or would I have to ADD them in to this one?

You can only load one sketch at a time to an Arduino. If desired, you could have a toggle switch that says do action A or B. For example, you could have a switch that enables debug println statements that you can view on the serial monitor, and if the switch is off, don't do the printing.

You'd have to ADD them into this one. But, you can have multiple sensors/inputs (buttons, switches) monitored by Arduino, and have them trigger other outputs (lights, LEDs, etc) when "activated".

It can only do one thing at a time ( read an input, change the state of an output...from LOW to HIGH, for example). But, it only takes tiny fractions of a second to do each "thing". So, as long as you're not playing a melody or something, it will seem like it's multitasking.

Good luck!

  OK, Been Busy with Life, but tonight I got a chance to Tinker with this thing. Got it wired up, Re-Worked & 
Uploaded the Sketch....But I can Only get TWO of the LED's to Work. 
The RED LED is ON by Default, and when I Press the Switch the First Time, it Switches to the YELLOW LED. 
If I Press It a Second Time, The YELLOW Stays ON! Press it a Third Time & the RED LED comes Back On???? 
  I tried Switching the Case Statements Around, And I Can get the GREEN to Work as Default and it Swithes with the YELLOW,
 or YELLOW Default & RED switch. It Seems that Whichever LED is HIGH in the Case 1(Second in Order) is Default. 

[code */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int  ledPinR = 3;       // the pin that the RedLED is attached to
const int  ledPinY = 4;       // the pin that the YellowLED is attached to
const int  ledPinG = 5;       // the pin that the GreenLED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 
  pinMode(buttonPin, INPUT);    // initialize the button pin as a input:
  
  pinMode(ledPinR, OUTPUT);     // initialize the LEDs as an output:
  pinMode(ledPinY, OUTPUT);     // initialize the LEDs as an output:    
  pinMode(ledPinG, OUTPUT);     // initialize the LEDs as an output: 
  
  digitalWrite(ledPinR, HIGH);
  digitalWrite(ledPinY, LOW);
  digitalWrite(ledPinG, LOW);
  
 Serial.begin(9600);          // initialize serial communication:
 
}


void loop() {
                 
  buttonState = digitalRead(buttonPin);   // read the pushbutton input pin:

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  switch (buttonPushCounter % 3 == 0) 
  {
  case 0: 
     digitalWrite(ledPinY, HIGH);
     digitalWrite(ledPinR, LOW);
     digitalWrite(ledPinG, LOW);
     break;
  case 1:
    digitalWrite(ledPinR, HIGH);
    digitalWrite(ledPinG, LOW);
    digitalWrite(ledPinY, LOW);
    break;
  case 2:
    digitalWrite(ledPinG, HIGH);
    digitalWrite(ledPinY, LOW);
    digitalWrite(ledPinR, LOW);
    break;
  
  }
 
}


    
]

Not sure if I forgot something Somewhere???