Does Processing read Serial port constantly?
in
Core Library Questions
•
6 months ago
- import processing.serial.*;
- import ddf.minim.*;
- Serial port;
- Minim minim;
- AudioPlayer player;
- void setup()
- {
- size(512, 200, P3D);
- println("Available serial ports:");
- println(Serial.list());
- port = new Serial(this, Serial.list()[0], 19200);
- // we pass this to Minim so that it can load files from the data directory
- minim = new Minim(this);
- // loadFile will look in all the same places as loadImage does.
- // this means you can find files that are in the data folder and the
- // sketch folder. you can also pass an absolute path, or a URL.
- player = minim.loadFile("espressomachine.mp3");
- // play the file
- }
- void draw()
- {
- char inByte = port.readChar();
- println("received char: "+ inByte);
- if( inByte == '!' )
- {
- player.play();
- }
- }
- void stop()
- {
- super.stop();
- }
- // If you're using Arduino 1.0 uncomment the next line:
- #include "SoftwareSerial.h"
- // If you're using Arduino 23 or earlier, uncomment the next line:
- //#include "NewSoftSerial.h"
- #include "Adafruit_Thermal.h"
- #include "trollface.h"
- #include "shots.h"
- #include <avr/pgmspace.h>
- const int buttonPin = 4; // the number of the pushbutton pin
- const int ledPin = 13; // the number of the LED pin
- int buttonState = 0; // variable for reading the pushbutton status
- int printer_RX_Pin = 2; // This is the green wire
- int printer_TX_Pin = 3; // This is the yellow wire
- Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);
- void setup(){
- Serial.begin(19200);
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- }
- 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 == HIGH) {
- // turn LED on:
- // send the string "Knock!" back to the computer, followed by newline
- digitalWrite(ledPin, HIGH);
- printer.begin();
- printer.printBitmap(shots_width, shots_height, shots_data);
- printer.justify('L');
- printer.setLineHeight(50);
- printer.boldOn();
- printer.println("Welcome to Shots");
- printer.println("The Machine is now making:");
- printer.boldOff();
- printer.setSize('L');
- printer.println("Latte");
- printer.println("Total: $6.50");
- printer.setSize('S');
- printer.setLineHeight(0);
- printer.println("Please wait while our machine");
- printer.println("does its thang.");
- printer.feed(1);
- delay(100);
- Serial.print("print!");
- Serial.write(5);
- delay(10000);
- printer.printBitmap(trollface_width, trollface_height, trollface_data);
- printer.setLineHeight(50);
- printer.println("Just kidding.");
- printer.setSize('L');
- printer.setLineHeight(0);
- printer.println("April Fools'!");
- printer.setSize('S');
- printer.println("Redeem your free coffee!");
- printer.feed(1);
- printer.sleep(); // Tell printer to sleep
- printer.wake(); // MUST call wake() before printing again, even if reset
- printer.setDefault(); // Restore printer to defaults
- }
- else {
- // turn LED off:
- digitalWrite(ledPin, LOW);
- }
- }
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!
1