Serial Communication sending more than one byte

Hey,

I've made a program in Vb.net for controlling the switching on and off of a LED,

My arduino code will read the following input and as the code runs, the LED will switch On.

SerialPort1.Write(1)
//////////////////////

But now for example, I have a numeric up down control (in VB.net) and i want to use it to set the brightness of the LED as well.

If I Select '128' from the numeric up down control then I'd like to write the vb.net code as; SerialPort1.Write(1128), which means something like (light up the led at half brightness)

But my problem is, I don't know how to seperate the '1' and '128' in during the arduino code.

When I used to switch the led on and off (digitally) the code was :

int val = Serial.read() - '0';

if (val == 0){
digitalWrite(led,LOW);
}

if (val == 1){
digitalWrite(led,HIGH);
}

But now if i use (int val = Serial.read() - '0'), it will not assign '1128' to it.

If someone could provide me with the correct arduino code so that it will be able to seperate the 1 and 128 to two different variables (val, and brightness?)

I tried googling but I couldn't find a solution,

Desperately need to figure this out so waiting for your replies :slight_smile:

But now if i use (int val = Serial.read() - '0'), it will not assign '1128' to it.

Of course not. Think about why you are subtracting '0' from the value that you read.

The value that is being send when you SerialPort1.Write(1) is ONE character. When you SerialPort1.Write(1128), how many characters are written to the serial port?

How many characters do you need to read?

if you keep the value transmitted to a byte (0 to 255) you can easily affect th brightness of the led this way:

if (Serial.aavailable()
{
  val = Serial.parseInt();
}
analogWrite(ledPin, val)

@BulldogLowell

I tested out the parse int method and its working!

Can you tell me what exactly does the parse int do?

But however there is still a small problem.

This is my test code :

int led = 11;

int val;

void setup(){
  pinMode(led,OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (Serial.available()>0){
    val = Serial.parseInt();
    Serial.println(val);
    analogWrite(led,val);
  }
}

If I enter 150 in the serial monitor, the serial monitor will output 150, and then after a second or two it outputs 0
and my led switches on at 150/255 brightness and and then switches off (because 0 is output ofcourse) after a second or two

how can i prevent the 0 from being output? and why is the 0 output?

What have you got the line ending set to in the Serial monitor ?

UKHeliBob:
What have you got the line ending set to in the Serial monitor ?

If by line ending you mean the first combo box on the bottom of the serial monitor, its set to 'Newline'

How does that affect anything?

I'm sorry I'm new to arduino so there's lots of things I dont understand

OK, I changed 'Newline' to 'No Line ending'

Now there is no 0 being output.

but there is a small delay between inputting to the serial monitor and the led switching On I (after the line ending change I made)

Can anybody explain why?

Can anybody explain why?

The timeout on parseInt.

AWOL:

Can anybody explain why?

The timeout on parseInt.

How do I fix the delay?

in setup()...

Serial.setTimeout(myDelay);

where myDelay is something in milliseconds, try 25 or 50 to start

OK.

Problem solved.

Thank you everybody!