#include "SevSeg.h"
#include "pitches.h"
SevSeg sevseg;
const int buttonPin = 11; // Change to the actual pin your button is connected to
const int ledPin = 10; // Change to the actual pin your LED is connected to
int buttonState = 0; // variable for reading the pushbutton status
#define BUZZER_PIN 12
int melody[] = {
NOTE_E5, NOTE_D5, NOTE_FS4, NOTE_GS4,
NOTE_CS5, NOTE_B4, NOTE_D4, NOTE_E4,
NOTE_B4, NOTE_A4, NOTE_CS4, NOTE_E4,
NOTE_A4
};
int durations[] = {
8, 8, 4, 4,
8, 8, 4, 4,
8, 8, 4, 4,
2
};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
byte numDigits = 1;
byte digitPins[] = {};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
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 == LOW) {
digitalWrite(ledPin, HIGH);
// Button is pressed, run pi_exchange_test()
int fract_area = pi_exchange();
digitalWrite(ledPin, LOW);
sevseg.setNumber(fract_area);
sevseg.refreshDisplay();
tune();
// Add a delay to debounce the button
delay(200); // You can adjust the delay time as needed
}
}
int pi_exchange() {
int fract_area;
// Button is pressed, send the exchange_string to the Pi
Serial.println("exchange_string");
// Wait for the response from the Pi
while (Serial.available() == 0) {
delay(10);
}
// Read input from Serial and handle errors
while (true) {
if (Serial.available() > 0) {
String userInput = Serial.readStringUntil('\n');
// Attempt to convert the user input to an integer
if (isdigit(userInput.charAt(0))) {
fract_area = userInput.toInt();
break; // Exit the loop if conversion is successful
} else {
// Prompt user again for valid input
Serial.println("Invalid input. Please enter a valid integer.");
}
}
}
return fract_area;
}
void tune()
{
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++) {
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);
//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing:
noTone(BUZZER_PIN);
}
}