Is button pressed? If yes how long? Won´t work

I want to use 3 pins as input. Just digital HIGH or LOW(Im using buttons while programming it). And afterwards write this into a database.

My problems are:

  1. It doesnt sense if/which button is pressed. Or how long it is pressed.

  2. It sends out some information, which are false and I dont know where they are coming from.

3.(Maybe not a part of this forum) I wrote a script in python to connect to the database and write the values. It works but it takes up to twenty seconds to connect and my whole arduino sketch rests in this time.

#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>            //Importieren der libraries um die Console und Bridge nutzen zu können
#include <Process.h>


Process p;

const int button1 = 8;
const int button2 = 9;
const int button3 = 10;





int Geraet=7890;             // Letzter Wert der LED

int buttonState1; 

int buttonState2;

int buttonState3;                     // Variable für Status des jeweiligen Tasters

int lastButtonState1=LOW;

int lastButtonState2=LOW;

int lastButtonState3=LOW;                 // Variable für vorhergehenden Status des Tasters


long startTime ;                    // Zeit, bis das Programm angefangen hat

long elapsedTime1 ;

long elapsedTime2;

long elapsedTime3;                  // Zeitspanne der Ausübung des Programmes(abzüglich startTime)

int fractional1;

int fractional2;

int fractional3;

int fractional4;                      // Variable für die Nachkommastellen

int x;

int y;

int z;                              //Variablen zum Übertrag von Zeiten

int average_lauf;

int gesamt1;

int t;

int average_ruest;

int gesamt2;

int u;

int average_stop;

int gesamt3;

int v;

int Sekunden1;

int Status;

void setup()

{
  Bridge.begin();                   //Kommunikation über WLAN
   Console.begin();                 //Verbindung über WLAN



pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);            //Deklaration der Pins
digitalWrite(button1, LOW);
digitalWrite(button2, LOW);
digitalWrite(button3, LOW);

}




void loop()

{

    // Prüfen, ob Knopf gedrückt wurde

   buttonState1 = digitalRead(button1);   
   buttonState2 = digitalRead(button2);
   buttonState3 = digitalRead(button3);
                                            // read the button state and store


 
 if (buttonState1==HIGH && lastButtonState1 == LOW){    

      Status=1;
        startTime= millis();
 lastButtonState1=buttonState1;
 
 
 }
  else if(buttonState1==LOW && lastButtonState1==HIGH){
        
        elapsedTime1 =   millis() - startTime;            
        Sekunden1= elapsedTime1/1000L;
        Console.print("Status: ");
        Console.println(Status);
        Console.print("Sekunden: ");
        Console.println(Sekunden1);                             
        gesamt1=(gesamt1+(elapsedTime1/1000));
        t++;
        average_lauf=(gesamt1/t);           

x=(elapsedTime1);
   
   }
else{
    lastButtonState1=buttonState1;
   }
   
 if (buttonState2==HIGH && lastButtonState2 == LOW){    

      Status=2;
        startTime= millis();
   
 lastButtonState2=buttonState2;
 
 
 }
  else if(buttonState2==LOW && lastButtonState2==HIGH){
        
        elapsedTime2 =   millis() - startTime;            
        Sekunden1= elapsedTime2/1000L;
        Console.print("Status: ");
        Console.println(Status);
        Console.print("Sekunden: ");
        Console.println(Sekunden1);                            
           gesamt2=(gesamt2+(elapsedTime2/1000));
        u++;
        average_ruest=(gesamt2/u);
                    

y=(elapsedTime2);
   
   }
else{
    lastButtonState2=buttonState2;
   }
   
if (buttonState3== HIGH && lastButtonState3== LOW){     
     

      Status=3;
      startTime = millis();                     
      lastButtonState3=buttonState3;
                                     

}
else if(buttonState3==LOW && lastButtonState3==HIGH){                                        

                         
      elapsedTime3 =   millis() - startTime;
    Sekunden1=elapsedTime3/1000L;
        Console.print("Status: ");
        Console.println(Status);
        Console.print("Sekunden: ");
        Console.println(Sekunden1);
           gesamt3=(gesamt3+elapsedTime3)/1000L;
        v++;
        average_stop=(gesamt3/v);
     Status=0;
   }

else{
    lastButtonState3=buttonState3;
   }
   



     


    

  Bridge.put("Kostenstelle", String(geraet));
  Bridge.put("Status", String(Status));
  Bridge.put("Zeit", String(Sekunden1));
  Bridge.put("average_L", String(average_lauf));
  Bridge.put("average_R", String(average_ruest));
  Bridge.put("average_S", String(average_stop));
  Bridge.put("gesamt_L", String(gesamt1));
  Bridge.put("gesamt_R", String(gesamt2));
  Bridge.put("gesamt_S", String(gesamt3));
  delay(1000);
  p.runShellCommand("python /root/start.py");
}

I know that it is not very good code right now.

I think it could be a problem with the if loops but cant find it.

#!/usr/bin/python
import sys, time
import MySQLdb

host = 'IP IP IP IP'
user = 'user user user'
passwd = 'pw pw pw'
db = 'db db db'


sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
bc=bridgeclient()

db = MySQLdb.connect(host, user, passwd, db)
cursor = db.cursor()

wert1 =bc.get('geraet')
wert2 = bc.get('Status')
wert3 = bc.get('Zeit')
wert4 = bc.get('average_L')
wert5 = bc.get('average_R')
wert6 = bc.get('average_S')
wert7 = bc.get('gesamt_L')
wert8 = bc.get('gesamt_R')
wert9 = bc.get('gesamt_S')

sql_c  ="""INSERT INTO geraete (geraet, Status, Zeit, m_Zeit_L, m_Zeit_R, m_Zeit_S,gesamt_L,gesamt_R,gesamt_S)VALUES("""+wert1+""","""+wert2+""","""+wert3+""","""+wert4+""","""+wert5+""","""+wert6+""","""+wert7+""","""+wert8+""","""+wert9+""")
ON DUPLICATE KEY UPDATE Status= """+wert2+""", Zeit="""+wert3+""", m_Zeit_L="""+wert4+""", m_Zeit_R="""+wert5+""", m_Zeit_S="""+wert6+""", gesamt_L="""+wert7+""", gesamt_R="""+wert8+""", gesamt_S="""+wert9+""""""
cursor.execute(sql_c)
bc.close()

I hope you find some time to give me a hint or two

It looks like your buttons are wired between the Arduino pin and the + rail (that is digitalRead returns HIGH when pressed). Have you got pull-down resistors on the buttons ? If not, that could explain the erratic behaviour you have described.

Incidentally, it more usual to wire buttons on the low side, so you can use the built in pull-up resistors.

Gigglerus:
I want to use 3 pins as input. Just digital HIGH or LOW(Im using buttons while programming it). And afterwards write this into a database.

My problems are:

  1. It doesnt sense if/which button is pressed. Or how long it is pressed.

Write a simple program to learn how to detect the button. Then when it works you can incorporate it into a larger program.

If it does not work it will be much easier to help with a short program.

Also, use the AutoFormat tool to lay your program out for easier reading.

And don't use single characters for variable names other than in FOR loops. When I try to highlight all of the instances of a variable called 'u' I get every 'u' in every word in the program.

The Python problem seems to be a Yun issue and there is a separate Yun section of the Forum.

...R

The variables with numeric suffixes are a big clue that the code is crying out for arrays to be used to avoid the repeated code.

Robin2:
Write a simple program to learn how to detect the button. Then when it works you can incorporate it into a larger program.

I did. And I translated it into this sketch. Do you think it was luck that it worked previously?

Robin2:
And don't use single characters for variable names other than in FOR loops. When I try to highlight all of the instances of a variable called 'u' I get every 'u' in every word in the program.

Okay, thank you for the information I will keep it in mind.

digitalWrite(button1, LOW);
digitalWrite(button2, LOW);
digitalWrite(button3, LOW);

Probably not what you want to do.

Gigglerus:

Write a simple program to learn how to detect the button

I did.

Post that simple program.

...R