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
$.ajax ({
url: 'http://localhost:8080/dip_ROV/LedRegolabiliServlet?valore='+pos2,
dataType: 'text',
success: function(data) {
alert(data);
}
And then the Servlet writes the value to the Arduino (rov)
String valore = req.getParameter("valore");
rov.write(valore);
Thanks for the help!
scarface78987