i am struggeling with making a morse code decoder for a school project.
can anyone help me. A bit of info. the cuircet is containing of 2 buttons. one witch is a push button for typing in the morse code (long and short). the other button is for entering the morse code in the Serial monitor.
this is the code for my push buttons
//// SWITCH ////
const int buttonPin = 8; // switch pin
const int outPin = 13; // outputpin
const int IR = 6; // IR pin
const int Enter = 9; // Enter pin
int buttonStatePrevious = LOW; // previousstate of the switch
unsigned long minButtonLongPressDuration = 600; // Time we wait before we see the press as a long press
unsigned long buttonLongPressMillis; // Time in ms when we the button was pressed
bool buttonStateLongPress = false; // True if it is a long press
const int intervalButton = 50; // Time between two readings of the button state
unsigned long previousButtonMillis; // Timestamp of the latest reading
unsigned long buttonPressDuration; // Time the button is pressed in ms
//duration of the long and short
int Short = 200;
int Long = 600;
//// GENERAL ////
unsigned long currentMillis; // Variabele to store the number of milleseconds since the Arduino has started
void setup(){
Serial.begin(9600); // Initialise the serial monitor
pinMode(buttonPin, INPUT); // set buttonPin as input
pinMode(outPin, OUTPUT); // set outPin as OUTPUT
Serial.println("Press button");
}
// Function for reading the button state
void readButtonState() {
// If the difference in time between the previous reading is larger than intervalButton
if(currentMillis - previousButtonMillis > intervalButton) {
// Read the digital value of the button (LOW/HIGH)
int buttonState = digitalRead(buttonPin);
// If the button has been pushed AND
// If the button wasn't pressed before AND
// IF there was not already a measurement running to determine how long the button has been pressed
if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
buttonLongPressMillis = currentMillis;
buttonStatePrevious = HIGH;
//Serial.println("Button pressed");
}
// Calculate how long the button has been pressed
buttonPressDuration = currentMillis - buttonLongPressMillis;
// If the button is pressed AND
// If there is no measurement running to determine how long the button is pressed AND
// If the time the button has been pressed is larger or equal to the time needed for a long press
if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
buttonStateLongPress = true;
//Serial.println("Button long pressed");
Serial.print("-");
digitalWrite(outPin, HIGH);
tone(IR, 38000, Long);
delay(Long);
digitalWrite(outPin, LOW);
}
// If the button is released AND
// If the button was pressed before
if (buttonState == LOW && buttonStatePrevious == HIGH) {
buttonStatePrevious = LOW;
buttonStateLongPress = false;
//Serial.println("Button released");
// If there is no measurement running to determine how long the button was pressed AND
// If the time the button has been pressed is smaller than the minimal time needed for a long press
if (buttonPressDuration < minButtonLongPressDuration) {
//Serial.println("Button pressed shortly");
Serial.print(".");
digitalWrite(outPin, HIGH);
tone(IR, 38000, Short);
delay(Short);
digitalWrite(outPin, LOW);
}
}
// store the current timestamp in previousButtonMillis
previousButtonMillis = currentMillis;
}
if (Enter == HIGH){
delay(150);
Enter_morse();
}
}
void loop() {
currentMillis = millis(); // store the current time
readButtonState(); // read the button state
}
void Enter_morse(){
Serial.println("");
}