/* AtoD.ino Serial Call and Response in ASCII Language: Wiring/Arduino this sketch outputs adF( A0,A1,A2,A3,D2,D4,pw ); every 100ms This could be captureded is a file called adF.js and included in a .svg The circuit: * potentiometers attached to analog inputs A0 , A1 , A2, A3 * pushbutton attached to digital I/O 2 http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII */ #define STR1 "ad(" #define STR2 ")" int sensorA0 = 0; // first analog sensor int sensorA1 = 0; // second analog sensor int sensorA2 = 0; // int sensorA3 = 0; // int sensorD2 = 0; // int sensorD4 = 0; // int inByte = 0; // incoming serial byte int band = 100; // second analog sensor int lastSample1 = 0; int update = 0; int now = 0; int pw = 250; String str1; String str2; void setup() { // start serial port at 9600 bps and wait for port to open: Serial.begin(9600); // while (!Serial) { // ; // wait for serial port to connect. Needed for Leonardo only // } pinMode(2, INPUT); // digital sensor is on digital pin 2 pinMode(4, INPUT); // digital sensor is on digital pin 2 //establishContact(); // send a byte to establish contact until receiver responds pinMode(3, OUTPUT); // digital sensor is on digital pin 2 now = millis(); str1 = "ad()"; } void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { Serial.print("hello:"); } if ((millis() - now) > 0) { now = now + 100; // get incoming byte: inByte = Serial.read(); // read first analog input: sensorA0 = analogRead(A0); sensorA1 = analogRead(A1); sensorA2 = analogRead(A2); sensorA3 = analogRead(A3); sensorD2 = digitalRead(2); sensorD4 = digitalRead(4); // read switch, map it to 0 or 255L // send sensor values: analogWrite( 3, pw ); Serial.print("adF"); // emulator does no like braces and semi ; in "" // simulator does not like ad( in strings Serial.write( 40 ); Serial.print(sensorA0); Serial.print(","); Serial.print(sensorA1); Serial.print(","); Serial.print(sensorA2); Serial.print(","); Serial.print(sensorA3); Serial.print(","); Serial.print(sensorD2); Serial.print(","); Serial.print(sensorD4); Serial.print(","); Serial.print(pw); // simulator does not like ); in strings Serial.write( 41 ); Serial.write( 59 ); Serial.println( "" ); pw++; if ( pw > 255 ){ pw=0; } } } void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0,0,0"); // send an initial string delay(300); } }