Pages: [1] 2   Go Down
Author Topic: having a little trouble please help  (Read 1605 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  }  
}
« Last Edit: July 30, 2011, 03:06:41 pm by nk3495 » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rob Tillaart

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34207
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to repeat the code in this section:-
Code:
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;}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
* add  int step = 1;   (you know how to do this)

* change loop.
Code:

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();
}
« Last Edit: July 30, 2011, 03:27:46 pm by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


wrt the array's

Code:
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:
for (byte i=0; i< 7; i++) pinMode(segment[i]. OUTPUT);

and it can change

Code:
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:
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



Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In a modded version you can change

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

into

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

This makes the counting faster and slower, upper and downer smiley-wink

Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now i'm getting several errors what did i do wrong?

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

new code

Code:
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;
  }  
}
« Last Edit: July 30, 2011, 04:18:43 pm by nk3495 » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34207
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  } 
}

« Last Edit: July 30, 2011, 04:33:18 pm by nk3495 » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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);



Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#brackets in loop is incorrect  check this one...
Code:
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();
}
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 30, 2011, 04:52:09 pm by nk3495 » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34207
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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();
}
Logged

Rob Tillaart

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

Pages: [1] 2   Go Up
Jump to: