Hi I am doing a learning project using arduino ATMEGA1280 and a LED and a small 1.5V motor. Essentially I wanted to see how I can send serial commands from VB.net and read that series of commands in Arduino and execute the commands. Here is what I did:
VB:
When you press a button on the form and a checkmark is ticked (or not ticked) following gets executed:
Try
With serialPort1
.PortName = "COM19"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.DtrEnable = True
.RtsEnable = True
.ReceivedBytesThreshold = 1
.Encoding = System.Text.Encoding.ASCII
.NewLine = Chr(13) + Chr(10)
End With
serialPort1.Open()
serialPort1.DiscardInBuffer()
Dim command As Byte()
Dim curspeed As Integer
command = {1, curspeed, 41}
serialPort1.Write(command, 0, command.Length - 1)
''''''''''''''''' {1, curspeed, 41} = {status of the LED=0 or 1, PWM speed for motor(0 to 255), end of command character}
Arduino:
void loop()
if (Serial.available()>0) {
light=int(Serial.read());
delay(100);
curspeed=int(Serial.read());
temp=Serial.read();
}
if (light==1) digitalWrite(52, HIGH); // there is a LED on pin 52
if (light==0) digitalWrite(52, LOW);
analogWrite(9, curspeed); //PWM pin9 is used to drive the motor
}
}
My question is:
If i dont use the delay(100) command after the "light=Serial.read();" line, this program doesnt work and "curspeed" gets a value of 255. This only works with a delay of any value put between the two Serial.read() commands. Why is this the case?
Also, if someone can point me to the maze of how these serial communications work, i would appreciate it very much. I am quiet confused as to the formats VB and arduino talk to each other in. I beleive VB is sending integers/characters and arduino is reading bytes and then converting them to integers or characters. I need to understand the serial communications protocols properly. Help is highly appreciated.
Cheers
Omi