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
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?
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?