Cesar_Morisco
Active Member
Hello everyone
I am designing a washing machine control using esp8266
and the 3-pin encoder I have some problems with the menu function getting stuck
Here is the code
Could anyone tell me what is blocking the menu?
Thanks
I am designing a washing machine control using esp8266
and the 3-pin encoder I have some problems with the menu function getting stuck
Here is the code
Could anyone tell me what is blocking the menu?
Thanks
B4R:
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private server As WiFiServerSocket
Private astream As AsyncStreams
Private timer1 As Timer
Private timer2 As Timer
'Private timer3 As Timer
Private ssd As AdafruitSSD1306
Private ESPin As D1Pins
Private LED As Pin
Private BUZZ As Pin
Private Relays As Int = 9
Private Registers(Relays) As Boolean
Private DS,STCP,SHCP As Pin
Private bc As ByteConverter
Private onewire As OneWire
Private ssd As AdafruitSSD1306
Dim Sensor_Pres As Pin
Dim Start As Pin
Dim EncoderPinA As Pin
Dim EncoderPinB As Pin
Private LastStateTime As Long = 0
Private Counter As Int = 5
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
'
' ' Inicializa os pinos como entrada
EncoderPinA.Initialize(9, EncoderPinA.MODE_INPUT_PULLUP) ' Inicializa o botão 1 com pull-up interno
EncoderPinB.Initialize(10, EncoderPinB.MODE_INPUT_PULLUP)' Inicializa o botão 2 com pull-up interno
EncoderPinA.AddListener("EncoderPinA_Changed")'Enconder de 3 pinos
Sensor_Pres.Initialize(ESPin.D3,Sensor_Pres.MODE_INPUT_PULLUP) 'Inicializa o botão 3 com pull-up interno
Start.Initialize(ESPin.D4,Start.MODE_INPUT_PULLUP) 'Inicializa o botão 4 com pull-up interno
ssd.InitializeI2C(ESPin.D6,0x3C) 'initialise SSD1306 0x78
LED.Initialize(ESPin.D7,LED.MODE_OUTPUT)
BUZZ.Initialize(ESPin.D8,BUZZ.MODE_OUTPUT)
BUZZ.DigitalWrite(True)
Delay(100)
BUZZ.DigitalWrite(False)
LED.DigitalWrite(False)
DS.Initialize(ESPin.D0,DS.MODE_OUTPUT) 'HS Pin 11 Serial In ESP D6 GPIO 12
STCP.Initialize(ESPin.D5,STCP.MODE_OUTPUT) 'HS Pin 12 RCK ESP D5 GPIO 14
SHCP.Initialize(ESPin.D6,SHCP.MODE_OUTPUT) 'HS Pin 14 Serial Clock ESP D7 GPIO 16
'Clear Register Values
ClearAllReg
ssd.InitializeI2C(ESPin.D6,0x3C) 'initialise SSD1306 0x78
ssd.ClearDisplay
ssd.GFX.SetCursor(0, 0)
ssd.GFX.ConfigureText(1, ssd.WHITE, False)
ssd.GFX.DrawText("Inicializando...").DrawText(CRLF).DrawText(CRLF)
ssd.Display
'example of connecting to a local network
If wifi.Connect2("..........................","..................................") Then
Log(wifi.LocalIp)
ssd.ClearDisplay
ssd.GFX.SetCursor(0, 0)
ssd.GFX.ConfigureText(1, ssd.WHITE, False)
ssd.GFX.DrawText("Conectado Wifi").DrawText(CRLF).DrawText(CRLF)
ssd.Display
Delay(2000)
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(1,ssd.WHITE,False)
ssd.GFX.DrawText("IP:")
ssd.GFX.DrawText(wifi.LocalIp)
ssd.Display
LED.DigitalWrite(True)
Else
Log("Falha ao se Conectatr.")
Return
End If
onewire.Initialize(ESPin.D4)
timer1.Initialize("Menu",100) 'Menu de função
timer1.Enabled = True
timer2.Initialize("Start_Ente",200) 'Start Enter
server.Initialize(80, "server_NewConnection")
server.Listen
End Sub
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
Log("Cliente Conectado")
astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub
Sub astreamCom_Error
Log("error")
End Sub
Sub Astream_NewData (Buffer() As Byte)
Log(Buffer)
Dim value As String = bc.StringFromBytes(Buffer)
Select value
Case "CMDA"
Log("OI BONITÃO")
End Select
WriteToReg
End Sub
Sub AStream_Error
Log("Desconectado")
server.Socket.Close
server.Listen
End Sub
' Método chamado quando o pino A do encoder muda de estado
Sub EncoderPinA_Changed (State As Boolean)
Dim StateTime As Long = Millis()
If StateTime - LastStateTime > 200 Then 'Only run if statechange firing interval is larger than 50ms, adjust to suit
If EncoderPinB.DigitalRead Then Counter = Counter - 1 Else Counter = Counter + 1 'Adjust the counter +-1
Counter = Min(21, Max(4, Counter)) 'Limit the counter between 0 and 100
LastStateTime = StateTime 'Store last state time
Log("Position: ", Counter)
End If
End Sub
Sub Menu'Ligar o Menu Timer 1
Select Counter
Case 5
Log("Menu 1")
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PROGRAMAS:")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PESADO")
ssd.Display
If Start.DigitalRead = False Then ' Botão pressionado (leitura é LOW devido ao pull-up)
timer2.Enabled = True 'Botão Start Enter
End If
Case 10
Log("Menu 2")
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PROGRAMAS:")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("NORMAL")
ssd.Display
If Start.DigitalRead = False Then ' Botão pressionado (leitura é LOW devido ao pull-up)
timer2.Enabled = True 'Botão Start Enter
End If
Case 15
Log("Menu 3")
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PROGRAMAS:")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("RAPIDO")
ssd.Display
If Start.DigitalRead = False Then ' Botão pressionado (leitura é LOW devido ao pull-up)
timer2.Enabled = True 'Botão Start Enter
End If
Case 20
Log("Menu 4")
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PROGRAMAS:")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("DIA A DIA")
ssd.Display
If Start.DigitalRead = False Then ' Botão pressionado (leitura é LOW devido ao pull-up)
timer2.Enabled = True'Botão Start Enter
End If
End Select
End Sub
Sub Start_Ente'Start Enter Timer 2
timer1.Enabled = False'Menu
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("PESADO.")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("Tempo 1:03")
ssd.Display
Registers(1) = True 'Abrir Valvola
WriteToReg
Delay(600)
If Sensor_Pres.DigitalRead = False Then ' Botão pressionado (leitura é LOW devido ao pull-up)
Lavagem_Normal
Registers(1) = False'Abrir Valvola
WriteToReg
End If
End Sub
'
Private Sub Lavagem_Normal()
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("Lavendo.")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("Aguarde...")
ssd.Display
Registers(1) = False'Fechar Valvola
WriteToReg
Delay(3000)
For i = 0 To 20
Registers(2) = True 'Ligar Motor
WriteToReg
Delay (200) 'Delei de 2 Mile Segundos
Registers(2) = False 'Desligar Motor
WriteToReg
Delay (200) 'Delei de 2 Mile Segundos
Registers(3) = True 'Ligar Motor
WriteToReg
Delay (200) 'Delei de 2 Mile Segundos
Registers(3) = False'Desligar Motor
WriteToReg
Delay (200) 'Dele de 2 segundo
Next
Delay(10000) 'PAUSA DE 1 SEGUNDO
For i = 0 To 20
Registers(2) = True 'Ligar Motor
WriteToReg
Delay (200) 'Delei de 2 Mile Segundos
Registers(2) = False 'Desligar Motor
WriteToReg
Delay(200) 'Delei de 2 Mile Segundos
Registers(3) = True 'Ligar Motor
WriteToReg
Delay (200) 'Delei de 2 Mile Segundos
Registers(3) = False'Desligar Motor
WriteToReg
Delay (200) 'Dele de 2 segundo
Next
Centrifugar
End Sub
Private Sub Centrifugar()
ssd.ClearDisplay
ssd.GFX.SetCursor(2,0)
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("Centrifugando.")
ssd.GFX.SetCursor(2,18)'18
ssd.GFX.ConfigureText(2,ssd.WHITE,False)
ssd.GFX.DrawText("Aguarde...")
ssd.Display
Registers(4) = True 'Ligar Motor do dreno
WriteToReg
Delay(6000)'Aguardar
Delay(6000)'Aguardar
Registers(3) = True 'Ligar Motor esquerdo
WriteToReg
Delay(6000)
Delay(6000)
Registers(4) = False 'Desligar Motor do dreno
WriteToReg
Registers(3) = False'Desligar Motor esquerdo
WriteToReg
For I = 0 To 5 'LIGAR O BUZZER DEPOIS DE TERMINA A LAVANGEM
BUZZ.DigitalWrite(True)
Delay(200)
BUZZ.DigitalWrite(False)
Delay(200)
BUZZ.DigitalWrite(True)
Delay(200)
BUZZ.DigitalWrite(False)
Next
timer1.Enabled = True 'Voltar para o menu
timer2.Enabled = False 'Desligar Start Enter
End Sub
'Write To Registers
Private Sub WriteToReg()
STCP.DigitalWrite(False)
Dim i As Int = 0
Do While i <= Relays - 1
SHCP.DigitalWrite(False)
DS.DigitalWrite(Registers(i))
SHCP.DigitalWrite(True)
i = i + 1
Loop
STCP.DigitalWrite(True)
End Sub
'Clear All Registers
Private Sub ClearAllReg()
Dim i As Int = Relays - 1
Do While i >= 0
Registers(i) = False 'True = NO
i = i - 1
WriteToReg
Loop
End Sub