Thank you so much johnwasser.I got the 4digit 7segment display with two shift registers module.you give me very excellent suggestion.now,i am going to try another code like below...
but.i still have another problem,that is how can i let the display always on.the code
ts2 += 5; // display-update interval = 5msec ,I don't know how to modify it...
#include <Four7Seg74hc595.h>
/*
* press button to counter increment
*/
esl::Four7Seg74hc595 display( 10,9,8 );
// Pin connected to Pin 14 of 74HC595 (Data=DIO,arduino pin8)
// Pin connected to Pin 12 of 74HC595 (Latch=RCLK,arduino pin9)
// Pin connected to Pin 11 of 74HC595 (Clock=SCLK,arduino pin10)
char sbuf[5];
uint16_t count;
uint32_t ts1, ts2;
const int buttonPin = 2; // (pushbutton)
const int relayPin = 13; // (Relay)
unsigned long detectingtime = 15000;
int buttonPushCounter = 0; //counter for the number of button presses
int buttonState = 0; //current state of the button
int lastButtonState = 0; //previous state of the button
void setup()
{
Serial.begin(9600); // Serial port, 9600 bps
pinMode(buttonPin, INPUT); //buttonPin set to INPUT
pinMode(relayPin, OUTPUT); // relayPin set to OUTPUT
for (uint8_t i=0; i < 100; i++) {
display.setDigits( "----", 4 );
display.update();
delay(10);
}
delay(1000);
buttonPushCounter = 0;
sprintf( sbuf, "%04u", buttonPushCounter );
display.setDigits( sbuf, 4 );
display.update();
ts1 = ts2 = millis();
}
uint32_t ts;
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
// check button(pressed)
// buttonState to HIGH
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
Serial.print("number of button pushesON: ");
Serial.println(buttonPushCounter,DEC);
ts = millis();
sprintf( sbuf, "%04u", buttonPushCounter );
if ( ts - ts2 >= 5 ) {
display.setDigits( sbuf, 4 );
display.update();
ts2 += 5; // display-update interval = 5msec
}
delay(15000);
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
}
else if (buttonState == LOW) {
// if the current state is LOW then the button
// went from on to off:
buttonPushCounter++;
Serial.print("number of button pushesOFF: ");
Serial.println(buttonPushCounter,DEC);
ts = millis();
sprintf( sbuf, "%04u", DEC);
if ( ts - ts2 >= 5 ) {
display.setDigits( sbuf, 4 );
display.update();
ts2 += 5; // display-update interval = 5msec
}
delay(3000);
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
}