repeat function for times in variable

Evening all,

Thank you again for taking a few moments to look over my questions, it is truly appreciated.

The desired outcome is as follows.

  1. select button 2 x times until desired count is reached in the buttonPushcounter variable
  2. select button 3 and perform a random number generation equal to the number stored in the buttonPushcounter variable.
  3. Serial.println each of the individual random numbers.

Code is below. Am I way off track here, or am I heading the right direction?

const int  buttonPin2 = 2;    // the pin that the pushbutton is attached to
const int  buttonPin3 = 3;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin  6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int button2State = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int button3State = 0;

void setup() {
	// initialize the button pin as a input:
	pinMode(buttonPin2, INPUT);
	pinMode(buttonPin3, INPUT);
	
	// initialize the LED as an output:
	pinMode(ledPin, OUTPUT);
	// initialize serial communication:
	Serial.begin(9600);
}


void loop() {
	// read the pushbutton input pin:
	button2State = digitalRead(buttonPin2);
	
	// compare the buttonState to its previous state
	if (button2State != lastButtonState) {
		// if the state has changed, increment the counter
		if (button2State == HIGH) {
			// if the current state is HIGH then the button
			// went from off to on:
			buttonPushCounter++;
			Serial.println("on");
			Serial.print("number of button pushes:  ");
			Serial.println(buttonPushCounter);
			
			button3State = digitalRead(buttonPin3);
			Serial.println(button3State);
			if (button3State == HIGH) {
				int buttonPushCounter1;
				buttonPushCounter1 = buttonPushCounter;
				Serial.println(buttonPushCounter1);
				for(int i = 0; i <= buttonPushCounter1; i++)
					int VAR_20 = random(1,21);
				Serial.println(VAR_20);
			}
			
			
		}
		
		
	}
	// save the current state as the last state, 
	//for next time through the loop
	lastButtonState = button2State;
	
	
	
}

Ninefingers:
Code is below. Am I way off track here, or am I heading the right direction?

You haven't told us what the code actually does?

...R

The way that it is written at the moment when a new button press is detected on button 2 the program is not going to wait for button 3 to be pressed.

Also, this portion of code, which I have Auto Formatted

       for (int i = 0; i <= buttonPushCounter1; i++)
          int VAR_20 = random(1, 21);
        Serial.println(VAR_20);

will not print each value of VAR_20 because only the random number generation occurs for each iteration of the for loop, not the printing.

Try

        for (int i = 0; i <= buttonPushCounter1; i++)
           {
              int VAR_20 = random(1, 21);
              Serial.println(VAR_20);
           }

Ninefingers:
Am I way off track here, or am I heading the right direction?

I'm not quite sure if I fully understood what you want:

  • You have two push buttons, connected to Pin2 and Pin3 of your Arduino board

  • first task is: count how many times the Pin2 button is pressed

  • second ttask: If button on Pin3 is pressed, then generate a random number, put it in a variable and send it to Serial

Is this a correct description of what you want?

And how did you connect the buttons?
Did you use pull-down resistors in your circuit?
Or did you use no pull-resistors and wired like that:

Wiring description:

  • first push button connected to Pin2- and GND
  • second push button connected to Pin3- and GND

Or maybe like that:

  • first push button connected to Pin2- and 5V
  • first pull-down resistor connected to Pin2 and GND
  • second push button connected to Pin3- and 5V
  • second pull-down resistor connected to Pin3 and GND

Or is the wiring different?