Pages: [1]   Go Down
Author Topic: Stuck on calling function dynamically  (Read 335 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and good day. My pinball project (see my first post) is commencing and I have received my first microcontroller  :o

I have been playing with the demo programs, and am attempting to develop my own to begin to understand how the programming language works.  smiley-wink

I have a 7 segment led (the kind that shows numbers 0-9) and I have been able to program it so that it displays those numbers. I accomplished this by connecting each led to a pin on the board, and then using functions called (one, two, three, etc...) that defined which pins were HIGH and which were low to turn on the corresponding LED's to display the appropriate number. 8-)

Now I am trying to integrate a button, so that each time it is pressed, a counter increments, and the value of how many times the button has been pressed is displayed.

I think that I am very close with my code in acheving this, the problem seems to be, translating the stored variable into the function I want to call. (see line 41)



Code:

int ledPinCenterBottom = 23;            // LED connected Center Bottom Location of 7 segment LED
int ledPinLeftBottom = 24;              // LED connected Left Bottom Location of 7 segment LED
int ledPinLeftTop = 25;                      // LED connected Left Top Location of 7 segment LED  
int ledPinCenterTop = 6;                // LED connected Center Top Location of 7 segment LED  
int ledPinRightTop = 5;                 // LED connected Right Top Location of 7 segment LED
int ledPinCenter = 26;                  // LED connected Center Center Location of 7 segment LED
int ledPinRightBottom = 27;             // LED connected Right Bottom Location of 7 segment LED

int Button = 0;                // Button connected to board
int ButtonCounter = 0;            //Counter for button presses


void setup()                    // run once, when the sketch starts
{
  pinMode(ledPinCenterBottom, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinLeftBottom, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinLeftTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinCenterTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinRightTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinCenter, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinRightBottom, OUTPUT);      // sets the digital pin as output

  pinMode(Button, INPUT);      // sets the digital pin as input
}

void loop()                     // run over and over again
{
  if (Button == HIGH)
      {
      ButtonCounter++;
      ButtonCounter();
      }
      
  
}

void alloff (void)
{
    digitalWrite(ledPinCenterBottom, LOW);    // sets the LED off
    digitalWrite(ledPinLeftBottom, LOW);    // sets the LED off
    digitalWrite(ledPinLeftTop, LOW);    // sets the LED off
    digitalWrite(ledPinCenterTop, LOW);    // sets the LED off
    digitalWrite(ledPinRightTop, LOW);    // sets the LED off
    digitalWrite(ledPinCenter, LOW);    // sets the LED off
    digitalWrite(ledPinRightBottom, LOW);    // sets the LED off

    delay(10);
}

void 1(void)
{
  digitalWrite(ledPinLeftTop, HIGH);   // sets the LED on
  digitalWrite(ledPinLeftBottom, HIGH);   // sets the LED on  
  delay(400);
}

void 2(void)
{
  digitalWrite(ledPinCenterTop, HIGH);   // sets the LED on
  digitalWrite(ledPinRightTop, HIGH);   // sets the LED on
   digitalWrite(ledPinCenter, HIGH);   // sets the LED on  
    digitalWrite(ledPinLeftBottom, HIGH);   // sets the LED on  
      digitalWrite(ledPinCenterBottom, HIGH);   // sets the LED on
    delay(400);  
}  

void 3(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  delay(400);
}
  
void 4(void)
{
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  delay(400);
}

void 5(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  delay(400);
}                        
  
void 6(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  delay(400);
}    

void 7(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  delay(400);
}
  
void 8(void)
{
    digitalWrite(ledPinCenterBottom, HIGH);  
    digitalWrite(ledPinLeftBottom, HIGH);  
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinCenter, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
  delay(400);
}

void 9(void)
{
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinCenter, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
  delay(400);
}

void 0(void)
{
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
    digitalWrite(ledPinCenterBottom, HIGH);
    digitalWrite(ledPinLeftBottom, HIGH);  
  delay(400);
}


Any assistance with this would be greatly appreciated. I know it is probably very simple and possibly an irritating (to experts) newbie question,  :-[ but for the record, I have been trying to find the solution myself, before I even began coding anything I printed out the "arduino programming notebook" by Brian Evans. spent time on the playground and getting started guide, but I have this feeling I'm just barely missing the mark here. :-/
« Last Edit: May 19, 2009, 11:44:05 am by randomy » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

Functions must have names that starts with a letter or an underscore.

You could do something like this:

Code:
switch (ButtonCounter){
  case 1: zero(); break;
  case 1: one(); break;
  case 2: two(); break;
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I'll look at that. I've got something that compiles, uploads, and works ... mostly. It counts perfectly 1-9 which each press of the switch, but instead of resetting the counter and starting over (which is what I intended to code) it goes from 9 back 8, and then doesn't  do anything any longer when I press the switch.

Code:
int ledPinCenterBottom = 23;            // LED connected Center Bottom Location of 7 segment LED
int ledPinLeftBottom = 24;              // LED connected Left Bottom Location of 7 segment LED
int ledPinLeftTop = 25;                      // LED connected Left Top Location of 7 segment LED  
int ledPinCenterTop = 6;                // LED connected Center Top Location of 7 segment LED  
int ledPinRightTop = 5;                 // LED connected Right Top Location of 7 segment LED
int ledPinCenter = 26;                  // LED connected Center Center Location of 7 segment LED
int ledPinRightBottom = 27;             // LED connected Right Bottom Location of 7 segment LED

int Button = 2;                // Button connected to board
int ButtonCounter = 0;            //Counter for button presses
int val = 0;                    // variable for reading the pin status

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPinCenterBottom, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinLeftBottom, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinLeftTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinCenterTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinRightTop, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinCenter, OUTPUT);      // sets the digital pin as output
  pinMode(ledPinRightBottom, OUTPUT);      // sets the digital pin as output

  pinMode(Button, INPUT);      // sets the digital pin as input
}



void loop()                     // run over and over again
{
val = digitalRead(Button);  // read input value
  if (val == HIGH && ButtonCounter < 9)
      {
      alloff();
      ButtonCounter++;
      ButtonCounterfun( ButtonCounter );
      }
else if (ButtonCounter == 9)
        {
          zero();
          ButtonCounter == 0;
          loop();
        }
}



void ButtonCounterfun (int buttonstate)
{
      if (buttonstate == 1)
      {
      one();
      }
      if (buttonstate == 2)
      {
      two();
      }
      if (buttonstate == 3)
      {
      three();
      }
      if (buttonstate == 4)
      {
      four();
      }
      if (buttonstate == 5)
      {
      five();
      }
      if (buttonstate == 6)
      {
      six();
      }
      if (buttonstate == 7)
      {
      seven();
      }
      if (buttonstate == 8)
      {
      eight();
      }
      if (buttonstate == 9)
      {
      nine();
      }
      if (buttonstate == 0)
      {
      zero();
      }
}

void alloff (void)
{
    digitalWrite(ledPinCenterBottom, LOW);    // sets the LED off
    digitalWrite(ledPinLeftBottom, LOW);    // sets the LED off
    digitalWrite(ledPinLeftTop, LOW);    // sets the LED off
    digitalWrite(ledPinCenterTop, LOW);    // sets the LED off
    digitalWrite(ledPinRightTop, LOW);    // sets the LED off
    digitalWrite(ledPinCenter, LOW);    // sets the LED off
    digitalWrite(ledPinRightBottom, LOW);    // sets the LED off

    delay(10);
}

void one(void)
{
  digitalWrite(ledPinLeftTop, HIGH);   // sets the LED on
  digitalWrite(ledPinLeftBottom, HIGH);   // sets the LED on  
  delay(400);
}

void two(void)
{
  digitalWrite(ledPinCenterTop, HIGH);   // sets the LED on
  digitalWrite(ledPinRightTop, HIGH);   // sets the LED on
   digitalWrite(ledPinCenter, HIGH);   // sets the LED on  
    digitalWrite(ledPinLeftBottom, HIGH);   // sets the LED on  
      digitalWrite(ledPinCenterBottom, HIGH);   // sets the LED on
    delay(400);  
}  

void three(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  delay(400);
}
  
void four(void)
{
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  delay(400);
}

void five(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  delay(400);
}
  
void six(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinLeftTop, HIGH);
  digitalWrite(ledPinCenter, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  digitalWrite(ledPinCenterBottom, HIGH);
  digitalWrite(ledPinLeftBottom, HIGH);
  delay(400);
}    

void seven(void)
{
  digitalWrite(ledPinCenterTop, HIGH);
  digitalWrite(ledPinRightTop, HIGH);
  digitalWrite(ledPinRightBottom, HIGH);
  delay(400);
}
  
void eight(void)
{
    digitalWrite(ledPinCenterBottom, HIGH);  
    digitalWrite(ledPinLeftBottom, HIGH);  
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinCenter, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
  delay(400);
}

void nine(void)
{
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinCenter, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
  delay(400);
}

void zero(void)
{
    digitalWrite(ledPinLeftTop, HIGH);    
    digitalWrite(ledPinCenterTop, HIGH);  
    digitalWrite(ledPinRightTop, HIGH);    
    digitalWrite(ledPinRightBottom, HIGH);    
    digitalWrite(ledPinCenterBottom, HIGH);
    digitalWrite(ledPinLeftBottom, HIGH);  
  delay(400);
}
« Last Edit: May 19, 2009, 08:40:21 pm by randomy » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got a solution. Set value with = not ==. (forehead slap)
turn off leds before trying to make  zero, or it look slike eight (when following nine).

Code:
void loop()                     // run over and over again
{
val = digitalRead(Button);  // read input value
  if (val == HIGH && ButtonCounter < 9)
      {
      alloff();
      ButtonCounter++;
      ButtonCounterfun( ButtonCounter );
      }
 
 if (val == HIGH && ButtonCounter >= 9)
        {
          alloff();
          zero();
          ButtonCounter = 0;
        }
}
Logged

Pages: [1]   Go Up
Jump to: