Pages: [1]   Go Down
Author Topic: Master Arm Switch  (Read 702 times)
0 Members and 1 Guest are viewing this topic.
800 Knots @ 50 feet
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  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 (17.37 KB, 457x270 - viewed 15 times.)

* MASTER ARM CIRCUIT.JPG (155.91 KB, 1056x816 - viewed 33 times.)
« Last Edit: July 03, 2013, 12:00:27 am by Daves_not_here » Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

800 Knots @ 50 feet
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

jeffmorris wrote:
Quote
I think you forgot to connect ground to the Arduino Micro.

I Did....& Fixed
Logged

800 Knots @ 50 feet
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I started the Program by Modifying the "StateChangeDetection" Sketch in the Example folder.
 I added another Output 
Quote
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
Quote
digitalWrite(ledPinY, HIGH);
    digitalWrite(ledPinR, LOW);
and the "else" statement to
Quote
digitalWrite(ledPinR, HIGH);
   digitalWrite(ledPinY, LOW);

Thus I wound up with
Code:
*/

// 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??
 
Code:
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.   
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A second else as you have it there makes no sense - as the compiler will tell you. A switch statement will do it though:
Code:
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;
  }
Logged

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
   digitalWrite(ledPinR, HIGH);
Logged

800 Knots @ 50 feet
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wildbill said:
Quote
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:
Quote
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. smiley-mr-green

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?
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1835
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

800 Knots @ 50 feet
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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??? 
« Last Edit: July 11, 2013, 01:39:15 am by Daves_not_here » Logged

Pages: [1]   Go Up
Jump to: