Help building a simple railway repeater.

Hi all,
I am in the process of trying to learn the arduino again as I have been given a task that I recon is perfect for the platform.
I want to take a morse code style button and take the input from that and have it mimicked on a bell on a delay.
I have used the sample code for a button to get the button and the buzzer to work together but I am a little on the lost side as far as taking the inputs and delaying them on the output side. For example say my button push sequence is long short very long short short, I will want the buzzer to do the same but delayed. I have tried the following code with a delay but that only delays the time before the button is reacted to.....

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);

I will keep playing and any assistance along the way will be much appreciated.
It will take me some time to learn to program but it will happen sometime in the :smiley:

You probably need a millis() based approach.

And store the durations of button presses (and possibly the durations how long the buttons were released in between) in an array first so they can be played later on the outputs.

Cool thanks gives me an idea of what code I need to learn to start playing with my project.

The state change (edge) detection tutorial shows how to detect the rising and falling edges of the pulses.

Cool thanks gives me an idea of what code I need to learn to start playing with my project.

Start here and if you need more explanation, there is plenty here and on the web.

Please note that the tutorial is -very- old, most or all Arduino boards have a led and resistor on pin 13, you won’t need what the picture shows.

The tutorial contains errors that you won’t see if you run it less than 25 days but be aware that all the variables you use for time should be unsigned integers, the millis() and micros() functions return type unsigned long values.

DO some work/play with the sketch (your IDE has a copy under File->Examples->Tutorials->section 2->BlinkWithoutDelay … you don’t need to type it in) and get some ideas if you want help since those ideas MAY let you ask better questions.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

I think my approach to this problem would be to save the value of millis() when the button is pressed and when it is released. Then you could calculate the duration of the button press and (if needed) the interval between presses and store the intervals in an array.

Later you could traverse the array to get the durations for each event.

This approach would also allow you to standardize the times if you want to.


Thanks for the info everyone I will have a play with some code when I am not sitting in front of the computer at 1am :smiley:

You will need to store however many press and release events as are likely possible in 2 seconds.

For that you should use an array. Have you gotten far enough in coding to know about arrays? Those are in section 5 of the Tutorial Examples loaded in your IDE and having a page on the main Arduino site.

If you change the BWOD sketch to always get the new blink interval from the array then the "hard part" will be in how to add and extract times on the go, try searching on the words circular buffer and the hard part will reward you with a technique you can use in all kinds of situations.

Take your time. If you can diagram or model how the data (press and release times from one to the next for example) gets moved/used before you code, it should help you not get lost in the code itself.