I think I may have been looking at this thing too long and might be ignoring something obvious.
Here is what the code below does. I have it set so when I type in a number in a text box, it sends that number to the arduino. It then parrots that number back at me. If that number is 33 it turns the led on, if its 34, it turns the led off.
Whats happening, is that while the led seems t control fine, the data I am getting parroted back to me is odd. The first time I type something and hit send, my message box comes up, and displays 34 3 (if I typed in 45 it would show 45 3, 22 would show 22 3, ect). The second time I put in any number the message box show 0 0.
The code is mainly just a way for me to figure out how communication works, but at this point I am stumped.
const int DirPin = 3;
const int StepPin = 4;int a = 0;
int b = 0;
int c = 0;
int d = 0;
int val = 0;void setup(){
pinMode(DirPin, OUTPUT);
pinMode(StepPin, OUTPUT);Serial.begin(9600);
pinMode(8, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {a = Serial.read();
val = a;
Serial.println(val);
if (val == 33) {
digitalWrite (8, HIGH);
}if (val == 34) {
digitalWrite (8, LOW);
}
// if (val == 5001){
// digitalWrite (8, HIGH);
// }// if (val == 5002){
// digitalWrite (8, LOW);
// }}
Serial.flush();
}
Dim TestVar As String
Private Sub sendDataBtn_Click(sender As System.Object, e As System.EventArgs) Handles sendDataBtn.Click
TestVar = goToStepTextBox.Text
Dim btyeArr() As Byte = BitConverter.GetBytes(CInt(goToStepTextBox.Text))
If Not SerialPort1.IsOpen Then
SerialPort1.Open()
End If
SerialPort1.Write(btyeArr, 0, btyeArr.Length)
'SerialPort1.Close()
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim recStr As String = ""
Dim Datareceived As Boolean
Datareceived = False
While Not Datareceived
Dim recVal As Integer
recVal = SerialPort1.ReadByte()
If recVal = 10 Or recVal = 13 Then
Datareceived = True
Else
recStr = recStr + Chr(recVal)
End If
End While
MessageBox.Show(recStr & " " & CChar(recStr))
End Sub
End Class