Pages: [1]   Go Down
Author Topic: Java interfacing with Arduino - Variable type problem  (Read 1057 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
Code:
$.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)
Code:
String valore = req.getParameter("valore");
rov.write(valore);

Thanks for the help!
scarface78987
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50440
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50440
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is some code to collect delimited packets:
                                                                                                                               
Code:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much, I'm going to try this later.
I'll let you know if it works!
« Last Edit: December 09, 2012, 06:04:26 am by scarface78987 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, it works!
I owe you a beer smiley
Logged

Pages: [1]   Go Up
Jump to: