The number shown on the web page is correct, but the led color is wrong and sometimes delayed. So, does it seem that the signal sent from web page to Arduino is very slow?
This is my Arduino code:
const int redPin = 2;
const int bluePin = 3;
const int greenPin = 4;
const byte debugPin = 13;
int usbnumber = 0;
The 'slow' part can be explained by the total delay time of 500 miliseconds after each read, I reckon.
You could remove this by using the same technique used in the Blink Without Delay example.
If I key in 218, the values '2', '1', '8' will be sent to the Arduino.
The Arduino will read 3 values ('2', '1', and '8'). The usbvariable variable will contain 50, 49, and 56 (the ascii equivalent of the characters sent).
if(50 % 3 == 0) as a method for determining whether to turn the red LED on, or not, makes no sense to me. Perhaps it does to you, and you could explain it to me.
I would think that you would want to send 3 values in the range 0 to 255 (as character strings), along with start and stop markers (something like "<128,217,18>"), then read the entire string, and parse that string to get "128", "217", and "18", then convert those strings to integers, and use digitalWrite on PWM pins to set the red, green, and blue LEDs intensity.
What I want to do now is to let a user to enter a value from PHP. Then, this value can be passed to the Arduino. There is a LED light with three different colors on my breadboard. I want to use the value to control the color of this LED. For example, if value%3==0, then show red light.
The syntax error may be causing some unexpected output on the serial port.
On the Arduino:
bool started = false;
bool ended = false;
int serValue = -1;
void loop()
{
while(Serial.available() > 0 && !ended)
{
char aChar = Serial.read();
if(aChar == '>')
{
started = true;
ended = false;
serValue = 0;
}
else if(aChar == '>')
{
started = false;
ended = true;
}
else
{
if(started && aChar >= '0' && aChar <= '9')
{
serValue *= 10;
serValue += aChar - '0';
}
}
}
// Done processing pending serial data
if(!started && ended) // Have we got a complete value?
{
// Light the LEDs based on serValue
}
}
This code will read a stream of data like "14><472><23><4", and set serValue to 0, 4, 47, and 472, then allow you to use serValue, then set serValue to 0, 2, and 23, and again allow you to use serValue.
If you had a serial LCD, you could use NewSoftSerial to communicate with it, and echo what is received on the serial port, for debugging purposes.
Hi guys! I seriously need you help! (I've been sitting for six hours straight with this problem. Everything is set up, only problem is, if I send "1" or "0" or anything, the arduino only receives ""
What is the point of sleeping 3 seconds before closing the file handle (PHP side)? I am wondering if the data isn't actually sent until the file handle is closed...?
Hmm - here's another interesting thread (indeed, googling "arduino php serial" is recommended):
That example shows the "sleeping 3 seconds" before closing as well; I still question that, it seems superfluous to me. Can anyone explain why its used?
I also noted that there seems to be an issue under Windows with reading the serial port - is this still true?
Thanks guys! - I've visited all that links before (thats where I found the sleep(3) part) - I myself don't know the reason why to put it to sleep for 3 secs
I have found a semi solution, combined with what cr0sh came up with - it might work:
Firstly, I changed the fopen to "w+" ... then I made it to sleep between each step. The auto-reset must be the problem. When it sleeps, the arduino has some time to do his stuff, and then it actually works. But who would want to wait 6 sec to turn off their room lights?...
So I will inform you if cr0sh's solution of removing the auto-reset does not work!