I've just started using Ardunio for a project at University. I am a designer and have no previous experience with electronics so go easy on the techno language.
I am trying to write a piece of code for a push button switch.
My design intent is that if the button is pressed and held for 5 seconds it activates the next section of code. However, if the button is released before 5 seconds nothing happens.
You don't call a function with it's return type specified (void loop();), and there is no reason to call loop() from inside of loop(). At the end of the function, loop() will be called again.
If you delay() for one second after you discover that the switch was pressed, you loose any ability to discover that the switch was released and pressed again (like by someone waiting for an elevator that presses the up button repeatedly, as though that would hurry the elevator).
So, unfortunately, you are not going in the right direction.
The project is a simulated User Interface for a simplified mobile phone concept.
To the user there is only one button that when held for 5 seconds makes a phone call (or in the case of the prototype plays back a pre recording from a separate circuit which requires a HIGH output to trigger the playback).
The user can also rotate a dial which selects a caller (from 3), behind this dial are 3 reed switches. So I need the code to know which switch of the 3 hidden) is selected at the time of pressing the main switch (visible to the user) for 5 seconds.
I am using an Ardunio Uno board and have a CD4016 quad bilateral switch to convert the digital outputs into analogue outputs triggering 3 pre different pre recorded conversations.
PaulS -
What are button libraries and where might I find them?
This is the overall aim of the project. Does anyone have suggestions about the best way to go about doing this?
From the users perspective:
One push button
makes phone call when held for 5 seconds, doesn't make call if held for less.
while held an LED is constantly on as a warning that a call is being made.
One dial
3 call number options
whichever is selected at the time of pressing the push button for 5 seconds will be played back to the user.
From our perspective:
One PBS - when held for 5 seconds activates a switch on a secondary circuit that plays back recording (3 possible, 3 corresponding switches), when not held for the full 5 seconds, resets.
3 Reed switches (behind dial)
Give either a HIGH or LOW output and tell us which recording to play back when the PBS is held for 5 seconds.
If anyone could suggest some code layout for this or existing examples I would be most grateful.
KE7- I've checked out your link to the debounce coding pages.
This seems exactly what I need to do - ask the arduino to remember what time the button was first pressed and then as it to go to the next piece of code if when asked again the time is greater than 5 seconds.
However I am unsure of how to do this and the examples given were no help. Here is my code and it is the "masterSwitch11" INPUT that needs to be measued.
Any guidance on which parts of the debounce code are relevant and what to apply it to would be grand.
Thanks
Tom
//Constants won't change. They're used here to set pin numbers:
const int Reedswitch2 = 2;
const int Reedswitch3 = 3;
const int Reedswitch4 = 4;
// const int LED5 = 11;
const int Playpin8 = 8;
const int Playpin9 = 9;
const int Playpin10 = 10;
int masterSwitch11 = 11;
int current; // Current state of the button i.e. LOW/NOT PRESSED
int count; // How long the button was held i.e. PRESSED
byte previous = HIGH;
unsigned long firstTime; // How long since the button was first pressed
// ---------------------------------------------------------------------
void setup() {
pinMode(masterSwitch11, INPUT); // main button
pinMode(Reedswitch2, INPUT); // reed switch2
pinMode(Reedswitch3, INPUT); // reed switch3
pinMode(Reedswitch4, INPUT); // reed switch4
pinMode(Playpin8, OUTPUT); // play pin 8
pinMode(Playpin9, OUTPUT); // play pin 9
pinMode(Playpin10, OUTPUT); // play pin 10
// pinMode(LED5, OUTPUT); // LED
}
// ---------------------------------------------------------------------
void loop() {
goto switchon; //sends programme to: switchon
switchon:
// if (digitalRead(masterSwitch11) == HIGH) {
// digitalWrite(LED5, HIGH);
[color=red]// THIS IS WHERE THE BUTTON NEEDS TO BE MEASURED FOR HOW LONG IT HAS BEEN PRESSED AND IF ITS 5 SECONDS DO WHAT IS STATED BELOW[/color]
goto switchchoice;
else goto switchon;
switchchoice:
if (digitalRead(Reedswitch2) == HIGH) {
digitalWrite(Playpin8, HIGH);
delay (1000);
digitalWrite(Playpin8, LOW); //simulates a single press and removes the "2nd unpress" function - this could be useful however for stopping playback?
}
else {
digitalWrite(Playpin8, LOW);
}
if (digitalRead(Reedswitch3) == HIGH) {
digitalWrite(Playpin9, HIGH);
delay (1000);
digitalWrite(Playpin9, LOW); //simulates a single press and removes the "2nd unpress" function - this could be useful however for stopping playback?
}
else {
digitalWrite(Playpin9, LOW);
}
if (digitalRead(Reedswitch4) == HIGH) {
digitalWrite(Playpin10, HIGH);
delay (1000);
digitalWrite(Playpin10, LOW); //simulates a single press and removes the "2nd unpress" function - this could be useful however for stopping playback?
}
else {
digitalWrite(Playpin10, LOW);
}
}
If you are a beginner, please forget that the goto command exists. You are not using it the way it should be used, and there is no reason for you to be using it at all.
Think about how YOU would do what you need to do, with just a switch, a stopwatch that you can't reset, and a piece of paper.
The stopwatch that you can't reset corresponds to the millis() function. The switch obviously is the physical switch, and the paper corresponds to the Arduino memory (and variables where you store information).
Try again, without the goto statement, after giving some thought as to how YOU would do it.