Hello Group!
I've been using an Arduino Uno for over a year now and I've been a Perl programmer since highschool and I've recently starting to incorporate my Arduino and my Perl scripts together with some amazing results. I usually try and work on a project until it is completed with out asking for any help however that is why I am now a registered forum member. So here goes nothing. I can successfully send commands to my Arduino from my Perl script however I am trying to read information being sent from the Arduino and using that information to execute other commands in the Perl script. I am stuck. I know the information is being sent as I open the serial monitor on the Arduino software and issue a command and based on that command I see the result. The result is not being parsed correctly by my Perl script. I've posted code of both the Ardiuno Sketch as well as the Perl code. Thanks in advanced for the help!
Arduino Code:
int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int sigoneRed = 12;
int sigoneYellow = 11;
int sigoneGreen = 10;
int sigtwoRed = 9;
int sigtwoYellow = 8;
int sigtwoGreen = 7;
int sigthreeRed = 6;
int sigthreeYellow = 5;
int sigthreeGreen = 4;
void setup() {
Serial.begin(9600);
pinMode(sigoneRed, OUTPUT);
pinMode(sigoneYellow, OUTPUT);
pinMode(sigoneGreen, OUTPUT);
pinMode(sigtwoRed, OUTPUT);
pinMode(sigtwoYellow, OUTPUT);
pinMode(sigtwoGreen, OUTPUT);
pinMode(sigthreeRed, OUTPUT);
pinMode(sigthreeYellow, OUTPUT);
pinMode(sigthreeGreen, OUTPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
char in;
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// TEST ALL SIGNAL LEDS ON:
digitalWrite(ledPin, HIGH);
digitalWrite(sigoneRed, HIGH);
digitalWrite(sigtwoRed, HIGH);
digitalWrite(sigthreeRed, HIGH);
digitalWrite(sigoneYellow, HIGH);
digitalWrite(sigtwoYellow, HIGH);
digitalWrite(sigthreeYellow, HIGH);
digitalWrite(sigoneGreen, HIGH);
digitalWrite(sigtwoGreen, HIGH);
digitalWrite(sigthreeGreen, HIGH);
Serial.println("TEST ALL SIGNAL LEDS ON");
delay(10000);
digitalWrite(ledPin, LOW);
digitalWrite(sigoneRed, LOW);
digitalWrite(sigtwoRed, LOW);
digitalWrite(sigthreeRed, LOW);
digitalWrite(sigoneYellow, LOW);
digitalWrite(sigtwoYellow, LOW);
digitalWrite(sigthreeYellow, LOW);
digitalWrite(sigoneGreen, LOW);
digitalWrite(sigtwoGreen, LOW);
digitalWrite(sigthreeGreen, LOW);
Serial.println("TEST ALL SIGNAL LEDS OFF");
}
if(Serial.available() > 0) {
int in = Serial.read();
switch(in) {
case 't':
digitalWrite(ledPin, HIGH);
digitalWrite(sigoneRed, HIGH);
digitalWrite(sigtwoRed, HIGH);
digitalWrite(sigthreeRed, HIGH);
digitalWrite(sigoneYellow, HIGH);
digitalWrite(sigtwoYellow, HIGH);
digitalWrite(sigthreeYellow, HIGH);
digitalWrite(sigoneGreen, HIGH);
digitalWrite(sigtwoGreen, HIGH);
digitalWrite(sigthreeGreen, HIGH);
Serial.println("ON");
delay(8000);
digitalWrite(ledPin, LOW);
digitalWrite(sigoneRed, LOW);
digitalWrite(sigtwoRed, LOW);
digitalWrite(sigthreeRed, LOW);
digitalWrite(sigoneYellow, LOW);
digitalWrite(sigtwoYellow, LOW);
digitalWrite(sigthreeYellow, LOW);
digitalWrite(sigoneGreen, LOW);
digitalWrite(sigtwoGreen, LOW);
digitalWrite(sigthreeGreen, LOW);
Serial.println("OFF");
break;
default:
Serial.println("Invalid Character");
break;
}
}
}
Perl Code:
use strict;
use warnings;
use Win32::SerialPort;
my $port = Win32::SerialPort->new("COM7") or die "Open Port Failed. $!\n";
$port->is_rs232;
# SET UP THE SERIAL PORT 9600, 81N ON THE USB FTDI DRIVER
$port->initialize();
$port->baudrate(9600);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);
$port->write_settings || undef $port;
#$port->are_match("\r\n");
sleep(3);
print STDOUT "\n$time Signal Driver Starting....\n";
my $testall = "t";
$port->write($testall);
my $data;
$port->lookclear;
while(1) {
$data = $port->lookfor();
if ($data) {
print STDOUT "$time Signal Driver Message Received: (" . $data . ")\n";
return;
}
if ($data =~ /OFF/) {
print STDOUT "$time Signal Driver Message Received: (" . $data . ")\n";
return;
}
}
return;
What I expect to see is when OFF is received I can have the perl script execute another command. So far it has not. Thanks again for pointing me in the right direction.