Skip to content

Instantly share code, notes, and snippets.

@dlarue
Forked from JamesNewton/PinReadWrite.ino
Created May 15, 2017 17:27
Show Gist options
  • Save dlarue/8e41c40c39779d3db6260bc9f46b1be1 to your computer and use it in GitHub Desktop.
Save dlarue/8e41c40c39779d3db6260bc9f46b1be1 to your computer and use it in GitHub Desktop.
Simple Arduino script to set pins high, low, input, pull up, or analog and read all or a single pin back via serial IO.
/*
PinReadWrite.ino
Simple Arduino script to set pins high, low, input, pull up, or analog and
read all or a single pin back via serial IO. Written for the tiny-circuits.com
TinyDuino in the end effector of the Dexter robot from HDRobotic.com
Commands:
#? //return binary value of digital pin, and value for analog input if exists
//if # and #, are zero, returns all pins and analog values at once.
#I //set pin # to an input
#P //set pin # to an input with internal pullup
#H //set pin # to a high output
#L //set pin # to a low output
#, //save pin # as the default pin for all commands
#A //set pin # to an analog output with value. Only PWM outputs will respond.
// use with , command e.g. 5,120A will put 120 on pin 5
Commands can be strung together on one line; spaces, tabs, carrage returns and line feeds
are all ignored. If no n is specified, value previously saved by , is used.
Examples:
?
//returns something like: <10000000001111,539,475,425,408,386,376>
// where 10000000001111 shows the binary value of each pin, from 0 to 14. Pin 0 is 1
// 539,475,425,408,386,376 are the values read from each analog channel 0 to 5
1?
//returns something like: <1:1,539> where 1 is the binary value of pin 1 and
//539 is the analog value of channel 1
6?
//returns something like <6:0> which is the value of pin 6 (no analog)
4L 6H 5,120A
//(nothing returned) Drives pin 4 low, pin 6 high and puts a PWM / Analog value of 120 on pin 5
//this also saves pin 5 as the default pin for all commands from now on
240A
//(nothing returned) assuming prior command was 5,120A put 240 out pin 5 as new analog value
?
//assuming 5, has been recieved before, returns just the value of pin 5 and analog 5
0,
//(nothing returned) clears saved pin, ? now returns all pins.
*/
#define ANALOG_PINS 6
#define DIGITAL_PINS 14
#define BAUD_RATE 57600
//didn't work at 115200
#define CYCLE_DELAY 100
int n,p;
char cmd;
void setup() {
Serial.begin(BAUD_RATE);
n=0; //number
p=0; //pin number
}
void loop(){
while (Serial.available() > 0) { //if data has arrived
int c = Serial.read(); //get the data
if ('0' <= c && c <= '9') { //if it's a digit
n = (c-'0') + n*10; //add it to n, shift n up 1 digit
continue; //and loop
}
cmd = char(c); //wasn't a number, must be a command
if (','==cmd) { p=n; n=0; continue;} //save n to p, clear n, loop
if (0==n) {n=p; } //if we don't have a value, use the prevous pin number
switch (cmd) {
case '?': //get information
Serial.print("<"); //optional, just to signal start of data
if (0==n) { //if we didn't have a number selecting a pin
for (int p = 0; p < DIGITAL_PINS; p++) { //get all the pins
//n = digitalRead(p) + n<<1; //convert to binary number
Serial.print(digitalRead(p));//and also print.
}
//Serial.print(",");
//Serial.print(n); //print the binary value of all pins
for (int p = 0; p < ANALOG_PINS; p++) { //also check all the analog
Serial.print(",");
Serial.print(analogRead(p));
}
}
else {
Serial.print(n);
Serial.print(":");
Serial.print(digitalRead(n)); //just that one pin
if (ANALOG_PINS > n) { //if there is an analog channel
Serial.print(",");
Serial.print(analogRead(p)); //also return it
}
}
Serial.println(">");
break;
case 'H': //set pin n output high
pinMode(n,OUTPUT);
digitalWrite(n,HIGH);
break;
case 'L': //set pin n output low
pinMode(n,OUTPUT);
digitalWrite(n,LOW);
break;
case 'I': //set pin n input
pinMode(n,INPUT);
break;
case 'P': //set pin n input with pullup
pinMode(n,INPUT_PULLUP);
break;
case 'A': //set pin p to analog output value n
pinMode(p,OUTPUT);
analogWrite(p,n);
break;
case '\n':
case '\r':
n=0; cmd=' '; //clear command and value at end of line.
//p is NOT cleared, so you can keep sending new commands only
case '\t':
case ' ':
break;
default:
Serial.print(n);
Serial.print(cmd);
Serial.print("?");
}
cmd=' '; //done with command.
n=0; //zero out value for next command.
}
delay(CYCLE_DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment