SerialCallResponseASCII

SerialCallResponseASCII - /* Serial Call and Response in...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /* Serial Call and Response in ASCII Language: Wiring/Arduino This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets some data in. Then it waits for a byte in the serial port, and sends three ASCII-encoded, comma-separated sensor values, truncated by a linefeed and carriage return, whenever it gets a byte in. Thanks to Greg Shakar and Scott Fitzgerald for the improvements The circuit: * potentiometers attached to analog inputs 0 and 1 * pushbutton attached to digital I/O 2 Created 26 Sept. 2005 by Tom Igoe Modified 4 Sep 2010 by Tom Igoe and Scott Fitzgerald This example code is in the public domain. http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII */ int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // digital sensor int inByte = 0; // incoming serial byte i void setup() { // start serial port at 9600 bps: Serial.begin(9600); pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); // read first analog input, divide by 4 to make the range 0-255: firstSensor = analogRead(A0)/4; // delay 10ms to let the ADC recover: delay(10); // read second analog input, divide by 4 to make the range 0-255: secondSensor = analogRead(1)/4; // read switch, map it to 0 or 255L thirdSensor = map(digitalRead(2), 0, 1, 0, 255); // send sensor values: Serial.print(firstSensor, DEC); Serial.print(","); Serial.print(secondSensor, DEC); Serial.print(","); Serial.println(thirdSensor, DEC); } } void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0,0"); // send an initial string delay(300); } } /* Processing code to run with this example: P // This example code is in the public domain. / import processing.serial.*; // import the Processing serial library Serial myPort; // The serial port S float bgcolor; // Background color float fgcolor; // Fill color float xpos, ypos; // Starting position of the ball void setup() { size(640,480); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino module, so I open Serial.list()[0]. // Change the 0 to the appropriate number of the serial port // that your microcontroller is attached to. myPort = new Serial(this, Serial.list()[0], 9600); // read bytes into a buffer until you get a linefeed (ASCII 10): myPort.bufferUntil('\n'); // draw with smooth edges: smooth(); } void draw() { background(bgcolor); fill(fgcolor); // Draw the shape ellipse(xpos, ypos, 20, 20); } // serialEvent method is run automatically by the Processing applet // whenever the buffer reaches the byte value set in the bufferUntil() // method in the setup(): / void serialEvent(Serial myPort) { // read the serial buffer: String myString = myPort.readStringUntil('\n');String myString = myPort....
View Full Document

Page1 / 23

SerialCallResponseASCII - /* Serial Call and Response in...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online