Go Down

Topic: having a little trouble please help (Read 1 time) previous topic - next topic

nk3495

Jul 30, 2011, 09:04 pm Last Edit: Jul 30, 2011, 10:06 pm by nk3495 Reason: 1
I'm trying to make an up down counter it counts up each time you press one button and i want it to count down when you press the other. i have it counting up but don't know what to do to get it to count down ? hope someone can help

here is my code

Code: [Select]
int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;
int number=0;
int pin9=9;
int pin10=10;
int pin12=12;
int tens=0;
int unit=0;
int digit=0;
int tens1=0;
int presentvalue=0;
int previousvalue=0;


// Pins for A B C D E F G, in sequence
const int segs[7] = { 2, 3, 4, 5, 6, 7, 8, };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

void setup()
{
//Serial.begin(9600);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(pin9,OUTPUT);
pinMode(pin10,OUTPUT);
pinMode(pin12,INPUT);
zero();
}

void loop()
{
presentvalue=digitalRead(pin12);
if (presentvalue!=previousvalue)
{
if (presentvalue==1)
{number=number+1;}
previousvalue=presentvalue;
}
display1();
}

void display1()
{
tens1=number%100;
tens=tens1/10;
unit=tens1%10;
for (int i=0;i<=10;i++)
{
 
 digit=tens;
 digitselection();  
 digitalWrite(pin9,HIGH);
 digitalWrite(pin10,LOW);
 delay(1);
 
 digit=unit;
 digitselection();  
 digitalWrite(pin9,LOW);
 digitalWrite(pin10,HIGH);
 delay(1);
}
if (number>99) {number=0;}
}



void zero()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

void one()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void two()
{
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}

void three()
{
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void four()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void five()
{
digitalWrite(e,HIGH);
digitalWrite(b,HIGH);
digitalWrite(a,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void six()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
}

void seven()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void eight()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void nine()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void digitselection()
{
 switch (digit)
 {
   case 0:
   zero();
   break;
   case 1:
   one();
   break;
   case 2:
   two();
   break;
   case 3:
   three();
   break;
   case 4:
   four();
   break;
   case 5:
   five();
   break;
   case 6:
   six();
   break;
   case 7:
   seven();
   break;
   case 8:
   eight();
   break;
   case 9:
   nine();
   break;
 }  
}

robtillaart

please modify your post, select the code and press the # button .. makes the code look better , thank you.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Grumpy_Mike

You need to repeat the code in this section:-
Code: [Select]
presentvalue=digitalRead(pin12);
if (presentvalue!=previousvalue)
{
if (presentvalue==1)
{number=number+1;}
previousvalue=presentvalue;
}


Using different variables to handle the down button, changing of course
{number=number+1;}
to
{number=number-1;}

robtillaart

#3
Jul 30, 2011, 10:17 pm Last Edit: Jul 30, 2011, 10:27 pm by robtillaart Reason: 1
First have a look at arrays, it can make your code much shorter !!

* add  pin11 in input mode  (you know how to do this, please use meaningfull names like up and DOWN :)
* add  int step = 1;   (you know how to do this)

* change loop.
Code: [Select]


void loop()
{
 // READ INPUT
 if (digitalRead(pinDown) == LOW) step = -1;
 if (digitalRead(pinUp) == LOW) step = +1;
 
 // ADJUST NUMBER
 number += step;
 number = constrain(number, 0, 100);  // optional

 // DISPLAY IT
 display1();
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart


wrt the array's

Code: [Select]
int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;

could be

int segment[7] = {2,3,4,5,6,7,8 };


and that allows you todo

Code: [Select]

for (byte i=0; i< 7; i++) pinMode(segment[i]. OUTPUT);


and it can change

Code: [Select]

void zero()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}
...

void nine()
{
..
}


to a single function:
Code: [Select]

void digit(int nr)
{
  for (byte i=0; i<7; i++)
  {
    digitalWrite(segment[i], bitRead(numbers[nr], i);
  }
}

call to zero() should be replaced by a call to digit(0);  etc

Success,
Rob



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

In a modded version you can change

Code: [Select]

// READ INPUT
  if (digitalRead(pinDown) == LOW) step = -1;
  if (digitalRead(pinUp) == LOW) step = +1;


into

Code: [Select]

// READ INPUT
  if (digitalRead(pinDown) == LOW) step--;
  if (digitalRead(pinUp) == LOW) step++;


This makes the counting faster and slower, upper and downer ;)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

nk3495

#6
Jul 30, 2011, 11:13 pm Last Edit: Jul 30, 2011, 11:18 pm by nk3495 Reason: 1
now i'm getting several errors what did i do wrong?

error: 'step' was not declared in this scope?

new code

Code: [Select]
int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;
int number=0;
int pin9=9;
int pin10=10;
int pinUp=11;
int pinDown=12;
int tens=0;
int unit=0;
int digit=0;
int tens1=0;
int increment=1;


// Pins for A B C D E F G, in sequence
const int segs[7] = { 2, 3, 4, 5, 6, 7, 8, };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

void setup()
{
//Serial.begin(9600);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(pin9,OUTPUT);
pinMode(pin10,OUTPUT);
pinMode(pinUp,INPUT);
pinMode(pinDown,INPUT);
zero();
}

void loop()
{
 //READ INPUT
 if (digitalRead(pinDown)== LOW) step =-1;
 if (digitalRead(pinUp) == LOW) step =1
 
 //ADJUST NUMBER
 number += step;
 number = consteian(number, 0 , 100);  //

void display1()
{
tens1=number%100;
tens=tens1/10;
unit=tens1%10;
for (int i=0;i<=10;i++)
{
 
 digit=tens;
 digitselection();  
 digitalWrite(pin9,HIGH);
 digitalWrite(pin10,LOW);
 delay(1);
 
 digit=unit;
 digitselection();  
 digitalWrite(pin9,LOW);
 digitalWrite(pin10,HIGH);
 delay(1);
}
if (number>99) {number=0;}
}



void zero()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

void one()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void two()
{
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}

void three()
{
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void four()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void five()
{
digitalWrite(e,HIGH);
digitalWrite(b,HIGH);
digitalWrite(a,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void six()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
}

void seven()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void eight()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void nine()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void digitselection()
{
 switch (digit)
 {
   case 0:
   zero();
   break;
   case 1:
   one();
   break;
   case 2:
   two();
   break;
   case 3:
   three();
   break;
   case 4:
   four();
   break;
   case 5:
   five();
   break;
   case 6:
   six();
   break;
   case 7:
   seven();
   break;
   case 8:
   eight();
   break;
   case 9:
   nine();
   break;
 }  
}

Grumpy_Mike

Read the compile errors and correct them:-
1) step appears to be a reserved word use mystep instead and define it as an int at the start.
2) Check for missing ; for the next error in the line above
3) what is this:- number = consteian(number, 0 , 100);  there is no function called consteian
4) You missed a } after this call

Then it compiles.

nk3495

#8
Jul 30, 2011, 11:28 pm Last Edit: Jul 30, 2011, 11:33 pm by nk3495 Reason: 1
that was constrain not constreian mybad
after your fixes im getting 3 errors at void display1()  {

cpp; in function 'void loop()':
63;error; a function-defintion is not allowed here before '{' token
232; error; expected '}' at end of input
?? thanks

heres the code if you need it


Code: [Select]
int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;
int number=0;
int pin9=9;
int pin10=10;
int pinUp=11;
int pinDown=12;
int tens=0;
int unit=0;
int digit=0;
int tens1=0;
int increment=1;
int mystep=0;


// Pins for A B C D E F G, in sequence
const int segs[7] = { 2, 3, 4, 5, 6, 7, 8, };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

void setup()
{
//Serial.begin(9600);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(pin9,OUTPUT);
pinMode(pin10,OUTPUT);
pinMode(pinUp,INPUT);
pinMode(pinDown,INPUT);
zero();
}

void loop()
{
  //READ INPUT
  if (digitalRead(pinDown) == LOW) mystep = -1;
  if (digitalRead(pinUp) == LOW) mystep = +1;
 
  //ADJUST NUMBER
  number += mystep;
  number = constrain(number, 0 , 100);  //
 
{
   
 
  // DISPLAY IT
  display1();
}
 

void display1()
{
tens1=number%100;
tens=tens1/10;
unit=tens1%10;
for (int i=0;i<=10;i++)
{
 
  digit=tens;
  digitselection(); 
  digitalWrite(pin9,HIGH);
  digitalWrite(pin10,LOW);
  delay(1);
 
  digit=unit;
  digitselection(); 
  digitalWrite(pin9,LOW);
  digitalWrite(pin10,HIGH);
  delay(1);
}
if (number>99) {number=0;}
}



void zero()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

void one()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void two()
{
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}

void three()
{
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void four()
{
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void five()
{
digitalWrite(e,HIGH);
digitalWrite(b,HIGH);
digitalWrite(a,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
}

void six()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
}

void seven()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
}

void eight()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void nine()
{
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

void digitselection()
{
  switch (digit)
  {
    case 0:
    zero();
    break;
    case 1:
    one();
    break;
    case 2:
    two();
    break;
    case 3:
    three();
    break;
    case 4:
    four();
    break;
    case 5:
    five();
    break;
    case 6:
    six();
    break;
    case 7:
    seven();
    break;
    case 8:
    eight();
    break;
    case 9:
    nine();
    break;
  } 
}



robtillaart


add 1) step is auto-highlighted but you can use it if you declare it. But increment might be a better name

add 3) number = consteian(number, 0 , 100);  ==>  number = constrain(number, 0 , 100);



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

#brackets in loop is incorrect  check this one...
Code: [Select]
void loop()
{
  //READ INPUT
  if (digitalRead(pinDown) == LOW) mystep = -1;
  if (digitalRead(pinUp) == LOW) mystep = +1;
 
  //ADJUST NUMBER
  number += mystep;
  number = constrain(number, 0 , 100);  //
 
  // DISPLAY IT
  display1();
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

nk3495

#11
Jul 30, 2011, 11:42 pm Last Edit: Jul 30, 2011, 11:52 pm by nk3495 Reason: 1
it doesnt count up or down when i press the button it jst runs?

i wanted it so everytime i press the up button it counts up & the down button it counts down not continously

Grumpy_Mike

How are the buttons wired up? Do you need to enable the internal pull ups?

That code will count very fast until it hits the limit, in fact it will count even when the buttons are not pressed, all the presses are doing are changing the direction of the count.

Why did you not do what I said earlier?

nk3495

if you are talking about shorting the code? its because i want to get this straightened out first. if thats what your talking about

the buttons are wired like this the input wire for pins 11 &12 both go to there own 10k resistor & 1 side of the switch then the other side of the resistor goes to gnd
the code i put at the beginning of this post counted up everytime i hit the button & all i wanted to know was how to add another button to count down everytime i hit it not for it to continuously count

robtillaart

Code: [Select]

void loop()
{
  mystep = 0;  // don't count if no butt on pressed
  //READ INPUT
  if (digitalRead(pinDown) == LOW) mystep = -1;
  if (digitalRead(pinUp) == LOW) mystep = +1;
 
  //ADJUST NUMBER
  number += mystep;
  number = constrain(number, 0 , 100);  //
 
  // DISPLAY IT
  display1();
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up