0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« on: January 16, 2011, 03:50:57 pm » |
I'm trying with Arduino and PHP. I can turn led on, but I can't turn it off. Arduino:const int ledPin = 9; int incomingByte;
void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); }
void loop() { if (Serial.available() > 0) { incomingByte = Serial.read(); if (incomingByte == 'L') { digitalWrite(ledPin, LOW); } if (incomingByte == 'H') { digitalWrite(ledPin, HIGH); } } } PHP:<?php include "php_serial.class.php"; $serial = new phpSerial; $serial->deviceSet("COM7"); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(1); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $serial->sendMessage('H'); $serial->deviceClose(); ?> I tried to invert H and L in Arduino code (H was turning off and L was turning on). With inverted code I can't turn it on or off (other words, nothing was working). That tells me that letter "L" is not working. I replaced it with S and now I can turn led on and off. But why letter L doesn't work? Thank you very much. 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #1 on: January 16, 2011, 05:45:22 pm » |
$serial->deviceOpen(); $serial->sendMessage('H'); $serial->deviceClose(); Every time you open the serial port, the Arduino resets. You need some delay between opening the serial port and sending data, to allow the Arduino time to reboot.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #2 on: January 17, 2011, 10:19:35 am » |
Oh. That will complicate things.  Ok. I tried it. Still not working. It looks like I can send some letters and some I can't. Php code: <?php include "php_serial.class.php"; $serial = new phpSerial; $serial->deviceSet("COM7"); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(6); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $serial->sendMessage(); sleep(5); $slovo = $_POST["slovo"]; $serial->sendMessage($slovo); sleep(5); $serial->deviceClose();
header("Location: led.html"); ?> But, thank you very much.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #3 on: January 17, 2011, 11:06:28 am » |
$serial->confCharacterLength(6); If this is setting the number of bits to send, the value should be 8.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #4 on: January 17, 2011, 01:54:15 pm » |
Well, I got it  <?php require("php_serial.class.php");
function Arduino($str){ $serial = new phpSerial(); $serial->deviceSet("COM7"); $serial->confBaudRate(9600); $serial->confCharacterLength(8); $serial->deviceOpen();
for ($i=0; $i<strlen($str); $i++){ $serial->sendMessage($str[$i]); } $serial->deviceClose(); } Arduino($_POST['slovo']);
header("Location: led.html"); ?>
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #5 on: January 18, 2011, 01:21:58 pm » |
Ok, as you see that PHP code is sending one character at time. So I need to store all characters in a string. I think it's possible by saving in array with for loop and than converting array into string. I tried, but I can't make it. Actually, I can, but only if i know number of characters. Can someone suggest me something?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #6 on: January 18, 2011, 02:41:50 pm » |
Actually, I can, but only if i know number of characters. Can someone suggest me something? Sure. Send some known end-of-packet marker from PHP. Have the Arduino just keep reading characters until that marker arrives. This is a packet! Stop reading when the exclamation point arrives! Then, do something with the data received! Make sure to check for room in the array, before adding the new character!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #7 on: January 18, 2011, 03:26:33 pm » |
Good idea  I decided for EOT (End Of Transmission). It's number 4 in ASCII. For php side I can use chr. But how can Arduino detect that character? http://en.wikipedia.org/wiki/End-of-transmission_characterEDIT: Oh stupid me. Arduino is reading integer (or ASCII). So simple if will do a job?
|
|
|
|
« Last Edit: January 18, 2011, 03:34:40 pm by dario111cro »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #8 on: January 18, 2011, 05:10:32 pm » |
So simple if will do a job? I recognized each and every one of those words. Comprehending what they mean in that order escapes me. If you are asking whether EOT/4 can be used as the end-of-packet marker, the answer is yes. I prefer a visual character, for printing purposes, but the marker does not have to be a printable character.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #9 on: January 19, 2011, 11:55:29 am » |
Haha. I read that sentence again. :o :  So I meant: Will "if" do a job? xD I tried and I can't get it. #include <LiquidCrystal.h>
const int ledPin = 9; int serial; int serial_last; int stop_bit = false; int i = 0; int array[0]; int result; int check = false;
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup() { Serial.begin(9600); lcd.begin(8, 2); pinMode(ledPin, OUTPUT); }
void loop() { if (Serial.available() > 0) { serial = Serial.read(); if(serial == 4 and !stop_bit){ stop_bit = true; } else { if(serial_last != serial) { serial_last = serial; array[i] = serial; i++; } } } if(stop_bit) { if(!check) { for (int x=0; x < i; x++){ result += array[x]; } check = true; } if(result == 'ON') { digitalWrite(ledPin, HIGH); } else if(result == 'OFF') { digitalWrite(ledPin, LOW); } else if (result != 0) { lcd.write(result); serial = 0; } } } Probably stupid mistake. ;D Thank you for helping.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #10 on: January 19, 2011, 12:59:20 pm » |
Take pencil and a piece of paper. Write down the values that should be in serial, serial_last, i, and array, when the string "OFF" is sent to the serial port. if(serial_last != serial) { serial_last = serial; array[i] = serial; i++; } Are the answers what you expected?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 36
Arduino rocks
|
 |
« Reply #11 on: January 19, 2011, 02:18:39 pm » |
Ahhh... #include <LiquidCrystal.h>
const int ledPin = 9; int serial; int stop_bit = false; int i = 0; int array[0]; int result; int check = false;
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup() { Serial.begin(9600); lcd.begin(8, 2); pinMode(ledPin, OUTPUT); }
void loop() { if (Serial.available() > 0) { serial = Serial.read(); if(serial == 4){ stop_bit = true; } else { array[i] = serial; i++; } } if(stop_bit) { if(!check) { for (int x=0; x < i; x++){ result += array[x]; } check = true; } if(result == 'ON') { digitalWrite(ledPin, HIGH); } else if(result == 'OFF') { digitalWrite(ledPin, LOW); } else if (result != 0) { lcd.write(result); serial = 0; } } } Now when I write "dsa" I get only dddddddddddddddd on my lcd. :/ How I can calculate number of bits? That's only thing I neec.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #12 on: January 19, 2011, 02:29:33 pm » |
Now when I write "dsa" I get only dddddddddddddddd on my lcd. Once stop_bit gets set to true, it never gets set back to false. Once check is set to true, it never gets set back to false. Once I gets set, it never gets set back to 0. Once array - gets set, it never gets overwritten.
See a pattern? See a solution? I don't see why the repeating ds happen, though. Fix the issues here, and see if that makes a difference.
|
|
|
|
|
Logged
|
|
|
|
|
|