Value storage problem of a variable in Arduino: it reads a value and saves for two cycles, is there any way to solve this?

Asked

Viewed 94 times

1

My project consists of an Arduino program using the RFID module, to read card frequencies, to release a ratchet (similar to buses). A program in Python will read this frequency printed in the Arduino serial and will in a database validate: if the frequency exists, it returns '1' pro Arduino; if it does not exist, it returns 0.

In Arduino, I have a variable that getting 1 releases the ratchet, and getting 0 blocks:

if (Serial.available())
  {
  validacao = Serial.read();
  Serial.parseInt();
  }

if (validacao == '1')
  {
    ...Liberado...
  }

else if (validacao == '0')
  {
    ...Bloqueado...
  }

And the Python validation program:

import mysql.connector 
import serial 
import time 

#-------------------CONEXAO COM BANCO-------------------#
conexao_banco_de_dados = mysql.connector.connect(host="localhost", user="root", passwd="1202", db="Arduino") 

validar = conexao_banco_de_dados.cursor()

validar.execute("SELECT frequencia FROM usuarios") 

dados_frequencia = [i[0] for i in list(validar)]
#-------------------------------------------------------#

#-------------------CONEXAO SERIAL----------------------#
conexao_monitor_serial_arduino = serial.Serial('/dev/ttyUSB0', 9600, timeout=1.7)
#-------------------------------------------------------#

#---------------------MAIN------------------------------#

print('\n{}Programa em andamento!{} \nPrecione {}Ctlr + Alt - C{} para interromper.\n'.format('\033[1m', '\033[0m', '\033[3m', '\033[0m'))

while 1:
    uid = conexao_monitor_serial_arduino.readline()

    if (uid in dados_frequencia):
        conexao_monitor_serial_arduino.write('1')  
        print ("1")

    if (uid not in dados_frequencia):       
        conexao_monitor_serial_arduino.write('0')  
        print ("0")

The problem is that when I swipe a card, the Arduino stores its variable for two cycles, when it was only supposed to be in one cycle. Example: X card passes and Y card does not pass. If I pass the X card first, Python sends 1 to Arduino and stores. From here, all the card that is registered passes normally. But if I decide to pass the Y card, which is not registered,Python normally sends 0, but Arduino first releases, and the second time I pass already blocks all normal; and so on. It has how to fix this storage method of Arduino not to need to pass the card twice to be the right procedure?

1 answer

0

Oops, nice to see that you followed the tip I gave you to send only the UID by serial and wait for the answer if it exists or not, and no longer recording the database Uids in the memory of the Norwegian.
About your current problem you could clear the variable right after releasing on the Arduin, thus:

if (validacao == '1')
{
  ...Liberado...
  //Depois que fez a liberacao na catraca seta validacao pra 0.
  validacao = 0;
}

else if (validacao == '0')
{
  ...Bloqueado...
}
  • Oops, thanks for before! About this solution, I’ve tried. It doesn’t work.

  • https://github.com/Neoiluminista/catraca/blob/Integra%C3%A7%C3%A3o-Incompleta/integracao_enxuta_buggada.Ino

  • It works with a fixed one. I did both with card without database, as using the program in Python without card. When it is fixed, there is no storage problem as it will not change constantly.

  • It worked, replace the if availible by a while and increased the range of the keys. Only enter the link if you want to see the differences. Now the project is acceptable, but still bugged. It first has to receive a frequency, so the internal RX LED is flashing very fast. Takes out the card, and 2s after the internal X1 LED flashes; there yes you can approach the card and it locks or frees right. The bug is that it always blocks first, then it runs right automatically. So, if it is a card that releases, it blocks -> releases; if it is a card that blocks, it blocks -> blocks.

  • I’ll be working right now to fix this, so you don’t always cancel first and you don’t have to put the card twice.

  • I solved the problem of having to put the card of times. Just print a string in the serial. But the lock always first remains.

  • @Noahsaulz, I’m glad you were able to solve the previous problem. To avoid a create a comment-only text we will delete the old comments and if possible post the solution to your own question, or if you prefer, you can delete it because leaving it unanswered has no sense.

  • Okay, as soon as I can get the last thing out (always block before), I put the solution.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.