This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have code I will provide below. I have connected a rotary pulse dial phone to my Arduino. I have sample code I've been working on with ChatGPT. Not a terrific partner but I'm not skilled at this.
I am successfully able to get it to recognize the pulses. I've also been successful in getting it to wait for a pause in dialing (2 seconds) in order to collect an array of numbers. I dial "4, 3, 5" and I want it to print the array to serial on pause.
This sample nearly works. Instead of printing "4, 3, 5" though it prints the digits in numerical order "3, 4, 5". My goal is for me to be able to dial a specific combination and have the Arduino take an action. Such as lighting an LED for 2 seconds after recognizing the array.
I would appreciate help in getting it to store the array correctly. I can then try to move onto making it take an action.
Sample output after dialing "3, 2, 6" :
17:15:49.190 -> 2 3 6
const int dialPin = 2; // Connect the wire from the dial to pin 2
int dialState = LOW; // Variable to track the dial state
int lastDialState = LOW; // Variable to track the previous dial state
int pulseCount = 0; // Counter for the number of pulses
unsigned long lastPulseTime = 0;
unsigned long pulseTimeout = 1000; // Adjust the timeout as needed (in milliseconds)
unsigned long debounceDelay = 200; // Increase the debounce delay to prevent rapid counts
int digits[10]; // Array to store digits (up to 10 digits)
void setup() {
pinMode(dialPin, INPUT_PULLUP); // Using INPUT_PULLUP to enable the internal pull-up resistor
Serial.begin(9600);
}
void loop() {
dialState = digitalRead(dialPin); // Read the state of the dial pin
// Check if the dial state has changed and is valid (after debounce)
if (dialState != lastDialState) {
delayMicroseconds(debounceDelay); // Increase the debounce delay
dialState = digitalRead(dialPin); // Recheck the dial state
if (dialState != lastDialState) {
if (dialState == LOW) {
pulseCount ;
lastPulseTime = millis();
}
}
}
// Check for a timeout before reporting the total pulse count
if (millis() - lastPulseTime >= pulseTimeout) {
if (pulseCount > 0) {
// Store the digit in the array if it's less than 10 digits
if (pulseCount <= 10) {
digits[pulseCount - 1] = pulseCount;
}
pulseCount = 0; // Reset the pulse count for the next dialing sequence
}
}
// Check for a 2-second delay before printing the digits if any digits are stored
if (millis() - lastPulseTime >= 2000) {
bool digitsStored = false;
for (int i = 0; i < 10; i ) {
if (digits[i] != 0) {
digitsStored = true;
break;
}
}
if (digitsStored) {
for (int i = 0; i < 10; i ) {
if (digits[i] != 0) {
Serial.print(digits[i]);
Serial.print(" ");
}
}
Serial.println(); // Print a newline
// Reset the digits array
for (int i = 0; i < 10; i ) {
digits[i] = 0;
}
}
}
lastDialState = dialState; // Save the last dial state
}
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/arduino/com...