Call and Response Help
in
Integration and Hardware
•
2 years ago
Hi,
I'm trying to do a very simple call and response sketch with processing and arduino. All I want to do is have processing initiate contact with arduino via serial, get a response from arduino, and then every time arduino ask for more data, have processing send another integer and print it out.
My Processing code is:
- import processing.serial.*;
- Serial myPort;
- int inByte;
- void setup() {
- myPort = new Serial(this,Serial.list()[0], 9600);
- establishContact();
- }
- void draw() {
- //while(myPort.available() <= 0) {
- inByte = myPort.read();
- if(inByte == 'B') {
- for (int i=0; i<=5; i++) {
- myPort.write(i);
- }
- }
- }
- //}
- void establishContact() {
- myPort.write('A');
- delay(300);
- }
And the arduino code is:
- #include <NewSoftSerial.h>
- boolean firstContact = false;
- char inByte;
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- //while (Serial.available() > 0){
- char inByte = Serial.read();
- if(firstContact == false) {
- if (inByte == 'A') {
- //Serial.flush();
- firstContact = true;
- delay(300);
- Serial.write('B');
- }
- }
- else{
- int inByte = Serial.read();
- //char val = inByte;
- Serial.println(inByte);
- delay(500);
- Serial.write('B');
- }
- //}
- }
I have found Tom Igoe's example of Call and Response, but it seems to be going in the opposite direction I want. Tried playing with it to turn it around, but no luck. I'm sure it is a ver simple problem, but any help would be really great.
1