Hi everybody,
I'm working on a project in which a Java Servlet sends data to an Arduino (basing on a 0-255 input slider) and the Arduino microcontroller does an analogWrite to a LED connected to pin 10 to determine its brightness.
If I send a value like 'H' or 'L' to switch the LED on and off it works with a simple
case 'H':
digitalWrite(led, HIGH);
break;
case 'L':
digitalWrite(led, LOW);
break;
on the Arduino.
If the arduino reads another value I want it to do this:
analogWrite(led,i);
Arduino writes a constant number (83) to pin 10 but I can't figure out what's wrong. I just wanted to make him read a serial input and then write that number to the LED.
I think there's a problem in the data type, here are the two codes.
Arduino:
int led = 10;
int i;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
i = Serial.read();
switch (i) {
case 'H':
digitalWrite(led, HIGH);
break;
case 'L':
digitalWrite(led, LOW);
break;
default:
analogWrite(led,i);
}
}
}
An js in an html file invokes the Java Servlet and sends pos2 that is the position of the slider
I'm working on a project in which a Java Servlet sends data to an Arduino (basing on a 0-255 input slider) and the Arduino microcontroller does an analogWrite to a LED connected to pin 10 to determine its brightness.
Writing to a pin using analogWrite() does not determine the brightness. It defines the brightness.
String valore = req.getParameter("valore");
rov.write(valore);
Why are you sending a String to the Arduino and expecting it to deal with it as a number. There is a big difference between 204 and '2', '0', '4'.
The Arduino can be programmed to read strings, if it knows to expect them, AND if there are delimiters that define when the string starts and ends.
Thanks, I'm new to programming so I don't know these things.
What should I write in the arduino sketch to make him convert the string into int to get a number from 0 to 255?
Thanks again for your reply
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
If you change the sending application so it sends '<', the String, and '>', then in the started && ended block, inData will contain the character string that was in valore, NULL terminated.
A simple call to atoi() will get that as a number.
So, if the slider position is 189, and the Java app sends "<189>", inData will contain "189", and atoi() will convert that to 189, which you can send to the PWM pin.