Right, exactly. If one part of the string is this, do that etc. String comparison you call it... that'll cut my explanations short
Anyway, the vb.net code isn't that hard, there's a simple process that initializes the serial connection, and I sent the string using
Public s As New System.IO.Ports.SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
s.Close()
s.PortName = "COM4" 'will need to change to your port number
s.BaudRate = 128000
s.DataBits = 8
s.Parity = System.IO.Ports.Parity.None
s.StopBits = System.IO.Ports.StopBits.One
s.Handshake = System.IO.Ports.Handshake.None
s.Encoding = System.Text.Encoding.ASCII 'very important!
s.Open()
s.RtsEnable = True
End Sub
'this is the part that initializes the serial connection on start. Next, the sending on pressing a form button:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F Then
If inout = 0 Then
inout = 1
ElseIf inout = 1 Then
inout = 0
End If
End If
If e.KeyCode = Keys.K Then
Label1.Text &= e.KeyCode
s.WriteLine("<0;2;" & inout & ";")
Label1.Text = "<0;2;" & inout & ";"
End If
If e.KeyCode = Keys.U Then
Label1.Text &= e.KeyCode
s.WriteLine("<1;2;" & inout & ";")
Label1.Text = "<0;2;" & inout & ";"
End If
If e.KeyCode = Keys.I Then
Label1.Text &= e.KeyCode
s.WriteLine("<1;3;" & inout & ";")
Label1.Text = "<0;2;" & inout & ";"
End If
If e.KeyCode = Keys.O Then
Label1.Text &= e.KeyCode
s.WriteLine("<2;3;" & inout & ";")
Label1.Text = "<0;2;" & inout & ";"
End If
If e.KeyCode = Keys.L Then
Label1.Text &= e.KeyCode
s.WriteLine("<0;3;" & inout & ";")
End If
If e.KeyCode = Keys.J Then
Label1.Text &= e.KeyCode
s.WriteLine("<1;0;" & inout & ";")
End If
End Sub
'the inout here is the red/green switch, i let the arduino treat that with an if.
As you said, I sent a string, turned it into characters in the arduino, and then compared them to get the correct pins to turn on. I also found the file on my disc, lodged somewhere I forgot I put it xD Here's the code on the arduino:
#include <stdio.h>
#include <stdlib.h>
char LedOn[10];
char LedOff[10];
int pin[16];
int t=0;
int on=0;
int off=0;
void setup(){
Serial.begin(128000);
pinMode(23,OUTPUT);
pinMode(27,OUTPUT);
pinMode(33,OUTPUT);
pinMode(35,OUTPUT);
pinMode(49,OUTPUT);
pinMode(53,OUTPUT);
pinMode(19,OUTPUT);
pinMode(15,OUTPUT);
pinMode(2,OUTPUT);
pin[0]=15;
pin[1]=2;
pin[2]=19;
pin[3]=23;
pin[4]=27;
pin[5]=33;
pin[6]=35;
pin[7]=49;
pin[8]=53;
}
void loop(){
if (t<2){
test();
delay(1000);
t++;}
SerialRead();
Serial.flush();
if (on==1){
makeemshine();}
}
void test(){
for (int i=3;i<9;i++){
digitalWrite(pin[i],HIGH);}
for (int j=0;j<10000;j++){
for (int i=0;i<3;i++){
digitalWrite(pin[i],HIGH);
if (i==0){
digitalWrite(pin[3],LOW);
digitalWrite(pin[3],HIGH);
digitalWrite(pin[4],LOW);
digitalWrite(pin[4],HIGH);
}
if (i==1){
digitalWrite(pin[5],LOW);
digitalWrite(pin[5],HIGH);
digitalWrite(pin[6],LOW);
digitalWrite(pin[6],HIGH);
}
if (i==2){
digitalWrite(pin[7],LOW);
digitalWrite(pin[7],HIGH);
digitalWrite(pin[8],LOW);
digitalWrite(pin[8],HIGH);
}
}}}
void turnoff(){
for (int i=2;i<54;i++){
digitalWrite(i,HIGH);}}
void LEDon(int tipka,int smer){
if (smer==0){
if (tipka==1){
digitalWrite(pin[0],HIGH);
digitalWrite(pin[3],LOW);}
if (tipka==2){
digitalWrite(pin[1],HIGH);
digitalWrite(pin[5],LOW);}
if (tipka==3){
digitalWrite(pin[2],HIGH);
digitalWrite(pin[7],LOW);}}
if (smer==1){
if (tipka==1){
digitalWrite(pin[4],LOW);}
if (tipka==2){
digitalWrite(pin[6],LOW);}
if (tipka==3){
digitalWrite(pin[8],LOW);}}}
void LEDoff(int tipka){
if (tipka==1){
digitalWrite(pin[0],HIGH);
digitalWrite(pin[3],HIGH);
digitalWrite(pin[4],HIGH);}
if (tipka==2){
digitalWrite(pin[1],HIGH);
digitalWrite(pin[5],HIGH);
digitalWrite(pin[6],HIGH);}
if (tipka==3){
digitalWrite(pin[2],HIGH);
digitalWrite(pin[7],HIGH);
digitalWrite(pin[8],HIGH);}}
void SerialRead(){
if (Serial.available()>0){
char s=Serial.read();
if (char(s)=='<'){
on=1;
int k=0;
while (Serial.available()>0){
LedOn[k]=Serial.read();
delay(100);
k++;
}Serial.flush();}
if (char(s)=='!'){
on=0;
Serial.flush();
// int k=0;
// while (Serial.available()){
// LedOff[k]=Serial.read();
// k++;
}}}
void makeemshine(){
int tipka1=int(LedOn[0])-48;
int tipka2=int(LedOn[2])-48;
int vennot=int(LedOn[4])-48;
LEDon(tipka1,vennot);
delay(5);
LEDoff(tipka1);
LEDon(tipka2,vennot);
delay(5);
LEDoff(tipka2);}
As you can see, I let the arduino do most of the work. How do I handle byte stream with the arduino, that would save me a lot of code and processing?