How to send numbers to my arduino via processing

edited December 2016 in Arduino

Hi guys,

my arduino code works but i have problems with my processing code. The code should gather one number from my website and send it to the arduino. It doesnt work sadly the way my processing code is written at the moment. Maybe you guys can take a look and help me with it.

Processing Code:

import processing.serial.*;
Serial ComPort;
String input[];
void setup(){

String portName = Serial.list() [0];
ComPort = new Serial(this, portName, 9600);
ComPort.bufferUntil('\n');
input = loadStrings("website-adresse");
if(input.length != 0){
String s_current = input[0];
int current = Integer.parseInt(s_current);
println(current);
delay(2000);
ComPort.write(current);
}
}

Arduino Code:

void setup() {
  Serial.begin(9600);  // Baudrate, muss mit PC übereinstimmen
  pinMode(13,OUTPUT);
}
void loop() {
  int c = Serial.read();
  switch (c) {
  case -1: return; // nichts neues gekommen, loop sofort beenden 
  case '0' :
      digitalWrite(13, LOW);
      break;
  case '1' :
      digitalWrite(13, HIGH);
      break;
  }
}

Greetings Jenni

Tagged:

Answers

  • Edit your post and format your code. Click on the gear icon in the corner of your post, select your code and hit ctrl+o. Ensure there is an empty line above and below your code.

    Kf

  • Done! :) Thank you kfrajer

  • edited December 2016 Answer ✓

    Does the println(current) work properly?
    In any case, why do you even need an int? Since you just have 0 and 1, all you need to do is replace line 15 of Processing code with this - ComPort.write(s_current.getCharAt(0)); and it should work.
    Note that the integer you are sending to the Arduino has the value of 0/1, whereas you're if it has the values of '0'/'1'. The latter are of type char whereas the former just int.

    EDIT: Changed "Arduino" to "Processing".

  • Yes println(current) works :)

    I used the code in the past to send 4 numbers like: 1530 For that use it was working.

    Are you sure to replace line 15 of the arduino code? Well both replacements did not work. About the rest iam a little bit confused can you explain whats up with char and int?

  • edited December 2016

    Oops, my mistake. I meant replace line 15 of Processing code with that. Sorry.
    UPDATE Fixed.

  • I changed in the arduino code '1'and '0' to 1 and 0. This way everything works perfectly :)

    Can you tell me what the change in line 15 would do and how it works?

    Greetings

  • edited December 2016

    You have effectively done the same thing.
    Here, you made the Arduino check for 1 and 0 instead of '1' and '0'. What I suggested was to change the Processing send '1' and '0' instead of 1 and 0.
    Check out how characters are coded in computers to know more of why it works that way.
    Note that my method will help you remove a lot of unnecessary code.

  • I see that makes sense :) I will look into it thx.

  • edited December 2016

    Edit....

    Post answer in wrong post....

    Kf

Sign In or Register to comment.