programming problem - void loop()

Hello there,

In programming arduino, can we stop the program to wait for another operation to run. I'm not referring to 'while' loop. I mean in c++ we can code like this:

int i;
cout<<"enter an integer";
cin>>i;// computer waits for an integer

In this code the program waits until we enter a number...no matter how long it takes. But in arduino
as we code inside the 'void loop()', the program runs repeatedly. So we cannot delay the process until another function works. Is there a way we can code as i tried to explain above(c++) ?

You can use a while loop.

Pseudocode:

while(user input not complete){

       check for user input

}

You just need some sort of flag or condition to tell it that the input part is done.

yeah, i already tried it. But i want few buttons as inputs and when i pressed one of them, program should store a value for it. When three buttons pressed, i need them to do some math calculations( like add them together). But as this "void loop()" runs the code again an again, all the values get reset. :frowning:
I'm not good at programming, please give me an idea...

If you have a problem with your code, wouldn't it make sense to post that code?

Use code tags, please.

ok here we go.. but I don't think it make any sense at all.I appreciate your help :slight_smile:

// a code for a combination lock using 10 buttons.
// Here the password is 123. So when the program runs, after entering three numbers, press 'enter button'(pin 12).
//If the code is right Blue LED turns on, else the Red LED turns on. 


int key[]={2,3,4,5,6,7,8,9,10,11};// the array key[] for digital pins
int red = 13; // a Red LED for pin 13
int blue = A0;// a blue LED for analog pin A0

/*here all numbers are represented by digital pins.

pin 2 =1 , pin 3 =2 ,pin 4 =3 ,
pin 5 =4 ,pin 6 =5, pin 7 =6 
,pin 8 =7, pin 9 =10  ,pin 11 =0 
        
 */
 
int k[2];// to three store numbers
int enter =12;// a button to start calculation
int j=0;// a variable to count the numbers entered

void setup() {
  
pinMode(2,INPUT);pinMode(3,INPUT);pinMode(4,INPUT);
pinMode(5,INPUT);pinMode(9,INPUT);pinMode(10,INPUT);
pinMode(8,INPUT);pinMode(11,INPUT);pinMode(12,INPUT);
pinMode(6,INPUT);pinMode(7,INPUT);

pinMode(13,OUTPUT);

Serial.begin(9600);

}

void loop() {



for(int i=0;i<=9;i++){

  /*here the pins of buttons are set in a array, so as the for loop runs, when a button is pressed the variable
   value of 'i' equals to the number of the button pressed. 
  */

 if( digitalRead(key[i])==HIGH){

  k[j]=i;
  j=j+1;// to change place of array 

  }
  

 
 }




if(digitalRead(enter)==HIGH){



  int x = (k[0]*100)+(k[1]*10)+k[2];// calculates the digits entered and compare with 123


  if(x==123){
    analogWrite(blue,150);//turns on Blue LED
  }

  else{
    digitalWrite(red,HIGH);//turns on Red LED
   }
  
 }

}

Everytime I run the code, I get Red flash.

If it doesn't make sense to you, rewrite it so that it does make sense.

The following is a real problem, and you have to rethink it completely.

  k[j]=i;
  j=j+1;// to change place of array

As soon as j is greater than 2, you start wiping out variable (RAM) memory.

The code in Planning and Implementing a Program includes taking input from the user.

...R

ok, I'll try again. Thank u guys :slight_smile:

int k[2];// to three store numbers

How do you expect to store three numbers in two places?

Why the buffer at all for that matter.