Counter

Hi,
I' ve just started using arduino, and I've already a problem..
Well.. I wanted to make simple counter, which will count single pulses on a 4 digit 7 segment display. I made something like that, for now:

void setup() {

pinMode(1, OUTPUT); // D1
pinMode(2, OUTPUT); // D2
pinMode(3, OUTPUT); // D3
pinMode(4, OUTPUT); // D4
pinMode(5, OUTPUT); // a
pinMode(6, OUTPUT); // b
pinMode(7, OUTPUT); // c
pinMode(8, OUTPUT); // d
pinMode(9, OUTPUT); // e
pinMode(10, OUTPUT); // f
pinMode(11, OUTPUT); // g
pinMode(12, INPUT); // conter entrance


digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);

// zero, as a starter

}

void one() {  // 1

digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}

void two() {  // 2

digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

digitalRead(12); // check Button on pin 12
if(digitalRead(12) == HIGH){  // If it s pressed, show 1 number
one();
  }
digitalRead(12); // check Button on pin 12
if(digitalRead(12) == HIGH) { // If it s pressed, show 2 number
two()
  }
}

But of course it is not working, as I would like it to. Instead if I push button, 2 digits are shown, instead of 1.
I came up also with this:

digitalRead(12)
if (digitalRead(12) == HIGH) && (void one = HIGH);{
   two() // If the Button is pushed and the programm is running, then show the 2 number
   } 
}

But it also is not working. It gave me an error: error: expected identifier before '(' token
I tried other things, but I didn't get better results.
I would be so happy, If someone helped me :slight_smile:

Why did you take the ; out?

Steve

(void one = HIGH)Please explain the intention.

Pin names would be useful

if (digitalRead(12) == HIGH) && (void one = HIGH);{
                                                 ^

the semicolon completes the if statement, making the following code in the braces unconditional.

remove the semicolon

gcjr:
remove the semicolon

...and remove the assignment

TheMemberFormerlyKnownAsAWOL:

(void one = HIGH)

Please explain the intention.

Pin names would be useful

TheMemberFormerlyKnownAsAWOL:

(void one = HIGH)

Please explain the intention.

Pin names would be useful

I meant by that that, the programm, which is showing 1, is running at the moment

gcjr:

if (digitalRead(12) == HIGH) && (void one = HIGH);{

^




the semicolon completes the if statement, making the following code in the braces unconditional.

remove the semicolon

gcjr:

if (digitalRead(12) == HIGH) && (void one = HIGH);{

^




the semicolon completes the if statement, making the following code in the braces unconditional.

remove the semicolon

I removed the semicoln, but it is not working either

try to understand all the comments

zyga05:
I meant by that that, the programm, which is showing 1, is running at the moment

If the function that is showing the 1 were running, you wouldn't be in this "if".

@OP

A simple tutorial that may help you.

1. Check that your connection agrees with the following circuit of Fig-1.
7seg4.png
Figure-1:

2. Your requirement could be described by the following Flow Chat of Fig-2.
7seg4Flow.png
Figure-2: Flow Chart describing program behavior

3. Upload the following sketch (your one slightly modified)

byte counter = 1;

void setup()
{
  pinMode(1, OUTPUT); // D1  L1:
  pinMode(2, OUTPUT); // D2
  pinMode(3, OUTPUT); // D3
  pinMode(4, OUTPUT); // D4
  //-----------------------
  pinMode(5, OUTPUT); // a
  pinMode(6, OUTPUT); // b
  pinMode(7, OUTPUT); // c
  pinMode(8, OUTPUT); // d
  pinMode(9, OUTPUT); // e
  pinMode(10, OUTPUT); // f
  pinMode(11, OUTPUT); // g
  //-------------------------
  pinMode(12, INPUT); // conter entrance; 
  //----------------------------
  digitalWrite(1, HIGH);  //display is blank
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  //---------------------
}

void one()
{ // 1
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);  //1 will appear at D4 position
  //---------------------
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

void two()
{ // 2

  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  //----------------------
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
}

void loop()
{
  bool m = digitalRead(12); // check Button on pin 12 L2:
  if (m == HIGH)
  { // If it s pressed, show 1 number
    if (counter == 1)  //k1 is closed for the 1st time
    {
      one();
      counter = 2;
    }
    else
    {
      two();
      counter = 1;
    }
  }
}

4. Check that the display unit is blank.
5. Gently press K1.
6. Check that 1 has appeared at D4 position of the display unit.
7. Gently press K1.
8. Check that 2 has appeared at D4 position of the display unit.

7seg4.png

7seg4Flow.png

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.