eccoti il codice di VB:
Public Class Form1
Private Sub Button2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
Timer1.Start() ' se pulsante premuto setto il timer
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Not SerialPort1.IsOpen Then 'apro la comunicazione seriale seriale se chiusa
SerialPort1.Open()
End If
If SerialPort1.IsOpen() Then
SerialPort1.Write(2) ' se porta aperta scrivo 2 ad arduino
End If
If SerialPort1.ReadChar() = 50 Then ' ricevo 2 in ASCII (50)
Label1.Text = ("Botton 2 premuto") 'scivo su label1
End If
SerialPort1.Close() ' chiudo comunicazione seriale
End Sub
Private Sub Button2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp
Timer1.Stop() ' se tasto rilasciato fermo il timer
End Sub
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
Timer2.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If Not SerialPort1.IsOpen Then
SerialPort1.Open()
End If
If SerialPort1.IsOpen() Then
SerialPort1.Write(1)
If SerialPort1.ReadChar() = 49 Then
Label1.Text = ("Botton 1 premuto")
End If
End If
SerialPort1.Close()
End Sub
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
Timer2.Stop()
End Sub
Private Sub Button3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button3.MouseDown
Timer3.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If Not SerialPort1.IsOpen Then
SerialPort1.Open()
End If
If SerialPort1.IsOpen() Then
SerialPort1.Write(3)
End If
If SerialPort1.ReadChar() = 51 Then
Label1.Text = ("Botton 3 premuto")
End If
SerialPort1.Close()
End Sub
Private Sub Button3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button3.MouseUp
Timer3.Stop()
End Sub
Private Sub Button4_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button4.MouseDown
Timer4.Start()
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
If Not SerialPort1.IsOpen Then
SerialPort1.Open()
End If
If SerialPort1.IsOpen() Then
SerialPort1.Write(4)
End If
If SerialPort1.ReadChar() = 52 Then
Label1.Text = ("Botton 4 premuto")
End If
SerialPort1.Close()
End Sub
Private Sub Button4_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button4.MouseUp
Timer4.Stop()
End Sub
End Class
e il codice di arduino:
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7);
int lettura;
const int Led = 22;
//unsigned long num_milli;
//unsigned long secondi = 0;
unsigned long last = millis();
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(2,0);
lcd.print("Valore tasto");
pinMode(Led,OUTPUT);
}
void loop() {
lcd.setCursor(5,1);
if(Serial.available() > 0){
lettura = Serial.read();
Serial.write(lettura);
if (lettura == 49) {
lcd.print("Tasto1");
}
if (lettura == 50){
digitalWrite(Led,HIGH);
lcd.print("Tasto2");
last = millis();
}
if (lettura == 51){
lcd.print("Tasto3");
}
if (lettura == 52){
lcd.print("Tasto4");
}
}
if (millis() - last > 150) {
digitalWrite (Led, LOW);
}
}
Non è sicuramente il massimo
Questo è il risultato della mia prima settimana di programmazione