Forum - C# / VB.NET
- info tecniche
Pagine: [ 1 2 3 ]
|
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
buongiorno a tutti, sto continuando con il mio progetto, ho ampliato il mio progetto aggiungendo un vumeter x l audio, la stazione radio e la possibilità di vedere i video, adesso ho aggiunto il collegamento con arduino e più precisamento il controllo del volume con un potenziometro, il tutto funziona ma non so perchè la tackbar del volume se uso arduino si attiva e disattiva in modo irregolare,
mi spiego meglio, la trackbar del volume è attivata attraverso 2 timer, uno x visualizzarla e uno x nasconderla, e se uso i button + e - non ci sono problemi, ma quando attivo arduino i timer si attivano e disattivano in modo irregolare, questo è dovuto dal potenziometro?
posto i vari codice
Codice sorgente - presumibilmente VB.NET
Private Sub Button3_Click_1( sender As Object , e As EventArgs) Handles Button3.Click
trc_volume.Value += 2
AxWindowsMediaPlayer1.settings .volume = trc_volume.Value
lb_volume.Text = trc_volume.Value .ToString + "%"
Timer3.Start ( )
End Sub
Private Sub Button4_Click_1( sender As Object , e As EventArgs) Handles Button4.Click
trc_volume.Value -= 2
AxWindowsMediaPlayer1.settings .volume = trc_volume.Value
lb_volume.Text = trc_volume.Value .ToString + "%"
Timer3.Start ( )
End Sub
Codice sorgente - presumibilmente VB.NET
Private Sub Timer3_Tick( sender As Object , e As EventArgs) Handles Timer3.Tick
Label1.Visible = True
trc_volume.Visible = True
lb_volume.Visible = True
Timer4.Start ( )
End Sub
Private Sub Timer4_Tick( sender As Object , e As EventArgs) Handles Timer4.Tick
Timer3.Stop ( )
Label1.Visible = False
trc_volume.Visible = False
lb_volume.Visible = False
End Sub
questo invece è il codice quando faccio la comunicazione seriale
Codice sorgente - presumibilmente VB.NET
Private Sub Timer1_Tick( sender As Object , e As EventArgs) Handles Timer1.Tick
Try
Dim i As Single = SerialPort1.ReadExisting
media_pleyer.lb_volume .Text = i.ToString + "%"
media_pleyer.trc_volume .Value = i.ToString
media_pleyer.Timer3 .Start ( )
media_pleyer.AxWindowsMediaPlayer1 .settings .volume = media_pleyer.trc_volume .Value
Catch ex As Exception
End Try
End Sub
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
questo è il codice su arduino, c'è anche il codice x visualizzare l orario su di un lcd i2c
Codice sorgente - presumibilmente C++
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd( 0x27,16,2) ;
RTC_DS3231 rtc;
DateTime now;
char str[ 3] ;
int stato1 = 0 ;
int ledPin = 9 ;
int analogPin = 0 ;
int val = 0 ;
void setup ( ) {
Serial.begin ( 9600) ;
lcd.backlight ( ) ;
lcd.init ( ) ;
Wire.begin ( ) ;
//pulsanti set e reset
pinMode( 6, INPUT) ;
pinMode( 7, INPUT) ;
pinMode( ledPin, OUTPUT) ;
if ( ! rtc.begin ( ) ) {
Serial.println ( "Couldn't find RTC" ) ;
while ( true ) ;
}
if ( ! rtc.lostPower ( ) ) {
Serial.println ( "RTC is NOT running, let's set the time!" ) ;
rtc.adjust ( DateTime( F( __DATE__) , F( __TIME__) ) ) ;
}
}
void loop ( ) {
ORA( ) ;
val = analogRead( analogPin) ;
analogWrite( ledPin, val / 10 - 1) ;
Serial.println ( val / 10 - 1) ;
delay( 100) ;
}
void ORA( ) {
switch ( stato1) {
case 0:
displayTime( ) ;
break ;
case 1:
setHour( ) ;
break ;
case 2:
setMinute( ) ;
break ;
case 3:
setSecond( ) ;
break ;
case 4:
setyear( ) ;
break ;
case 5:
setmonth( ) ;
break ;
case 6:
setday( ) ;
break ;
}
}
unsigned long t1, dt1;
bool FIRST = true ;
void displayTime( ) {
if ( FIRST) {
lcd.clear ( ) ;
t1 = millis( ) ;
FIRST = false ;
}
dt1 = millis( ) - t1;
if ( dt1 > 1000) {
now = rtc.now ( ) ;
lcd.setCursor ( 4,1) ;
sprintf ( str, "%02d" , now.hour ( ) ) ;
lcd.print ( str) ;
lcd.print ( ':' ) ;
sprintf ( str, "%02d" , now.minute ( ) ) ;
lcd.print ( str) ;
lcd.print ( ':' ) ;
sprintf ( str, "%02d" , now.second ( ) ) ;
lcd.print ( str) ;
lcd.setCursor ( 3,0) ;
sprintf ( str, "%02d" , now.day ( ) ) ;
lcd.print ( str) ;
lcd.print ( '/' ) ;
sprintf ( str, "%02d" , now.month ( ) ) ;
lcd.print ( str) ;
lcd.print ( '/' ) ;
sprintf ( str, "%02d" , now.year ( ) ) ;
lcd.print ( str) ;
t1 = millis( ) ;
}
if ( digitalRead( 6 ) ) {
//imposta ora
stato1 = 1 ;
delay( 300) ;
FIRST = true ;
}
}
int seth = 0 ;
void setHour( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
seth = now.hour ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta ora" ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , seth) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
seth++ ;
if ( seth >= 24) seth = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva ora scelta e passa ai minuti
rtc.adjust ( DateTime( now.year ( ) , now.month ( ) , now.day ( ) , seth, now.minute ( ) , now.second ( ) ) ) ;
stato1 = 2 ;
FIRST = true ;
delay( 200) ;
}
}
int setm;
void setMinute( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
setm = now.minute ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta minuti" ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , setm) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
setm++ ;
if ( setm >= 60) setm = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva min scelta e passa ai sec
rtc.adjust ( DateTime( now.year ( ) , now.month ( ) , now.day ( ) , now.hour ( ) , setm, now.second ( ) ) ) ;
stato1 = 3 ;
delay( 200) ;
FIRST = true ;
}
}
int sets = 0 ;
void setSecond( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
sets = now.second ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta sec." ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , sets) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
sets++ ;
if ( sets >= 60) sets = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva sec scelta e passa ai minuti
rtc.adjust ( DateTime( now.year ( ) , now.month ( ) , now.day ( ) , now.hour ( ) , now.minute ( ) , sets) ) ;
stato1 = 4 ;
delay( 200) ;
FIRST = true ;
}
}
int sety = 0 ;
void setyear( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
sety = now.year ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta anno" ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , sety) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
sety++ ;
if ( sety >= 9999) sety = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva sec scelta e passa ai minuti
rtc.adjust ( DateTime( sety, now.month ( ) , now.day ( ) , now.hour ( ) , now.minute ( ) , now.second ( ) ) ) ;
stato1 = 5 ;
delay( 200) ;
FIRST = true ;
}
}
int setmo = 0 ;
void setmonth( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
setmo = now.month ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta mese" ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , setmo) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
setmo++ ;
if ( setmo >= 12) setmo = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva sec scelta e passa ai minuti
rtc.adjust ( DateTime( now.year ( ) , setmo, now.day ( ) , now.hour ( ) , now.minute ( ) , now.second ( ) ) ) ;
stato1 = 6 ;
delay( 200) ;
FIRST = true ;
}
}
int setd = 0 ;
void setday( ) {
if ( FIRST) {
lcd.clear ( ) ;
now = rtc.now ( ) ;
setd = now.day ( ) ;
lcd.setCursor ( 0,0) ;
lcd.print ( "Imposta giorno" ) ;
FIRST = false ;
}
lcd.setCursor ( 0,1) ;
sprintf ( str, "%02d" , setd) ;
lcd.print ( str) ;
if ( digitalRead( 7) ) {
setd++ ;
if ( setd >= 31) setd = 0 ;
delay( 200) ;
}
if ( digitalRead( 6 ) ) {
//salva sec scelta e passa ai minuti
rtc.adjust ( DateTime( now.year ( ) , now.month ( ) , setd, now.hour ( ) , now.minute ( ) , now.second ( ) ) ) ;
stato1 = 0 ;
delay( 200) ;
FIRST = true ;
}
}
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
questo è il video del problema che ho, se qulcuno riesc a capirci qualcosa
grazie mille
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
Postato originariamente da nightwolf :
questo è il video del problema che ho, se qulcuno riesc a capirci qualcosa
grazie mille
Ultima modifica effettuata da nightwolf il 29/11/2023 alle 20:49
Thejuster (Admin )
Guru^2
Messaggi: 2338
Iscritto: 04/05/2008
Perché usi un timer per attivare una trackbar?
nightwolf (Normal User)
Pro
Messaggi: 153
Iscritto: 14/09/2010
i timer li uso per far si che si visualizzi e dopo 5 secondi si nascondono, li ho messi come visible=false di default
Thejuster (Admin )
Guru^2
Messaggi: 2338
Iscritto: 04/05/2008
il problema deve essere proprio li allora
perchè la tackbar del volume se uso arduino si attiva e disattiva in modo irregolare,
Prova ad usare i thread paralleli con Thread.Sleep ottenendo lo stesso risultato, ma con la sicurezza
che dopo l'esecuzione i thread viengono chiusi.
Ma ricorda di utilizzare il metodo Invoke.
Non sò in vb.net come si scrive penso carlo possa aiutarti.
in c# si esegue un Invoke sui controlli da un thread diverso da quello principale
Codice sorgente - presumibilmente C# / VB.NET
Thread t = new Thread(Avvio);
t.start();
void Avvio()
{
Thread.sleep(5000);
Invoke(new Action(delegate() { trc_volume.Visible = true; });
}
Altra cosa, bisogna vedere bene cosa legge il programma da arduino e cosa realmente fà.
Anche in quel caso, imposta un contatore o un breakpoint per sapere cosa succede.
Probabile che anche li ci sia un problema ed avvii piu volte la stessa operazione.
Ultima modifica effettuata da Thejuster il 01/12/2023 alle 4:14
Carlo (Member )
Guru^2
Messaggi: 1420
Iscritto: 29/01/2018
Postato originariamente da Thejuster :
Codice sorgente - presumibilmente C# / VB.NET
Thread t = new Thread(Avvio);
t.start();
void Avvio()
{
Thread.sleep(5000);
Invoke(new Action(delegate() { trc_volume.Visible = true; });
}
in VB .NET:
Codice sorgente - presumibilmente VB.NET
Dim t As New Threading.Thread ( AddressOf Avvio)
t.Start ( )
Private Sub Avvio( )
Threading.Thread .Sleep ( 5000)
Invoke( New Action( Sub ( )
trc_volume.Visible = true
End Sub ) )
End Sub
Ultima modifica effettuata da Carlo il 02/12/2023 alle 15:53
in programmazione tutto è permesso