Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Last active October 14, 2019 19:38
Show Gist options
  • Save futureshocked/52d756791d46efb7c1b7fd59acaf22ca to your computer and use it in GitHub Desktop.
Save futureshocked/52d756791d46efb7c1b7fd59acaf22ca to your computer and use it in GitHub Desktop.
10.2. Arduino UART receive/transmit ASCII character and decode with an oscilloscope demo sketch (Oscilloscopes for Busy People)
/* 11.2. Arduino UART receive/transmit ASCII character and decode with an oscilloscope demo sketch
*
* Use this sketch to experiment with the decoding of an ASCII character
* that is received and transmitted by an Arduino on an oscilloscope.
*
* Use in tandem with the matching Processing sketch
*
* Connect CH1 of the oscilloscope to pin 0 of the Arduino.
* Connect CH2 of the oscilloscope to pin 1 of the Arduino.
*
*
* This sketch was written for Oscilloscopes for Busy People by Peter Dalmaris.
* Find out more at https://techexplorations.com/courses/oscilloscopes/
*
* Components
* ----------
* - Arduino Uno
* - An oscilloscope (of course)
*
* Libraries
* ---------
* - NONE
*
* Connections
* -----------
*
*
*
* Other information
* -----------------
* -
* Created on September 12 2019 by Peter Dalmaris
*
*/
byte incomingByte = 0; // for incoming serial data
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600,SERIAL_8N1);
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.write(incomingByte); // This is the data we'll send to the PC. We'll include it in a single packet.
// Serial.write(0x0A); // Use hex 0A as the packet end. This can be picked up by the Rigol.
// Alternative packet end bytes are 0x20 (space), 0x0D (CR), 0x0A (LF), FF, and 0x00 (NULL)
}
}
import processing.serial.*;
Serial myPort; // The serial port
int whichKey = -1; // Variable to hold keystoke values
int inByte = -1; // Incoming serial data
void setup() {
size(400, 300);
// create a font with the third font available to the system:
PFont myFont = createFont(PFont.list()[2], 14);
textFont(myFont);
// List all the available serial ports:
printArray(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// In Windows, this usually opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[7];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(0);
text("Last Received (hex): " + hex(inByte), 10, 100);
text("Last Received (dec): " + inByte, 10, 120);
text("Last Received (char): " + char(inByte), 10, 140);
text("Last Sent (hex): " + hex(whichKey), 10, 160);
text("Last Sent (dec): " + whichKey, 10, 180);
text("Last Sent (char): " + char(whichKey), 10, 200);
}
void serialEvent(Serial myPort) {
inByte = myPort.read();
}
void keyPressed() {
// Send the keystroke out:
myPort.write(key);
whichKey = key;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment