Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Does Processing read Serial port constantly?

$
0
0
    This is my Processing code
    1. import processing.serial.*;
    2. import ddf.minim.*;

    3. Serial port;
    4. Minim minim;
    5. AudioPlayer player;

    6. void setup()
    7. {
    8.  size(512, 200, P3D);
    9.  println("Available serial ports:");
    10.  println(Serial.list());  
    11.  
    12.   port = new Serial(this, Serial.list()[0], 19200);  
    13.   // we pass this to Minim so that it can load files from the data directory
    14.   minim = new Minim(this);
    15.   
    16.   // loadFile will look in all the same places as loadImage does.
    17.   // this means you can find files that are in the data folder and the 
    18.   // sketch folder. you can also pass an absolute path, or a URL.
    19.   player = minim.loadFile("espressomachine.mp3");
    20.   
    21.   // play the file
    22. }

    23. void draw()
    24. {
    25.   char inByte = port.readChar();
    26.     println("received char: "+ inByte);
    27.     if( inByte == '!' )
    28.     { 
    29.           player.play();         
    30.     }
    31. }

    32. void stop()
    33. {
    34.     super.stop();
    35. }
    This is my Arduino code:

    1. // If you're using Arduino 1.0 uncomment the next line:
    2. #include "SoftwareSerial.h"
    3. // If you're using Arduino 23 or earlier, uncomment the next line:
    4. //#include "NewSoftSerial.h"

    5. #include "Adafruit_Thermal.h"
    6. #include "trollface.h"
    7. #include "shots.h"
    8. #include <avr/pgmspace.h>

    9. const int buttonPin = 4;     // the number of the pushbutton pin
    10. const int ledPin =  13;      // the number of the LED pin
    11. int buttonState = 0;         // variable for reading the pushbutton status

    12. int printer_RX_Pin = 2;  // This is the green wire
    13. int printer_TX_Pin = 3;  // This is the yellow wire

    14. Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);


    15. void setup(){
    16.   Serial.begin(19200);
    17.   // initialize the LED pin as an output:
    18.   pinMode(ledPin, OUTPUT);      
    19.   // initialize the pushbutton pin as an input:
    20.   pinMode(buttonPin, INPUT);   
    21.   
    22. }

    23. void loop(){
    24.   // read the state of the pushbutton value:
    25.   buttonState = digitalRead(buttonPin);

    26.   // check if the pushbutton is pressed.
    27.   // if it is, the buttonState is HIGH:
    28.   if (buttonState == HIGH) {     
    29.     // turn LED on:    
    30.         // send the string "Knock!" back to the computer, followed by newline
    31.     digitalWrite(ledPin, HIGH); 
    32.    
    33.   printer.begin();
    34.   
    35.   printer.printBitmap(shots_width, shots_height, shots_data);
    36.   printer.justify('L');
    37.   printer.setLineHeight(50);
    38.   printer.boldOn();
    39.   printer.println("Welcome to Shots");
    40.   printer.println("The Machine is now making:");
    41.   printer.boldOff();
    42.   printer.setSize('L');
    43.   printer.println("Latte");
    44.   printer.println("Total: $6.50");
    45.   printer.setSize('S');
    46.   printer.setLineHeight(0);
    47.   printer.println("Please wait while our machine");
    48.   printer.println("does its thang.");
    49.   printer.feed(1);
    50.   
    51.   delay(100);
    52.   Serial.print("print!");
    53.   Serial.write(5);
    54.   delay(10000);
    55.      
    56.   printer.printBitmap(trollface_width, trollface_height, trollface_data);
    57.   printer.setLineHeight(50);
    58.   printer.println("Just kidding.");
    59.   printer.setSize('L');
    60.   printer.setLineHeight(0);
    61.   printer.println("April Fools'!");
    62.   printer.setSize('S');
    63.   printer.println("Redeem your free coffee!");
    64.   printer.feed(1);

    65.   printer.sleep();      // Tell printer to sleep
    66.   printer.wake();       // MUST call wake() before printing again, even if reset
    67.   printer.setDefault(); // Restore printer to defaults
    68.   
    69.   } 
    70.   else {
    71.     // turn LED off:
    72.     digitalWrite(ledPin, LOW);
    73.   }
    74. }

    Basically what I'm trying to do is for someone to press a button (read by arduino) and print something on a thermal printer and play a piece of music on the computer at the same time. So far it works for the 1st time, but when I press the button a second time, the serial prints, but Processing doesn't play the file again. I'm not sure which part of the code is going wrong. Would be super grateful for some help here!

    Viewing all articles
    Browse latest Browse all 1768

    Trending Articles