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

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

Adding new version

File size: 9.6 KB
Line 
1/*
2  Serial Call and Response
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 sensor values whenever it gets a byte in.
9
10 Thanks to Greg Shakar and Scott Fitzgerald for the improvements
11
12   The circuit:
13 * potentiometers attached to analog inputs 0 and 1
14 * pushbutton attached to digital I/O 2
15
16 Created 26 Sept. 2005
17 by Tom Igoe
18 modified 24 April 2012
19 by Tom Igoe and Scott Fitzgerald
20
21 This example code is in the public domain.
22
23 http://www.arduino.cc/en/Tutorial/SerialCallResponse
24
25 */
26
27int firstSensor = 0;    // first analog sensor
28int secondSensor = 0;   // second analog sensor
29int thirdSensor = 0;    // digital sensor
30int inByte = 0;         // incoming serial byte
31
32void setup() {
33  // start serial port at 9600 bps:
34  Serial.begin(9600);
35  while (!Serial) {
36    ; // wait for serial port to connect. Needed for native USB port only
37  }
38
39  pinMode(2, INPUT);   // digital sensor is on digital pin 2
40  establishContact();  // send a byte to establish contact until receiver responds
41}
42
43void loop() {
44  // if we get a valid byte, read analog ins:
45  if (Serial.available() > 0) {
46    // get incoming byte:
47    inByte = Serial.read();
48    // read first analog input, divide by 4 to make the range 0-255:
49    firstSensor = analogRead(A0) / 4;
50    // delay 10ms to let the ADC recover:
51    delay(10);
52    // read second analog input, divide by 4 to make the range 0-255:
53    secondSensor = analogRead(1) / 4;
54    // read  switch, map it to 0 or 255L
55    thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
56    // send sensor values:
57    Serial.write(firstSensor);
58    Serial.write(secondSensor);
59    Serial.write(thirdSensor);
60  }
61}
62
63void establishContact() {
64  while (Serial.available() <= 0) {
65    Serial.print('A');   // send a capital A
66    delay(300);
67  }
68}
69
70/*
71Processing sketch to run with this example:
72
73// This example code is in the public domain.
74
75import processing.serial.*;
76
77int bgcolor;                         // Background color
78int fgcolor;                         // Fill color
79Serial myPort;                       // The serial port
80int[] serialInArray = new int[3];    // Where we'll put what we receive
81int serialCount = 0;                 // A count of how many bytes we receive
82int xpos, ypos;                      // Starting position of the ball
83boolean firstContact = false;        // Whether we've heard from the microcontroller
84
85void setup() {
86  size(256, 256);  // Stage size
87  noStroke();      // No border on the next thing drawn
88
89  // Set the starting position of the ball (middle of the stage)
90  xpos = width/2;
91  ypos = height/2;
92
93  // Print a list of the serial ports for debugging purposes
94  // if using Processing 2.1 or later, use Serial.printArray()
95  println(Serial.list());
96
97  // I know that the first port in the serial list on my mac
98  // is always my  FTDI adaptor, so I open Serial.list()[0].
99  // On Windows machines, this generally opens COM1.
100  // Open whatever port is the one you're using.
101  String portName = Serial.list()[0];
102  myPort = new Serial(this, portName, 9600);
103}
104
105void draw() {
106  background(bgcolor);
107  fill(fgcolor);
108  // Draw the shape
109  ellipse(xpos, ypos, 20, 20);
110}
111
112void serialEvent(Serial myPort) {
113  // read a byte from the serial port:
114  int inByte = myPort.read();
115  // if this is the first byte received, and it's an A,
116  // clear the serial buffer and note that you've
117  // had first contact from the microcontroller.
118  // Otherwise, add the incoming byte to the array:
119  if (firstContact == false) {
120    if (inByte == 'A') {
121      myPort.clear();          // clear the serial port buffer
122      firstContact = true;     // you've had first contact from the microcontroller
123      myPort.write('A');       // ask for more
124    }
125  }
126  else {
127    // Add the latest byte from the serial port to array:
128    serialInArray[serialCount] = inByte;
129    serialCount++;
130
131    // If we have 3 bytes:
132    if (serialCount > 2 ) {
133      xpos = serialInArray[0];
134      ypos = serialInArray[1];
135      fgcolor = serialInArray[2];
136
137      // print the values (for debugging purposes only):
138      println(xpos + "\t" + ypos + "\t" + fgcolor);
139
140      // Send a capital A to request new sensor readings:
141      myPort.write('A');
142      // Reset serialCount:
143      serialCount = 0;
144    }
145  }
146}
147*/
148
149/*
150Max/MSP version 5 patch to run with this example:
151
152----------begin_max5_patcher----------
1533908.3oc6ckziiaiE9b0+J3XjCIXpp.WzZNMURv.jCInQ5fYNjNngrDssRKK
1544nkp6JA4+973hrkrsjncKu0SRiXasQ83G+dKj7QV+4qtaxzrOxKlf9Zzuft6
155t+7U2cm7ThSbm936lrL3igIAExaaRJ+CYS+sI2qtTI+ikxSuBMKNojm+N3D4
156Aua5KkPwpuoUAkgKhSm+tbdXo5cQXVOhuGwrohuHD4WT7iXzupen3HY4BuqG
157rH0kzrrzxzfkb4kdJONHo9JoUKiSS3kRgjt4jYUk0mkznPJh+CYgHewpSqty
158xWVwUh3jIqkEYEfmqQEMr.ETbB+YddQbVZix+tIAqV03z203QDX4ukIKHm6W
159ep3T0ovqOUN+435m2Rcx+5U0E+FTzVBh9xOsHXIh5YuADg1x4IYgumG0r3mj
160shmFmtJmWvSKCJ0um0WNhOKnJo7c6GmZe8YAg7Ne381Rc2j44wQYoBgn0SJN
161c8qCHH1RhQqJi7NRCVsmGt.pGUESCxE31zDdCV.PRyxRZeo0MU.WOHMdYPIu
162LVIrT75BMd4p73zxVuHdZ.TFKJByyRRZUTpq77dtRDzZFx+PbT4BYY0DJgaO
163dUcSvj0XTT7bdQY6yUFLun8YZo71jl0TIt042RYNLa4RfCTWfsznKWDWfJpl
164tJHrbgV6t.AZInfzWP.4INpJHA8za91u+6QN1nk7hh.PpQwonxEbTAWzpilV
165MimilkmsDtPbo3TPiUdY0pGa9ZShS4gYUJz1pwE1iwCpxbAgJI9DGGwWNzFT
166ksLf3z7M0MybG6Hj1WngsD7VEXS8j5q7Wu5U0+39ir8QJJS5GMHdtRimL4m1
1670e1EVX0YsE2YssINriYRoFRyWVMoRRUGQvnkmms3pnXDYHbBKMPpIOL5i1s8
1683rMPwFcRCsGRyPH780.8HBnpWz.vlEQBWJ+0CSunehJSmJxiIZRtNGhhDYrU
169jt3ZQyA2fHJhZDifXIQHUHH8oGYgOREI5nqHIzhFWUndPyBdB3VzHJGwUhkV
170rgvRl2UCVNM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W8+.vfqkDA
240-----------end_max5_patcher-----------
241
242
243
244*/
Note: See TracBrowser for help on using the repository browser.