I was wandering if its possible to connect a simple on/off switch to an arduino nano, so the arduino would process it in a way to convert an input toggle to a output pulse each time the switch is switched either on or off.
Yes.
Have a look at the 'StateChangeDetection' example in the Arduino IDE.
Here is example code that will flash the built-in LED on pin 13 for 1/5 second on each button press. The button is wired to ground and pin 2 with the pin's internal pullup enabled. The code is modified from the state change for active low inputs tutorial.
/*
The circuit:
- pushbutton attached to pin 2 from ground
- the internal pullup on pin 2 is enabled
- LED attached from pin 13 to ground (or use the built-in LED on most
Arduino boards)
*/
// this constant won't change:
const byte buttonPin = 2; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
// 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 == LOW)
{
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
}
}
Crude debounce there!
Thanks, I guess?
Thanks! I'll try it when I get home. But I was wondering if the Arduino can works as a relay because I just need it to complete a circuit without supplying any power like it does for the LED on pin 13.
OK, so now you need to explain exactly what your proposed application is, what you want to connect it to, and why. You must give a Web link to the device and circuit to which you propose to connect it.
So basically I need to connect the Arduino nano to a keyboard circuit board that connects to a keyboard so when I flip a switch it presses a button once. It is basically like this guy did but with toggle switches: FAQ #231: Button box made from a keyboard
I don't have a web link to the keyboard circuit board but I only need to short the connection between two signals on the keyboard circuit.
In this example, pin 3 could be your "relay" (open drain output limited to 5V, 20mA):
#include <Toggle.h>
const byte buttonPin = 2;
const byte openDrainPin = 3;
const byte ledPin = LED_BUILTIN;
const byte blinkMs = 100; // blink and open drain period (ms) after button press or release
bool blinkState = false, lastBlinkState = false;
Toggle sw1(buttonPin);
void setup() {
sw1.begin(buttonPin);
pinMode(ledPin, OUTPUT);
}
void loop() {
sw1.poll();
blinkState = sw1.blink(blinkMs, 2); // blinks on press and release
if (blinkState && !lastBlinkState) { // rising edge
digitalWrite(ledPin, HIGH);
pinMode(openDrainPin, OUTPUT);
} else if (!blinkState && lastBlinkState) { // falling edge
digitalWrite(ledPin, LOW);
pinMode(openDrainPin, INPUT);
}
lastBlinkState = blinkState;
sw1.onPress();
sw1.onRelease();
}
Take a look at the pressCode()
function and example. Lots you can do (225 things) with just one button.
OK, you use a 74HC4066 Quad analog switch controlled by the Arduino.
Note the Arduino must be connected to the same power as the keyboard - but I think you knew that anyway because it just makes sense.
This code is perfect but the thing is I have 4 buttons that need 4 separate in and out pins on the Arduino and I'm still learning C#, so I was wondering what to change/duplicate to make it 4 buttons instead of 1.
This is what I did so far but I don't know if it's correct and I don't know how to change the last void loop bit of code.
Also, I don't know how to paste the code into the forum very well.
OK, so the 74HC4066 has exactly four such switches.
I dont have a 74HC4066
Suggest you go get one.
It will do the job you are asking.
The only other real alternative - not quite so easy - is to use four 2N7000 FETs.
But the code that was pasted above by dlloyd did the job the only problem is that I need it to do that to 4 separate switches which I am trying to fix now.
But thanks anyways for suggesting different solutions!
Also if i can't do it by code, i do have 4 2N7000s that i can use.
Four buttons, four open drain outputs:
#include "Toggle.h"
const byte buttonPin1 = 2;
const byte buttonPin2 = 3;
const byte buttonPin3 = 4;
const byte buttonPin4 = 5;
const byte openDrainPin1 = 6;
const byte openDrainPin2 = 7;
const byte openDrainPin3 = 8;
const byte openDrainPin4 = 9;
const byte ledPin = LED_BUILTIN;
const byte blinkMs = 100; // blink and open drain period (ms) after any button press or release
bool blinkState1 = false, lastBlinkState1 = false;
bool blinkState2 = false, lastBlinkState2 = false;
bool blinkState3 = false, lastBlinkState3 = false;
bool blinkState4 = false, lastBlinkState4 = false;
Toggle sw1(buttonPin1);
Toggle sw2(buttonPin2);
Toggle sw3(buttonPin3);
Toggle sw4(buttonPin4);
void setup() {
sw1.begin(buttonPin1);
sw2.begin(buttonPin2);
sw3.begin(buttonPin3);
sw4.begin(buttonPin4);
pinMode(ledPin, OUTPUT);
}
void loop() {
sw1.poll();
sw2.poll();
sw3.poll();
sw4.poll();
// first button, first open drain output
blinkState1 = sw1.blink(blinkMs, 2); // blinks on press and release
if (blinkState1 && !lastBlinkState1) { // rising edge
digitalWrite(ledPin, HIGH);
pinMode(openDrainPin1, OUTPUT);
} else if (!blinkState1 && lastBlinkState1) { // falling edge
digitalWrite(ledPin, LOW);
pinMode(openDrainPin1, INPUT);
}
lastBlinkState1 = blinkState1;
sw1.onPress();
sw1.onRelease();
// second button, second open drain output
blinkState2 = sw2.blink(blinkMs, 2); // blinks on press and release
if (blinkState2 && !lastBlinkState2) { // rising edge
digitalWrite(ledPin, HIGH);
pinMode(openDrainPin2, OUTPUT);
} else if (!blinkState2 && lastBlinkState2) { // falling edge
digitalWrite(ledPin, LOW);
pinMode(openDrainPin2, INPUT);
}
lastBlinkState2 = blinkState2;
sw2.onPress();
sw2.onRelease();
// third button, third open drain output
blinkState3 = sw3.blink(blinkMs, 2); // blinks on press and release
if (blinkState3 && !lastBlinkState3) { // rising edge
digitalWrite(ledPin, HIGH);
pinMode(openDrainPin3, OUTPUT);
} else if (!blinkState3 && lastBlinkState3) { // falling edge
digitalWrite(ledPin, LOW);
pinMode(openDrainPin3, INPUT);
}
lastBlinkState3 = blinkState3;
sw3.onPress();
sw3.onRelease();
// fourth button, fourth open drain output
blinkState4 = sw4.blink(blinkMs, 2); // blinks on press and release
if (blinkState4 && !lastBlinkState4) { // rising edge
digitalWrite(ledPin, HIGH);
pinMode(openDrainPin4, OUTPUT);
} else if (!blinkState4 && lastBlinkState4) { // falling edge
digitalWrite(ledPin, LOW);
pinMode(openDrainPin4, INPUT);
}
lastBlinkState4 = blinkState4;
sw4.onPress();
sw4.onRelease();
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.