Help With ++

Hi guys! I need some help in a project that i'm tryng to finish

I need to make some (7) led's to turn on in sequence when I press a button ONLY (Button = LOW: No increment) as a item counter, but I always get stumped.

Using a UNO, Leds, Resistors e etc.

Logic:

Button initially OFF = All leds OFF Button ON for the first time = LED1 ON Button ON for the second time = LED2 ON, LED1 OFF ... Button ON for the last time = All Leds OFF

Can u guys help me with the code?

I'm brazillian, then sorry for the bad_english.exe !

Thx!

Sure we can help but we don't do it for you. Post what you have so far and post a schematic of how things are wired.

Yep, it’s not that hard. What’s the problem?

Some tips:

  • Use the internal pullups. So a button press is low and a button not pressed is a high.
  • To make buttons/switches easy, use the Bounce2 library.
  • Use the method button.fell() to increment a variable

This is an example of a "state machine". There should be one variable which keeps track of what state it's in - in your case, this is the number of LEDs that are on. Then for each state, there may be several possible transitions out of that state into another state. In your case, the button becoming pressed will transition it to the next state.

Note that when the button is pressed or when it becomes released, you probably don't want any state transitions. So you also need to store the state of the button when you last looked at it and only take an action when it was previously up but now is down.

look at the debounce example

strip away the debounce part....

void loop() {

 int reading = digitalRead(buttonPin); // loads the button value into 'reading'


 if (reading != lastButtonState) {    // if not the same, then TRUE
   count = count +1;
 }

 lastButtonState = reading;    // goes at the end of the sketch after everything else.
}

no reading, lastButtonState = reading because the last line of the sketch makes these two values AFTER the sketch has run.

press the button and now the reading is NOT the same. because lastButtonState is still showing the state of the button as it was from the last loop. at the end of this loop, it will be the same, so this is a one time event, one time per press.

if your button does have a bounce, this will count each bounce.

this concept is valuable and used in may other ways. spend a few minutes to really grasp the concept.

That's why I like the Bounce2 library. Takes care of it all.

button.update();
if(button.fell()){
  count++;
}

second look… does that add 1 when you press and ANOTHER 1 when you release ?

may need to add this to only work in one state.

if(reading==HIGH){                           // use high or low, one will turn on when the button is released
      if (reading != lastButtonState) {    // if not the same, then TRUE
             count = count +1;
          }
}

use fricking code tags!!!

what’s a flicking code tag ?

there are things I flick… not code tags though.

And there are things you read correct and there are things you don't :D F*R*icking

septillion:
And there are things you read correct and there are things you don’t :smiley: FRicking

FRACK,. I missed that.

Thx for everyone that posted!

Sorry, I fgt the code, but I will post it soon!

I'm having problems with the count, that sometimes goes without order and some other things.

Srry again for the mistake!

Thx for everyone that posted!

Sorry, I fgt the code, but I will post it soon!

I'm having problems with the count, that sometimes goes without order and some other things.

Srry again for the mistake!

So, then…

I need to make a item counter, simulating item-pass-sensor-count-wait-item thing. Some codes on net gives me a keep-counting sketch, that doesnt help, but still ++ in some ways.

one of the code references:

int num = 0;
int BotaoA = 3;
int cont = 0;

int A = 5;
int B = 6;
int C = 7;
int D = 8;
int E = 9;
int F = 10;
int G = 11;

void setup () {

  Serial.begin(9600);
  pinMode (BotaoA, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(13, OUTPUT);

}

void loop() {

  int Botao = digitalRead (BotaoA);
  Serial.println (Botao);
  //delay 70;

  if (Botao == 0 && num <= 9)
  {
    num = num + 1;
    delay (200);
  }  
  
  else if (num > 9) {

    Limpar();
    Zero();
    num = 0;
    //contador++;
  }

  switch (num) {
    case 0:
      Limpar();
      Zero();
      break;
    case 1:
      Limpar();
      Um();
      break;
    case 2:
      Limpar();
      Dois();
      break;
    case 3:
      Limpar();
      Tres();
      break;
    case 4:
      Limpar();
      Quatro();
      break;
    case 5:
      Limpar();
      Cinco();
      break;
    case 6:
      Limpar();
      Seis();
      break;
    case 7:
      Limpar();
      Sete();
      break;
    case 8:
      Limpar();
      Oito();
      break;
    case 9:
      Limpar();
      Nove();
  }
}
  
  
  void Zero ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
  }

  void Um ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
  }

  void Dois ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
  }

  void Tres ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
  }

  void Quatro ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }

  void Cinco ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }

  void Seis ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }

  void Sete ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
  }

  void Oito ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }

  void Nove ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
  }

  void Limpar ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
  }

I’m tryng to:

1 - Count Itens with a sensor feedback
2 - Display the count in a 7-segment Led display
3 - Make sure that the sensor do not count “while” HIGH (Item standing in front of the sensor)
4 - Make sure that the display, with the right count reading, do the right sequence of outputs (if == 1 or case [void “One”]
5 - Make sure that reset when count <= 10 (end of count with a 7-segment, 0 to 9)

I’m good with the int and pins, but after, I’m keeping screwing it up!

I’m not asking for a full code, but i really need to know a way to count and make it right, as i said.

Thx guys, and repeating, srry for the bad_english.exe!

First of all, lose the variables A, B, C... etc. You will have trouble with F anyway, as it is a defined macro. Use something like segA, segB and so on.

OK, that looks like a good start. I'm guessing Portugese, because some of your function names look like Spanish but not exactly.

You have done well to encapsulate the different number displays into separate functions. How do the same for the big switch() statement so that you have a simple displayNumber(int num) function, or whatever you decide to call it. That will make the main loop so much simpler to read without the long switch().

So how to actually make it count properly? The responses above have suggested debouncing, as if you are using a switch. If you're using something like a light beam detector, then debouncing is not as important.

//declarations, setup and other functions omitted

void loop() {
  static int AnteriorBotao = LOW; //we need a way to remember that we've already counted an object

  int Botao = digitalRead (BotaoA);
  if(Botao == LOW) {
    AnteriorBotao = LOW;
    //nothing else to do while the object is not detected

  } else {
    //The sensor is HIGH - we have an object in front of the sensor

    if(AnteriorBotao == LOW) {
      //this is the first time that we have seen it high
      //this means we have a new object

      num++;

      if(num > 9) num = 0; //wrap back to zero 

      displayNumber(num); //show this number on the LED display

      AnteriorBotao = HIGH; //record that we have seen this object and we don't need to detect it again  
    }
  }
}

Like this?

int num = 0;
int BotaoA = 3;
int cont = 0;

int A = 5;
int B = 6;
int C = 7;
int D = 8;
int E = 9;
int segF = 10;
int G = 11;

void setup () {

  Serial.begin(9600);
  pinMode (BotaoA, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(13, OUTPUT);

}

void loop() {
  static int AnteriorBotao = LOW; //we need a way to remember that we've already counted an object

  int Botao = digitalRead (BotaoA);
  if(Botao == LOW) {
    AnteriorBotao = LOW;
    //nothing else to do while the object is not detected

  } 
  
  else {
    //The sensor is HIGH - we have an object in front of the sensor

    if(AnteriorBotao == LOW) {
      //this is the first time that we have seen it high
      //this means we have a new object

      num++;

      if(num > 9) num = 0; //wrap back to zero

      AnteriorBotao = HIGH; //record that we have seen this object and we don't need to detect it again  
    }
  }


  switch (num) {
    case 0:
      Limpar();
      Zero();
      break;
    case 1:
      Limpar();
      Um();
      break;
    case 2:
      Limpar();
      Dois();
      break;
    case 3:
      Limpar();
      Tres();
      break;
    case 4:
      Limpar();
      Quatro();
      break;
    case 5:
      Limpar();
      Cinco();
      break;
    case 6:
      Limpar();
      Seis();
      break;
    case 7:
      Limpar();
      Sete();
      break;
    case 8:
      Limpar();
      Oito();
      break;
    case 9:
      Limpar();
      Nove();

  }  
  }
  
  void Zero ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(G, LOW);
  }

  void Um ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(G, LOW);
  }

  void Dois ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(G, HIGH);
  }

  void Tres ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(G, HIGH);
  }

  void Quatro ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(segF, HIGH);
    digitalWrite(G, HIGH);
  }

  void Cinco ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(segF, HIGH);
    digitalWrite(G, HIGH);
  }

  void Seis ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(G, HIGH);
  }

  void Sete ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(G, LOW);
  }

  void Oito ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(G, HIGH);
  }

  void Nove ()
  
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(segF, HIGH);
    digitalWrite(G, HIGH);
  }

  void Limpar ()
  
  {
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(G, LOW);
  }

This result in a unstoppable count and desorded.

I think that I'm not doing the right thing, for sure.

HAPPY EDIT!

Yea! Yes, Guys! Progress!

I've change one part of the code adding a "HIGH" situation for "BotaoA"

But I'm having a bounce issue. Going to check it out!

UNHAPPY EDIT!

So, guys. It kinda worked in the Porteus project, but when I did it on the protoboard, it keep counting without pressing the button. Any ideias?

How is it wired? What is the actual switch or sensor that you are using?

MorganS: How is it wired? What is the actual switch or sensor that you are using?

I'm using a PUSHBUTTON for now, with a NO setup.

It's wired with a pour pack of .75 netcable wires.

When I press the button, it increment, but if I do nothing, it increment too

So no pull-down resistor then? Your pinMode specifies that it’s not using the internal pullup resistor either.

You need one or the other to get a regular pushbutton to work as an input. An input pin left floating (switch open) can be high or low at random.

MorganS: So no pull-down resistor then? Your pinMode specifies that it's not using the internal pullup resistor either.

You need one or the other to get a regular pushbutton to work as an input. An input pin left floating (switch open) can be high or low at random.

I've tried to pull off the Button and work only with wires, just like a short circuit thing or do nothing, and it keeps me in a uncontrolable count. I've read in another page that a resistor could help, but still having problems though.

I'm was thinking to give up. Maybe my settings are screw up.

Any ideias?