Go Down

Topic: Make Arduino execute commands on computer? (Read 6424 times) previous topic - next topic

computer_freak_8

Oh duh! I'm kicking myself for this right now:
Quote
You'll need to replace /dev/ttyUSB0 with the serial port your arduino is attached to, dmesg will show the port used by the arduino shortly after you plug it in


It really helped for me to try again - with it plugged in.
No errors are show, so my guess is that it works, but I'll report back after adding a new sketch to the Arduino.

Thanks so much,
computer_freak_8

computer_freak_8

Hmm... Somehow, I managed to make a program that freezes the Arduino software when I switch to the Serial Monitor. Now, a few seconds after plugging the Arduino into the computer, the "TX" light starts slowly blinking, and my Serial Monitor shows the number 4 being spit out time and time again, just flooding it.

Here's the code I used on the Arduino:
Code: [Select]
/*
*
*  Trial sketch for handling button presses (converting to serial output)
*
*/

int button4 = LOW;
int button5 = LOW;
int button6 = LOW;

void setup()
{
 pinMode(4, INPUT);
 pinMode(5, INPUT);
 pinMode(6, INPUT);
 Serial.begin(9600);
}

void loop()
{
 button4 = digitalRead(4);
 button5 = digitalRead(5);
 button6 = digitalRead(6);
 
 if (button4 == HIGH) {Serial.print("4");}
 if (button5 == HIGH) {Serial.print("5");}
 if (button6 == HIGH) {Serial.print("6");}
}


I had started my Perl script, and had not pressed any buttons yet. There is nothing connecting pin 4; it did this even when it was dangling by the cord, with nothing touching it.

Any ideas?

Digger450

That's because the input is floating and will randomly switch between high and low.  If you connect your button you should be fine.  Otherwise comment out that line until you get it hooked up and you should be fine.

computer_freak_8

Quote
That's because the input is floating and will randomly switch between high and low.

Thanks for that. But if the switch is "off", then what's the difference?

Anyways, I think I've discovered two things:
1. I can't get it to work.
2. The Arduino will only transmit to the computer if the Serial Monitor is open. It must not like the Perl script.

Also, the Perl script releases the terminal, and it does not show up in the list of processes. Here is the code I have for it:
Code: [Select]
#!/usr/bin/perl -w

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

$number_of_chars_to_read=1;

$command4="/home/jaredtbrees/Scripts/Linux/wake-up_alarm.sh";

$command5="/home/jaredtbrees/Scripts/Linux/start_elisa.sh";

# $time="1";

while(my $code=$port->read($number_of_chars_to_read))
{

print "$code";

system("$command4") if($code eq "a");
system("$command5") if($code eq "b");

}


Ideas?

Thanks again,
computer_freak_8

gnu_linux

You may need to adjust a few other statements a little

sleep(1); will sleep for 1sec that may be too long, in which case you can sleep for 100 or 200ms using Time::HiRes

Code: [Select]


#!/usr/bin/perl -w

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

$number_of_chars_to_read=1;

$command4="/home/jaredtbrees/Scripts/Linux/wake-up_alarm.sh";

$command5="/home/jaredtbrees/Scripts/Linux/start_elisa.sh";

# $time="1";

while(1)
{

my $code=$port->read($number_of_chars_to_read);

print "$code";

system("$command4") if($code eq "a");
system("$command5") if($code eq "b");


sleep(1);
}





:)

computer_freak_8

Okay, thanks, gnu_linux.
I got it to work - a little bit.
Apparently, whenever the computer is listening, the Arduino sends out the number 4 continually - even with the switch attached.
But hey, at least now it runs the specified command! It's just kind of hard to exit the script. (But I was able to.)

More ideas? Great job helping me so far! Thanks again.

gnu_linux

#21
Jan 16, 2009, 09:36 pm Last Edit: Jan 16, 2009, 09:40 pm by gnu_linux Reason: 1
You can CTRL + C the perl script ( inside the terminal / console / xterm ) at any time and you can also find its PID and kill it

man ps

man kill

man killall

:)

computer_freak_8

Yeah, I figured out that part okay.

But I'm still stumped on how to get this to work properly. Why does it just send one "4" after the other when the computer listens, but the switch is "off" and connected?

gnu_linux

How do you have your switch wired up?

Can you post some pictures and wiring diagrams for us?

:)

computer_freak_8

I can try. The short answer is that it is a SPST Momentary (NO) that goes between the 3.3V and digital 4 sockets on the Arduino. Pictures here in a couple of minutes...

gnu_linux

Awesome :)

The pictures should help diagnose the problem :)

computer_freak_8

Okay, sorry for the long delay.
Keep in mind that it does the same thing, whether or not the switch is connected. It also doesn't matter which of my three surfaces I put it on (plastic, wood, semi-glossy paper).

Here is the Arduino with the switch plugged in (on the plastic surface).
Here you can see that the connection is not shorted where it goes into the switch.
Here you can see that the underside of the Arduino is not shorted by something stuck to it.
Here is the wooden surface (the computer cart).
Here is the semi-glossy paper surface (a calendar).

gnu_linux

A piece of cardboard should provide good insulation

Which pins did you connect the switch to?

Also have you tried button 5 or just 4?

Hopefully someone with better wiring skills than myself can chime in :)

computer_freak_8

#28
Jan 17, 2009, 12:48 am Last Edit: Jan 17, 2009, 12:50 am by computer_freak_8 Reason: 1
Hmm... I have a feeling that something is wrong with my Arduino code (which was previously posted).

Here's part that I caught from my serial monitor while the switch was off, but connected to the 3.3V and digital pin 5:
Code: [Select]
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555554545454544444445454545454444444545454545454444545454545454544454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454544445454545454544444454545454544444445454545454444444545454545444444445454545444444444545454544444444454545454444444445454545444444444545454544444444454545454444444445454545444444444545454544444444454545454444444445454545444444444545454544444444454545454444444445454545444444444545454544444444445454545444444444545454544444444454545454444444445454545444444444545454544

Maybe I need to utilize the ground pin somehow? Don't know if, and definitely don't know how.

gnu_linux

Yes that doen't look right the pins seem to be floating

If I am not mistaken you may need pull down resistors on your pins but I am not an expert in that area so hopefully someone more knowledgeable can jump in

:)

Go Up