Project (1 button and display7seg)

im new at this and i am having troubles doing a project in which involves a button when pressed, shows a letter in the display, and when its pressed again the letter "disapears".

how do i do this?

Look at the state change detection tutorial to find out how to detect when the button becomes pressed. The first time that the button is pressed, write the letter to the display, the next time the button is pressed, write a space or spaces to erase the letter.

Here is an example using the state change detection method shows how one could do the above. Replace the serial prints with the functions to write to the display:

// by C Goulding aka groundFungus

const byte  buttonPin = 4;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin));   // toggle the output
            if(digitalRead(ledPin) == HIGH)
            {
               Serial.println("print a letter to display");
            }
            else
            {
               Serial.println("print space(s) to erase letter\n");
            }
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}

@groundFungus;
You might want to add "connect button between input pin and GND".
You KNOW beginners will automatically connect their button between input pin and Vcc. ::slight_smile: :wink:

i´ve figure it out, thanks for the help but now i press the button and the letter apears, but when i let go the button the letter disapears

It is difficult for us to help fix code that we cannot see. Post your code as described in the forum guidelines.

The example code that I posted was tested and is known to work as described.

Also, please, post a diagram of your wiring.

i dont have a diagram wiring because we are using arduino with the shield

#define OE2 8  //Atribui valor do nome constante ao pino digital 8
#define OE1 7  //Atribui valor do nome constante ao pino digital 7
#define LE2 10 //Atribui valor do nome constante ao pino digital 10
#define LE1 9  //Atribui valor do nome constante ao pino digital 9
#define Clear 11   //Atribui valor do nome constante ao pino digital 11
#define SClk 12   //Atribui valor do nome constante ao pino digital 12
#define SData 13   //Atribui valor do nome constante ao pino digital 13

#define Key0 A2
byte buttonState = 0;
byte lastbuttonState =0;

#define Key1 A3
byte buttonState1 = 0;
byte lastbuttonState1 =0;

#define Key2 A4
byte buttonState2 = 0;
byte lastbuttonState2 = 0;

byte contagem=0;  // Definição e inicialização a zero da variável contagem

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 20;    // the debounce time; increase if the output flickers

unsigned long lastDebounceTime1 = 0;  // the last time the output pin was toggled
unsigned long debounceDelay1 = 20;    // the debounce time; increase if the output flickers

unsigned long lastDebounceTime2 = 0;  // the last time the output pin was toggled
unsigned long debounceDelay2 = 20;    // the debounce time; increase if the output flickers

void setup() {   //
  pinMode(Key0,INPUT);
  pinMode(Key1,INPUT);
  pinMode(Key2,INPUT);
  pinMode(OE2,OUTPUT);   //configuracao do pino digital OE2 como saída
  pinMode(OE1,OUTPUT);   //configuracao do pino digital OE1 como saída
  pinMode(LE2,OUTPUT);   //configuracao do pino digital LE2 como saída
  pinMode(LE1,OUTPUT);   //configuracao do pino digital LE1 como saída
  pinMode(Clear,OUTPUT);   //configuracao do pino digital Clear como saída
  pinMode(SClk,OUTPUT);   //configuracao do pino digital SClk como saída
  pinMode(SData,OUTPUT);   //configuracao do pino digital SData como saída
  digitalWrite(LE1, LOW);
  digitalWrite(OE1,HIGH);  //Desativa o display OE1 com valor lógico 1
  digitalWrite(Clear, HIGH); 
}

void loop() {
  buttonState = digitalRead(Key0);
  buttonState1 = digitalRead(Key1);
  buttonState2 = digitalRead(Key2);


  if(buttonState != lastbuttonState){
    lastDebounceTime = millis();
    if(buttonState == HIGH){
      contagem++; 
      
    }
  }
  if((millis() - lastDebounceTime) > debounceDelay){
    if(buttonState != lastbuttonState){ 
    lastbuttonState = buttonState;  
    }
  }
  
    if(buttonState1 != lastbuttonState1){
    lastDebounceTime1 = millis();
    if(buttonState1 == HIGH){
      contagem--;
      
    }
    }
    
  if((millis() - lastDebounceTime1) > debounceDelay1){
    if(buttonState1 != lastbuttonState1){
    lastbuttonState1 = buttonState1;
    }
  } 
  buttonState2 = digitalRead(Key2);
  if(buttonState2 == HIGH){ 
    shiftOut(13, 12, MSBFIRST, 0x86);
    lastbuttonState2 = buttonState2;
      }  
    
  if((millis() - lastDebounceTime2) > debounceDelay2){
    if(buttonState2 != lastbuttonState2){ 
    lastbuttonState2 = buttonState2;
    
    }
  }
  
  Display7Seg(contagem);
}



void Display7Seg(byte valor){ 

  digitalWrite(OE2,LOW);   
  digitalWrite(LE2,HIGH);   
  if(valor == 0) shiftOut(13, 12, MSBFIRST, 0xC0); //Mostra o número 0 no display
  if(valor == 1) shiftOut(13, 12, MSBFIRST, 0x79); //Mostra o número 1 no display
  if(valor == 2) shiftOut(13, 12, MSBFIRST, 0x24); //Mostra o número 2 no display
  if(valor == 3) shiftOut(13, 12, MSBFIRST, 0x30); //Mostra o número 3 no display
  if(valor == 4) shiftOut(13, 12, MSBFIRST, 0x19); //Mostra o número 4 no display
  if(valor == 5) shiftOut(13, 12, MSBFIRST, 0x12); //Mostra o número 5 no display
  if(valor == 6) shiftOut(13, 12, MSBFIRST, 0x02); //Mostra o número 6 no display
  if(valor == 7) shiftOut(13, 12, MSBFIRST, 0x78); //Mostra o número 7 no display
  if(valor == 8) shiftOut(13, 12, MSBFIRST, 0x00); //Mostra o número 8 no display
  if(valor >= 9) shiftOut(13, 12, MSBFIRST, 0xC7); //Mostra o número 9 no display
  digitalWrite(OE2,HIGH);   //Ativa output enable com valor Low
  digitalWrite(LE2,LOW);   //Ativa RCLK Input Com valor High
        
  lastbuttonState = buttonState; // estado anterior do botão é igual ao estado atual do botão de incremento 
 
  lastbuttonState1 = buttonState1; // estado anterior do botão é igual ao estado atual do botão de decremento

  lastbuttonState2 = buttonState2;
}