0
votes

this is my first question so i try my best to explain it good enough. My Goal is to gather a number via processing from my website and send it to my arduino. I try to turn my led on when the number 1 comes in or turn it off when the number 0 comes in.

So far i managed to gather the number from my website. And when i send a number with the arduino serial monitor to the arduino the arduino code works, too.

The part that is not working is the sending of the number from processing to the arduino. The led just flickers once.

Here is my 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);
    }
}

Here is my 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;
 }
 }

Iam new to programming and my english is not that good so i apologize for that.

Thanks for your help

Greetings Jenni

1
Can you verify that Serial.read() really return -1 when nothing new is on the line? Also, do you set the output elsewhere, too? Maybe you unintentionally pull it down? You'll have to add more code. Oh, and english comments would be nice for non-german speakers.Fildor
I dont know how to verify that. The led stays in the right state when nothing is new on the line maybe that counts as a verify?^^ "Also, do you set the output elsewhere, too? Maybe you unintentionally pull it down?" Iam sry i dont know what you mean there. ;( Ofcourse i will respond in english :) I guess my english skills should be enough.Jennifer96

1 Answers

2
votes

The main problem is sending values 0 (0x00) and 1 (0x01) over the ComPort but you are expecting '0' (0x30) and '1' (0x31).

If you change cases to expect 0 and 1 instead of '0' and '1' it will work (but these values won't work in Serial Monitor unless you'll have both variants)