Commit e00a8c10 authored by Evan Raskob's avatar Evan Raskob

some bug fixed, copyright additions, and further instructions after 1st lesson

parent 81c66e85
...@@ -51,7 +51,6 @@ void setupGUI() { ...@@ -51,7 +51,6 @@ void setupGUI() {
; ;
//Serial Port selector //Serial Port selector
// serialddl = cp5.addDropdownList("SerialPort")
serialddl = cp5.addScrollableList("SerialPort") serialddl = cp5.addScrollableList("SerialPort")
.setPosition(50, 100) .setPosition(50, 100)
.setSize(200, 200) .setSize(200, 200)
......
...@@ -6,11 +6,16 @@ import processing.serial.*; ...@@ -6,11 +6,16 @@ import processing.serial.*;
/* /*
* This example reads sensor data from an Arduino * This example reads sensor data from an Arduino
* and relays it to another program using OSC. * and relays it to another program using OSC.
* Try changing the TEST_ON and TEST_OFF functions to send some serial data to an Arduino (or other microcontroller) and
* test how that works. Then, in the Serial_functions tab, edit the serialEvent() function to handle some
* incoming serial messages from the Arduino.
* *
* by Evan Raskob (e.raskob@gold.ac.uk) * copyright 2019 Evan Raskob (e.raskob@gold.ac.uk)
* Jan. 29, 2019
* MIT Licensed. Use at your own risk.
*/ */
// whether we're connected to an Arduino or not:
boolean applicationRunning = false; boolean applicationRunning = false;
//start everything //start everything
...@@ -50,8 +55,14 @@ public void STOP() ...@@ -50,8 +55,14 @@ public void STOP()
public void TEST_ON() public void TEST_ON()
{ {
if (applicationRunning) if (applicationRunning)
{ {
// Whatever is in here gets run when the TEST_ON button is pressed.
// the following works with the SerialSendReceiveTest sketch for Arduino to turn the LEDs on and off
serial.write("ledon" + stopChar); serial.write("ledon" + stopChar);
// this one works with the SerialParseTest Arduino sketch:
// serial.write("n 1,0,0" + stopChar);
} }
println("finished"); println("finished");
} }
...@@ -60,6 +71,10 @@ public void TEST_OFF() ...@@ -60,6 +71,10 @@ public void TEST_OFF()
{ {
if (applicationRunning) if (applicationRunning)
{ {
// Whatever is in here gets run when the TEST_OFF button is pressed.
// the following works with the SerialSendReceiveTest sketch for Arduino to turn the LEDs on and off
serial.write("ledoff" + stopChar); serial.write("ledoff" + stopChar);
} }
println("finished"); println("finished");
...@@ -73,7 +88,6 @@ public void ERROR() ...@@ -73,7 +88,6 @@ public void ERROR()
serial.write("bad data" + stopChar); serial.write("bad data" + stopChar);
// could also leave out the stopChar and see what happens! // could also leave out the stopChar and see what happens!
} }
} }
/************************************************************************************ /************************************************************************************
......
...@@ -32,6 +32,9 @@ void serialEvent(Serial serial) { ...@@ -32,6 +32,9 @@ void serialEvent(Serial serial) {
String[] msgs = split(input, ','); String[] msgs = split(input, ',');
// "c,2,3,4" --> ["c", "2", "3", "4"]
// TODO: handle message here! // TODO: handle message here!
} }
......
...@@ -11,7 +11,7 @@ int z; ...@@ -11,7 +11,7 @@ int z;
void setup() { void setup() {
// this is the string we're reading from. It's a few values separated by commas // this is the string we're reading from. It's a few values separated by commas
const char buf[] = "N 90,80,180"; const char buf[] = "N 1,2,3";
// sscanf reads the values and stores them in variables we just declared. %d represents a number of digits to read // sscanf reads the values and stores them in variables we just declared. %d represents a number of digits to read
int n = sscanf(buf, "%c %d,%d,%d", &c, &x, &y, &z); int n = sscanf(buf, "%c %d,%d,%d", &c, &x, &y, &z);
...@@ -72,8 +72,18 @@ void loop() { ...@@ -72,8 +72,18 @@ void loop() {
// now handle bytes read // now handle bytes read
// NXXX means note // NXXX means note
// sscanf reads the values and stores them in variables we just declared. %d represents a number of digits to read // sscanf reads the values and stores them in variables we just declared. %d represents a number of digits to read
// the "n" returned by this function is the number of matches. Whitespace is ignored, by default.
int n = sscanf(buffer, "%c %d,%d,%d", &c, &x, &y, &z); int n = sscanf(buffer, "%c %d,%d,%d", &c, &x, &y, &z);
//-----------------------------------------------------------------------------------------------
// PUT YOUR CODE HERE! Make it do something, like turn on a NEOPixel array to a colour (r,g,b).
// do something like:
// if (c == 'l') {
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, x);
// }
Serial.print(c); Serial.print(c);
Serial.print(' '); Serial.print(' ');
Serial.print(x); Serial.print(x);
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
* Maybe you just want to turn somethings on and off? Or control a motor speed? * Maybe you just want to turn somethings on and off? Or control a motor speed?
* *
* Then, what data do you want from it? And how often? * Then, what data do you want from it? And how often?
* copyright Evan Raskob (e.raskob@gold.ac.uk) Jan. 29, 2019
* MIT Licensed.
*/ */
const char stopChar = '\n'; //new line character const char stopChar = '\n'; //new line character
...@@ -15,7 +17,6 @@ const long timeoutMs = 400; // timeout if message took too long ...@@ -15,7 +17,6 @@ const long timeoutMs = 400; // timeout if message took too long
const char LEDONCommand[] = "ledon"; const char LEDONCommand[] = "ledon";
const char LEDOFFCommand[] = "ledoff"; const char LEDOFFCommand[] = "ledoff";
long bytesReceived = 1;
/** /**
* Blink an LED * Blink an LED
...@@ -102,8 +103,6 @@ void loop() ...@@ -102,8 +103,6 @@ void loop()
// stop if we've taken too much time! // stop if we've taken too much time!
if (millis() - startTime > timeoutMs) if (millis() - startTime > timeoutMs)
{ {
bytesToRead = 0;
// flash error lights // flash error lights
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
...@@ -143,7 +142,7 @@ void loop() ...@@ -143,7 +142,7 @@ void loop()
Serial.println(':'); Serial.println(':');
// now handle bytes read // now handle bytes read
handleMessage(messageReceived); handleMessage(buffer);
charsRead = 0; // go back to beginning of buffer for next command charsRead = 0; // go back to beginning of buffer for next command
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment