Skip to content

changed serial communication, and added digital inputs #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 123 additions & 102 deletions arduino.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,146 +6,167 @@ int cmd_arg[2];

int serialStatus = 0;

void setup() {
// connect to the serial port
Serial.begin(115200);
setupPins();
serialStatus = 1;
void setup()
{
// connect to the serial port
Serial.begin(115200);
// setup the output and input pins
setupPins();
serialStatus = 1;
}

void loop()
{

if(serialStatus==0)
{
Serial.flush();
setupPins();
}
askCmd();

{
if(Serial.available()>0)
{
cmd = int(Serial.read()) - 48;

if(cmd==0) //set digital low
{
cmd_arg[0] = int(readData()) - 48;
digitalWrite(cmd_arg[0],LOW);
}

if(cmd==1) //set digital high
{
cmd_arg[0] = int(readData()) - 48;
digitalWrite(cmd_arg[0],HIGH);
}

if(cmd==2) //get digital value
{
cmd_arg[0] = int(readData()) - 48;
cmd_arg[0] = digitalRead(cmd_arg[0]);
Serial.println(cmd_arg[0]);
}

if(cmd==3) // set analog value
{
Serial.println("I'm in the right place");
cmd_arg[0] = int(readData()) - 48;
cmd_arg[1] = readHexValue();
analogWrite(cmd_arg[0],cmd_arg[1]);
}

if(cmd==4) //read analog value
{
cmd_arg[0] = int(readData()) - 48;
cmd_arg[0] = analogRead(cmd_arg[0]);
Serial.println(cmd_arg[0]);
}

if(cmd==5)
if(serialStatus == 0)
{
Serial.flush();
// setup the output and input pins
setupPins();
serialStatus = 1;
}
askCmd();



{
if(Serial.available()>0)
{
serialStatus = 0;
cmd = Serial.read();

if(cmd == 0) //set digital low
{
cmd_arg[0] = readData();
digitalWrite(cmd_arg[0],LOW);
}
else if(cmd == 1) //set digital high
{
cmd_arg[0] = readData();
digitalWrite(cmd_arg[0],HIGH);
}
else if(cmd == 2) //get digital value
{
cmd_arg[0] = readData();
cmd_arg[0] = digitalRead(cmd_arg[0]);
Serial.write(cmd_arg[0]);
}
else if(cmd == 3) // set analog value
{
Serial.println("I'm in the right place");
cmd_arg[0] = readData();
cmd_arg[1] = readHexValue();
analogWrite(cmd_arg[0],cmd_arg[1]);
}
else if(cmd == 4) //read analog value
{
cmd_arg[0] = readData();
cmd_arg[0] = analogRead(cmd_arg[0]);
Serial.println(cmd_arg[0]);
}
else if(cmd == 5)
{
serialStatus = 0;
}
else
{
// invalid command
}
}
}
}
}
}

char readData()
{
askData();

while(1)
{
if(Serial.available()>0)
{
return Serial.read();
}
}
askData();

while(1)
{
if(Serial.available() > 0)
{
return Serial.read();
}
}
}


//read hex value from serial and convert to integer
int readHexValue()
{
int strval[2];
int converted_str;

while(1)
{
if(Serial.available()>0)
{
strval[0] = convert_hex_to_int(Serial.read());
break;
}
}
int strval[2];
int converted_str;

askData();
while(1)
{
if(Serial.available() > 0)
{
strval[0] = convert_hex_to_int(Serial.read());
break;
}
}

while(1)
{
if(Serial.available()>0)
{
strval[1] = convert_hex_to_int(Serial.read());
break;
}
}
askData();

while(1)
{
if(Serial.available() > 0)
{
strval[1] = convert_hex_to_int(Serial.read());
break;
}
}

converted_str = (strval[0]*16) + strval[1];
return converted_str;
converted_str = (strval[0]*16) + strval[1];
return converted_str;
}


int convert_hex_to_int(char c)
{
return (c <= '9') ? c-'0' : c-'a'+10;
return (c <= '9') ? c-'0' : c-'a'+10;
}


void askData()
{
Serial.println("?");
Serial.println("?");
}


void askCmd()
{
askData();
while(Serial.available()<=0)
{}
askData();
while(Serial.available() <= 0)
{}
}


void setupPins()
{
while(Serial.available()<1)
{
// get number of output pins and convert to int
cmd = int(readData()) - 48;
for(int i=0; i<cmd; i++)
{
cmd_arg[0] = int(readData()) - 48;
pinMode(cmd_arg[0], OUTPUT);
}
break;
}
// wait for serial data, to define the outputs
while(Serial.available() < 1)
{
// first byte of number of pins,
// then each byte is a pin to activate
cmd = readData();
for(int i=0; i < cmd; i++)
{
cmd_arg[0] = readData();
pinMode(cmd_arg[0], OUTPUT);
}
break;
}

// wait for serial data, to define the inputs
while(Serial.available() < 1)
{
// first byte of number of pins,
// then each byte is a pin to activate
cmd = readData();
for(int i=0; i < cmd; i++)
{
cmd_arg[0] = readData();
pinMode(cmd_arg[0], INPUT);
}
break;
}
}
5 changes: 4 additions & 1 deletion example_blink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
board = Arduino.new(port)

#declare output pins
board.output(13)
board.output(13) # manditory function call
#declare input pins
board.input() # manditory function call


#perform operations
5.times do
Expand Down
47 changes: 47 additions & 0 deletions example_io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

require 'rubygems'
require './lib/arduino.rb'
#require 'arduino'

#specify the port Baudrate is optional and set to 115200 by default
#board = Arduino.new("/dev/ttyAMC0")
board = Arduino.new("/dev/ttyUSB0" )

puts "connected"

#declare output pins
#board.output(12, 13) # multiple outputs
board.output(13)

puts "output defined"

#declare input pins
#board.input(10, 11) # multiple inputs
#board.input() # no inputs
board.input(10)

puts "input defined"

puts board.to_s

puts "running... "

while true

#puts "checking input"

# checking a normally open button
# when the button is pressed, turn on the LEDs
if(true == board.isLow?(10))
#puts "on"
board.setHigh(13)
else
#puts "off"
board.setLow(13)
end

end

board.close


Loading