Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

2
Help with how an array is captured.
Post Flair (click to view more posts with a particular flair)
Post Body

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
}

Author
Account Strength
70%
Account Age
3 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,989
Link Karma
1,529
Comment Karma
3,395
Profile updated: 1 week ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago