Pages: [1]   Go Down
Author Topic: Sending a Pulse  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I hope someone can help me please.

Please bear with me as this is quite difficult to explain. smiley-confuse

I'm making a flight simulator control panel.
I need to use toggle switches to make the panel look realistic. But the electrical output from the toggle switch is different to what I need.

When I turn on one side of the toggle switch, I need to send a short signal to another board.
Then when I turn the toggle switch off again, I need it to send the same signal again.

Is that possible?

I hope that makes sense.

Thanks in advance.

Paul
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23748
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure - watch the switch for a change of state (lo to hi, hi to lo) and send a pulse when you see it.
Debounce the switch so you don't get multiple pulses as the switch bounces & settles.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thanks for the info.

I'm very new to all this so I did a bit of searching and I managed to cobble this together from a few different places.

Code:
/*
 Multiple State Change Test
   
 The circuit:
 * Pushbuttons attached to pins 2 and 3 from +5V
 * 10K resistosr attached to pins 2 and 3 from ground
 * LEDs attached to pins 10 and 11 to ground

*/

const int  buttonPin1 = 2;    // the pin that pushbutton 1 is attached to
const int  buttonPin2 = 3;    // the pin that pushbutton 2 is attached to
const int ledPin1 = 10;       // the pin that LED 1 is attached to
const int ledPin2 = 11;       // the pin that LED 2 is attached to

// Variables will change:
int buttonState1 = 0;         // current state of button 1
int buttonState2 = 0;         // current state of button 2
int lastButtonState1 = 0;     // previous state of button 1
int lastButtonState2 = 0;     // previous state of button 2

void setup() {
  // initialize the button pins as inputs
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the LEDs as outputs
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}


void loop() {
  buttonState1 = digitalRead(buttonPin1);  //Read the pushbutton input pin

  // Compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {  // if the state has changed,
     
   
    if (buttonState1 == HIGH) {      // If the button is high
      digitalWrite(ledPin1, HIGH);  // Turn LED 1 on
      delay(250);                   // Wait quarter of a second
      digitalWrite(ledPin1, LOW);   // Turn LED 1 off
    }
    else {                          // If the button is low
      digitalWrite(ledPin1, HIGH);  // Turn LED 1 on
      delay(250);                   // Wait quarter of a second
      digitalWrite(ledPin1, LOW);   // Turn LED 1 off
     
     
    }
  }
 
  lastButtonState1 = buttonState1;    // Save the current state of button 1 as the last state 1
 
    buttonState2 = digitalRead(buttonPin2);  //Read the pushbutton input pin

  // Compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {  // if the state has changed,
     
   
    if (buttonState2 == HIGH) {      // If the button is high
      digitalWrite(ledPin2, HIGH);  // Turn LED 2 on
      delay(250);                   // Wait quarter of a second
      digitalWrite(ledPin2, LOW);   // Turn LED 2 off
    }
    else {                          // If the button is low
      digitalWrite(ledPin2, HIGH);  // Turn LED 2 on
      delay(250);                   // Wait quarter of a second
      digitalWrite(ledPin2, LOW);   // Turn LED 2 off
     
     
    }
  }
 
  lastButtonState2 = buttonState2;    // Save the current state of button 2 as the last state 2
}

I put LEDs on the outputs for testing and it seems to work just how I want it to.

Thanks again for the help.

Paul
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 509
Posts: 31462
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need an arduino to do this you can achieve the same thing by using a circuit called a monostable. A 74LS123 has two of these circuits and costs about $0.40
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23748
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, but now he can add a few more, easily change pulse widths, flash a few LEDs, add control panel display and stick shaker simulator ...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the info Mike.

But as Crossroads eluded to, this is just the early stages of the project.
There are a lot more things I want it to do as well, a bit further down the line.

Much appreciated though.

Thanks
Paul
Logged

Brighton, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Colaboy,

You can simplify your code like this:

Code:

  // Compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {  // if the state has changed,
   
      digitalWrite(ledPin1, HIGH);  // Turn LED 1 on
      delay(250);                   // Wait quarter of a second
      digitalWrite(ledPin1, LOW);   // Turn LED 1 off

      lastButtonState1 = buttonState1;    // Save the current state of button 1 as the last state 1
  }
 

You had something like  if (X) do this, else do the same.

  Regards,
  Jim
Logged

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for that Jim.

Being new to programing, I tend to copy and paste bits of code from different places and just try to get them to work.

I'm all for trying to do it properly though.

Thanks again.

Paul
Logged

Pages: [1]   Go Up
Jump to: