Ir para conteúdo
  • Cadastre-se

dúvida 1 botao 6 leds

Avalie este tópico:


baporc

Posts em destaque

Parece que é no Arduíno que você quer programar a função acima.

Esses artigos podem te ajudar:

Você não tem permissão para ver links. Faça login ou cadastre-se.

 

Editado: por mariomendes77
Link para o comentário
Compartilhar em outros sites

Exemplo de código de botão:

Você não tem permissão para ver links. Faça login ou cadastre-se.



"

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

 
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
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() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {           <<< BEM SUGESTIVO ESTE TRECHO DE CÓDIGO!!!
    digitalWrite(ledPin, HIGH);                     <<< PODE MUDAR ESTA CONDIÇÃO PARA A Q DESEJA!!!
  } else {                                                          <<< SUGESTÃO DE USO DE UM SWITCH/CASE OU IF ANINHADOS!!!
    digitalWrite(ledPin, LOW);
  }


}

"

 

Flw...

Editado: por nagkiller
Link para o comentário
Compartilhar em outros sites

Talvez seja isso que desejava:

"

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int led1 = 13;       // the pin that the LED is attached to
const int led2 = 12;       // the pin that the LED is attached to
const int led3 = 11;       // the pin that the LED is attached to
const int led4 = 10;       // the pin that the LED is attached to
const int led5 = 9;       // the pin that the LED is attached to
const int led6 = 8;       // the pin that the LED is attached to

// Variables will change:
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() {
  //Delay for reprogram the mcu
  delay(5000);
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LEDs as an output:
  DDRB = DDRB | B11111111;
  // Particularmente eu sempre deixo os pinos a primeira vez setados em LOW.
  PORTB = B00000000;
  Serial.begin(19200);
  Serial.println("Button 6x1");
}

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      seeTheResult();
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

void seeTheResult() {
  switch (buttonPushCounter) {
    case 1:
      digitalWrite(led1, digitalRead(led1) ^ 1);
      Serial.print("LED1");
      break;
    case 2:
      digitalWrite(led2, digitalRead(led2) ^ 1);
      Serial.print("LED2");
      break;
    case 3:
      digitalWrite(led3, digitalRead(led3) ^ 1);
      Serial.print("LED3");
      break;
    case 4:
      digitalWrite(led4, digitalRead(led4) ^ 1);
      Serial.print("LED4");
      break;
    case 5:
      digitalWrite(led5, digitalRead(led5) ^ 1);
      Serial.print("LED5");
      break;
    case 6:
      digitalWrite(led6, digitalRead(led6) ^ 1);
      Serial.print("LED6");
      buttonPushCounter = 0;
      break;
  }
  Serial.println(" mudou o status...");
}

"

Editado: por nagkiller
Melhorias no código com a visualização na Serial Port
Link para o comentário
Compartilhar em outros sites

  • 1 mês depois...

A forma mais simples é com uma interrupção. Veja como fazer nesse link.

Você não tem permissão para ver links. Faça login ou cadastre-se.

 

byte ledPin = 3;//ledPin1 = 3;ledPin2 = 4;ledPin3 = 5;ledPin4 = 6; ledPin5 = 7; ledPin6 = 8;
const byte interruptPin = 2;
volatile byte state = LOW;


void setup() {

  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {  /////chama esta função quando o botão ir de 0(0v) para 1(5v ou 3,3)
  if(ledPin==3)state=HIGH;
  else if(ledPin>3 &&ledPin<9)ledPin++;
  else{
      state=LOW;
      for(byte i=ledPin;i<3;i--)digitalWrite(i, state);
      ledPin = 3;
  }
}

Acredito que funcione... não testei.

Link para o comentário
Compartilhar em outros sites

  • 4 meses depois...

Participe agora da conversa!

Você pode postar agora e se cadastrar mais tarde. Se você tiver uma conta, faça login para postar com sua conta.

Visitante
Responder

×   Você colou conteúdo com formatação.   Restaurar formatação

  Apenas 75 emoticons máximos são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir apenas como um link

×   Seu conteúdo anterior foi restaurado.   Limpar o editor

×   Você não pode colar imagens diretamente. Envie ou insira imagens do URL.

SOBRE O ELETRÔNICABR

EletrônicaBR é o melhor fórum técnico online, temos o maior e mais atualizado acervo de Esquemas, Bios e Firmwares da internet. Através de nosso sistema de créditos, usuários participativos têm acesso totalmente gratuito. Os melhores técnicos do mundo estão aqui!
Técnico sem o EletrônicaBR não é um técnico completo! Leia Mais...
×
×
  • Criar Novo...