source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/04.Communication/SerialCallResponseASCII/SerialCallResponseASCII.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 8.9 KB
Line 
1/*
2  Serial Call and Response in ASCII
3 Language: Wiring/Arduino
4
5 This program sends an ASCII A (byte of value 65) on startup
6 and repeats that until it gets some data in.
7 Then it waits for a byte in the serial port, and
8 sends three ASCII-encoded, comma-separated sensor values,
9 truncated by a linefeed and carriage return,
10 whenever it gets a byte in.
11
12 Thanks to Greg Shakar and Scott Fitzgerald for the improvements
13
14  The circuit:
15 * potentiometers attached to analog inputs 0 and 1
16 * pushbutton attached to digital I/O 2
17
18
19
20 Created 26 Sept. 2005
21 by Tom Igoe
22 modified 24 Apr 2012
23 by Tom Igoe and Scott Fitzgerald
24
25 This example code is in the public domain.
26
27 http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII
28
29 */
30
31int firstSensor = 0;    // first analog sensor
32int secondSensor = 0;   // second analog sensor
33int thirdSensor = 0;    // digital sensor
34int inByte = 0;         // incoming serial byte
35
36void setup() {
37  // start serial port at 9600 bps and wait for port to open:
38  Serial.begin(9600);
39  while (!Serial) {
40    ; // wait for serial port to connect. Needed for native USB port only
41  }
42
43
44  pinMode(2, INPUT);   // digital sensor is on digital pin 2
45  establishContact();  // send a byte to establish contact until receiver responds
46}
47
48void loop() {
49  // if we get a valid byte, read analog ins:
50  if (Serial.available() > 0) {
51    // get incoming byte:
52    inByte = Serial.read();
53    // read first analog input:
54    firstSensor = analogRead(A0);
55    // read second analog input:
56    secondSensor = analogRead(A1);
57    // read  switch, map it to 0 or 255L
58    thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
59    // send sensor values:
60    Serial.print(firstSensor);
61    Serial.print(",");
62    Serial.print(secondSensor);
63    Serial.print(",");
64    Serial.println(thirdSensor);
65  }
66}
67
68void establishContact() {
69  while (Serial.available() <= 0) {
70    Serial.println("0,0,0");   // send an initial string
71    delay(300);
72  }
73}
74
75
76/*
77Processing code to run with this example:
78
79// This example code is in the public domain.
80
81import processing.serial.*;     // import the Processing serial library
82Serial myPort;                  // The serial port
83
84float bgcolor;                  // Background color
85float fgcolor;                  // Fill color
86float xpos, ypos;               // Starting position of the ball
87
88void setup() {
89  size(640,480);
90
91  // List all the available serial ports
92 // if using Processing 2.1 or later, use Serial.printArray()
93  println(Serial.list());
94
95  // I know that the first port in the serial list on my mac
96  // is always my  Arduino module, so I open Serial.list()[0].
97  // Change the 0 to the appropriate number of the serial port
98  // that your microcontroller is attached to.
99  myPort = new Serial(this, Serial.list()[0], 9600);
100
101  // read bytes into a buffer until you get a linefeed (ASCII 10):
102  myPort.bufferUntil('\n');
103
104  // draw with smooth edges:
105  smooth();
106}
107
108void draw() {
109  background(bgcolor);
110  fill(fgcolor);
111  // Draw the shape
112  ellipse(xpos, ypos, 20, 20);
113}
114
115// serialEvent  method is run automatically by the Processing applet
116// whenever the buffer reaches the  byte value set in the bufferUntil()
117// method in the setup():
118
119void serialEvent(Serial myPort) {
120  // read the serial buffer:
121  String myString = myPort.readStringUntil('\n');
122  // if you got any bytes other than the linefeed:
123    myString = trim(myString);
124
125    // split the string at the commas
126    // and convert the sections into integers:
127    int sensors[] = int(split(myString, ','));
128
129    // print out the values you got:
130    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
131      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
132    }
133    // add a linefeed after all the sensor values are printed:
134    println();
135    if (sensors.length > 1) {
136      xpos = map(sensors[0], 0,1023,0,width);
137      ypos = map(sensors[1], 0,1023,0,height);
138      fgcolor = sensors[2];
139    }
140    // send a byte to ask for more data:
141    myPort.write("A");
142  }
143
144*/
145
146/*
147
148Max/MSP version 5 patch to run with this example:
149----------begin_max5_patcher----------
1503640.3oc6cs0jZajE94Y9UzKkeHoVloTeSHkm1II0VkeHIthSs6C1obIjZ.E
151KjHRhY7jT4+9d5KBj.jTCAXfoV6x.sj5VmyW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GQ5wBx47o.Ssw+CkcgQOmud
221KZic4QKzCw+7ROm8nY2LfMsEDtdfeMKSn5Ev95IQhorcqJcBrzPsQUhRNe8M
2221X6lhOezC4Bidv1nKcFs8YimJ9n8RWZXiO7aSCxDRLdjd91qU5TnmXCeRvmR
2239jnm7b15RmJ9rO4Kr+IgO04BfczyOpqx9npzofOsIlaR8Mo0IUMR48i0mYly
224lVMwlw6gbloGRezy4yKEw6BHBBWik.eRi3DNM5KDahS.SOE1EjmXl7Uyqo9T
225AtQAO8fG3oLX3cZFxKh0FLNSRfDaoG74gdvW.ZDU9FMGSdFMBt+IQh.6eIvw
226FujTkJREGKKcJ3X2WtXf7Ub1HywEqxh2tJnE.FcZhMByrcXQw1x+bOWJYjpy
227lv8oq55aEHLcwD8hJjxbVU5EigcNtL7Ql76KVVp69Huhcb87vpoCkRYT+96v
228Hd5Ay1rofMqm+FkLYvv0+GL3FkL6bLp21kL6QFNV8BNM48foWBV4zt1wXm5V
2294jkNEbL45dtNw13Iltmi9sAyY0S0l8BR+3yWjVXax7eOmKrp4m0QKIal6VYo
230SAf5XQxSrCa5l0qk45k5kAzqEgMNgzkz9FmL5abpnu4IhNzZ+0s+OKCSg0.
231-----------end_max5_patcher-----------
232
233*/
Note: See TracBrowser for help on using the repository browser.