Skip to content

Instantly share code, notes, and snippets.

@pral2a
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pral2a/c523320f836d7ac6b8dd to your computer and use it in GitHub Desktop.
Save pral2a/c523320f836d7ac6b8dd to your computer and use it in GitHub Desktop.
Processing Talsk Arduino - Basic Commands Comunication
String inputString = ""; // a string to hold incoming data
boolean commandComplete = false; // whether the string is complete
int somethingGlobal = 100;
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// checkCommands and run
checkCommands();
}
void checkCommands() {
if (Serial.available()) {
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
commandComplete = true;
}
}
if (commandComplete) {
// not really optimize on strings but easier to learn
// get the first character of the string which is the commands
char command = inputString.substring(0,1).charAt(0);
// get the rest of the string which is the integer value
int value = inputString.substring(1).toInt();
// clean the string to receive more commands
inputString = "";
// set the commands complete to false
commandComplete = false;
// execute the received commands with the attached value
runCommand(command, value);
}
}
void runCommand(char commands, int value){
switch (commands) {
case 'A':
Serial.print("COMMAND A ");
Serial.println(value);
updateSomething(value);
break;
case 'B':
Serial.print("COMMAND B ");
Serial.println(value);
doSomething(value);
break;
case 'C':
/*
Here any function you want to run
*/
break;
}
}
void doSomething(int value){
delay(value);
}
void updateSomething(int value){
somethingGlobal = value;
}
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[5].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String[] portsAvailable = Serial.list(); // Get a List of all the serial ports available
for (int i=0; i < portsAvailable.length; i++) { // Iterate over the list and print them on console for selection
print("[" + i + "]\t");
println(portsAvailable[i]);
}
String portName = portsAvailable[5]; // "5" is our selection
myPort = new Serial(this, portName, 9600); // Connect to the specified port
}
void draw() {
sendCMD('A', 100); // Send Command A with value 100
}
public void sendCMD(char cmd, int value) {
print("SENDING CMD: ");
print(cmd);
print(" VALUE: ");
print(value);
print(" >> ");
print(cmd);
println(str(value));
myPort.write(cmd);
myPort.write(String.valueOf(value));
myPort.write('\n');
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if(myString != null) {
print("RECEIVD CMD: ");
println(myString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment