sending string variables to processing

Hello Arduino Forum!

I am in the process of hacking a rotary phone, using Arduino and Processing to convert the phone into a recording and playback device. It is a really interesting project and I have made some progress, but I am stuck. My function dialReader is not communicating properly through the serial port.

The full code is on a public gist: https://gist.github.com/lizzybrooks/b88531b906860d4fc4af

And here is my code snippet:

void dialReader(){ int initRead = digitalRead (INITPIN); // Is the wheel turning or not? static int lastValue = HIGH; // holds the last read from the pulse pin.

if (initRead == LOW) { // If the wheel is turning.... Serial.println("number"); Serial.flush(); int newValue = digitalRead (NUMPIN); // check the pulse pin. if (newValue != lastValue) { // if it's CHANGED from the last read... lastDebounceTime = millis(); // save its clock cycle; we need to check it. } // If enough time has passed (aka, it's not just a "bounce" from the // analog signal)... if ((millis() - lastDebounceTime) > debounceDelay) { // and if the current value is DIFFERENT than the one you just read... if (currentValue != newValue) { currentValue = newValue; // make them the same. if (newValue == 1) { // If you just set it to a 1... counter++; // it just finished a pulse, so add one to the counter. } } }

lastValue = newValue; // Your new value becomes the old one for comparison.

} else { // once the dial returns home and switches the initializing pin OFF, // you can be sure that there will be no more pulses coming. // "Counter" is the number dialed. You may now use it for whatever you want. // This is adjusted for the special case of "0" actually being 10 pulses. if (counter > 0) { if (counter == 10) { Serial.println (0); } else { Serial.println (counter); } } // After you're done using it, reset counter so it can start again on the // next dial. counter = 0; } }

If you can help me out, I so much appreciate it! I am a beginner coder, and eager to learn :)

Many thanks!! Lizzy

LizzyB:
My function dialReader is not communicating properly through the serial port.

Well, what does it do ?
What should it do ?

And please use the code button </> so your code looks like this

…R

Serial.println("number");

What good is it to send a string to Processing, when you send the SAME string every time?

Hi, thanks for the replies.

I am trying to send serial messages to Processing from a rotary phone. I attached the phone's dial wheel and handset switch to an Arduino Uno. The below code checks whether the handset is on or off the hook. If off the hook, it sends "dial tone" to Processing. If on the hook, it sends 'A' and lights up an LED in the Arduino. It also counts the dialed pulses and sends that number to Processing. Finally, if the wheel is spinning, it sends "number" to Processing to stop the dial tone sound.

The problem that I am having is that the pulse values from the dialReader function are not arriving in Processing while the hookReader function is running.

Thanks very much for your ideas.

/* Basic Digital Read and Rotary Phone Dial Reader
 * ------------------ 
 * This code reads whether the phone is on the hook by treating that hook like a button that is either open or depressed
AND
This sketch reads out the number dialed on a rotary phone dial.

The rotary dial has two signals: 
            1) turns ON when the wheel is turning
            2) pulses a count based on the number dialed.

The results are printed to the serial monitor.

 */
#define INITPIN 12
#define NUMPIN 11

int counter; // holds the pulse count for each dial spin
int currentValue = 0; 
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 5;    // the debounce time; increase if the output flickers

int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

int incomingByte = 0; //for incoming serial data

void setup(){
   Serial.begin(9600);
   
     pinMode(INITPIN, INPUT_PULLUP);
  pinMode(NUMPIN, INPUT_PULLUP);
   
  // configure the two inputs, and the onboard LED.
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare phone hook as input
}

void loop(){

   hookReader();
   dialReader();
  

}

void hookReader(){
   int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
  
  if (initRead != LOW) {
  
   val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, HIGH);  // turn LED OFF
    Serial.print('A');   // send a capital A
    delay(1);        // delay in between reads for stability
  
} else {
    digitalWrite(ledPin, LOW);  // turn LED ON
    Serial.println("dial tone");
  delay(1);        // delay in between reads for stability
  }
  }
}

void dialReader(){  
   int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
  static int lastValue = HIGH;  // holds the last read from the pulse pin.

  if (initRead == LOW) {  // If the wheel is turning....
  Serial.println("number");
  Serial.flush();
    int newValue = digitalRead (NUMPIN); // check the pulse pin.
    if (newValue != lastValue) { // if it's CHANGED from the last read...
      lastDebounceTime = millis(); // save its clock cycle; we need to check it.
    }
    // If enough time has passed (aka, it's not just a "bounce" from the 
    // analog signal)...
    if ((millis() - lastDebounceTime) > debounceDelay) { 
      // and if the current value is DIFFERENT than the one you just read...
      if (currentValue != newValue) { 
        currentValue = newValue; // make them the same.
        if (newValue == 1) { // If you just set it to a 1...
          counter++; // it just finished a pulse, so add one to the counter.
        }
      }
    }

    lastValue = newValue; // Your new value becomes the old one for comparison.

  } else {
// once the dial returns home and switches the initializing pin OFF,
// you can be sure that there will be no more pulses coming.
// "Counter" is the number dialed. You may now use it for whatever you want.
// This is adjusted for the special case of "0" actually being 10 pulses.
    if (counter > 0) {
      if (counter == 10) {
        Serial.println (0);
      } else {
        Serial.println (counter);
      }
    }
// After you're done using it, reset counter so it can start again on the
// next dial.
    counter = 0;
  }  
}

Can you get your code to produce the correct output on the Serial Monitor ? That way you can get your Arduino code working without worrying about problems in your Processing program.

When the Arduino is working properly you will know that any remaining problems are in your Processing code.

You might look at serial input basics. The same principles would be appropriate in Processing or any other language.

...R

It also counts the dialed pulses and sends that number to Processing.

Nonsense.

Here is the code:

  Serial.println("number");
  Serial.flush();

Regardless of what was counted, if anything, you send the string of letters "number" to Processing.

I'll ask again. Why?

@PaulS, here is where the arduino program sends the counted pulses:

if (counter > 0) { if (counter == 10) { Serial.println (0); } else { Serial.println (counter); }

It sends "number" while the wheel is spinning, to tell Processing to stop playing the dial tone track.