Arduino - PC communication

I wrote a small wpf programm to control some LED's. I built 4 different modi:
LEDON , LEDOFF, DIMMING, FLASH
It's very simple and works fine. I send a,b,c or d (4 different buttons) to the arduino and execute the corresponding loop.
Now I try to create a slider in my WPF program that can control the LED brightness. My slider creates values from 0 to 255 which I send to the arduino.
Now I ask if the incoming byte is a number, if yes I execute a method which should determine the brightness using analog.write(PIN,value);.
WPF:
private void targetSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { try { var val = Math.Round(targetSlider.Value).ToString(); Console.WriteLine("WPF: " + val); serialPort1.Write(val); var ArduVal = serialPort1.ReadByte(); Console.WriteLine("Arduino: " + ArduVal); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Arduino:
void loop() { checkSerial(); if(lightON)LightON(); else if(lightOFF)LightOFF(); else if(lightDIM) LightDIM(); else if(lightBLINK) LightBLINK(); } void checkSerial() { while(Serial.available()>0){ int incoming=Serial.read(); if(isdigit(incoming)){ executeSlider(incoming); Serial.print(incoming); } else{ char c=incoming; executeCommand(c); } } } void executeSlider(int val){ analogWrite(BLUE, val); analogWrite(GREEN, val); analogWrite(RED, val); }
As you can see, I check the values of the slider (which are sent to the arduino) and the values which arrive at the arduino.
The arduino values always commute around 50. I don't understand why? Should I pass an array? Or is something wrong with arudino-PC communication?
Console Output:
WPF: 2 Arduino: 53 WPF: 4 Arduino: 48 WPF: 4 Arduino: 53 WPF: 5 Arduino: 50 WPF: 6 Arduino: 53 WPF: 8 Arduino: 50 WPF: 8 Arduino: 53 WPF: 10 Arduino: 51 WPF: 10 Arduino: 53 WPF: 12 Arduino: 52 WPF: 13 Arduino: 53 WPF: 14 Arduino: 54 WPF: 14 Arduino: 53 WPF: 15 Arduino: 54 WPF: 16 Arduino: 52 WPF: 16 Arduino: 57 WPF: 17 Arduino: 52 WPF: 18 Arduino: 56 WPF: 20 Arduino: 52 WPF: 20 Arduino: 57 WPF: 21 Arduino: 52 WPF: 22 Arduino: 56 WPF: 23 Arduino: 52 WPF: 25 Arduino: 57 WPF: 26 Arduino: 53 WPF: 27 Arduino: 48 WPF: 29 Arduino: 52 WPF: 30 Arduino: 57 WPF: 31 Arduino: 53 WPF: 32 Arduino: 49 WPF: 33 Arduino: 52 WPF: 34 Arduino: 57 WPF: 35 Arduino: 53 WPF: 37 Arduino: 50
Hi
Could you please try Serialevent?
It is designed to handle the serial input, which has a better performance than loop.
See the tutorial here.
https://docs.arduino.cc/built-in-examples/communication/SerialEvent
Hope it can help.
