///////////////////////////////////////////////// // // Serial_IO: Simple serial interface to // read and write Digitial I/O lines // // Open serial monitor at 9600 board // // Type "in" to read the digital inputs: // 10,16,14,15,18,19,20,21 // // Type "out 10101010" to write digital outputs: // 2,3,4,5,6,7,8,9 with bit pattern // ////////////////////////////////////////////////// const byte numChars = 64; char receivedChars[numChars]; void setup() { Serial.begin(9600); } void loop() { if (SerialRecvString()) { ParseNewData(); } } boolean SerialRecvString() { boolean newData = false; byte i = 0; char rc; while (Serial.available() > 0 && newData == false) { rc = Serial.read(); if (rc != '\n') { receivedChars[i] = rc; i++; if (i >= numChars) i = numChars - 1; } else { receivedChars[i] = '\0'; // terminate the string i = 0; newData = true; } } return newData; } void ParseNewData() { if ((receivedChars[0]=='o') && (receivedChars[1]=='u') && (receivedChars[2]=='t')) { //output bits for (int c=0; c<9; c++) { if (receivedChars[(c+4)] == '1') digitalWrite(c+2, HIGH); else digitalWrite(c+2, LOW); } Serial.println("Digital Outputs Updated."); } else { if ((receivedChars[0]=='i') && (receivedChars[1]=='n')) { //read the input bits to a string char outputstr[32] = "Digital Inputs 11111111"; outputstr[15] = (digitalRead(10))?'1':'0'; outputstr[16] = (digitalRead(16))?'1':'0'; outputstr[17] = (digitalRead(14))?'1':'0'; outputstr[18] = (digitalRead(15))?'1':'0'; outputstr[19] = (digitalRead(18))?'1':'0'; outputstr[20] = (digitalRead(19))?'1':'0'; outputstr[21] = (digitalRead(20))?'1':'0'; outputstr[22] = (digitalRead(21))?'1':'0'; //display results Serial.println(outputstr); } else { Serial.println(" in or out"); } } }