char serString[4]; // 4 padded string ie 1111 int ledPin = 9; // to test input on an LED int serInt; //integer that comes out void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { readSerial(serString); //read the serial port serInt = atoi(serString); if serInt is more than 0 then do whatever is in the if statement if (serInt > 0){ Serial.println(serInt); analogWrite(ledPin, serInt); clearSerial(); } delay(50); } //function to read serial void readSerial(char *strArray) { int i = 0; if(!Serial.available()) { return; } //if serial has something read each figure and put it in char array while (Serial.available()) { strArray[i] = Serial.read(); i++; } } //clear the char array of any previous data void clearSerial() { for (int i=0; i < 4; i++) { serString[i] = ' '; }
/** Code for reading in data from one sensor on an arduino Pete Rogers 19/12/2008 17:20 **/ import processing.serial.*; int lf = 10; // Linefeed in ASCII int myWidth = 1024; // stage width int myHeight = 1024; //stage height String sensorString = null; //string holding raw data from arduino int sensorInt = 0; //number from sensor Serial myPort; // The serial port void setup() { size(myWidth, myHeight); //set up the stage window frameRate(30); //set frame rate playback smooth(); //make the visual quality high //lines below open and configure the serial port println(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); sensorString = myPort.readStringUntil(lf); sensorString = null; } void draw() { background(0); //repaints background ellipse(myWidth/2,myHeight/2, sensorInt, sensorInt); //draws circle } //serialEvent function is triggered whenever there is data from the arduino void serialEvent(Serial myPort){ sensorString = myPort.readStringUntil(lf); if ( sensorString != null) { sensorString = trim( sensorString); sensorInt = int( sensorString); } }