Run operation Once

Hello all, first post, and my C experience is about the level of ‘blinky’ so it has been a learning curve for me. Just got the arduino a few weeks ago and have been trying to make a clock that makes you do math problems to turn the alarm off. An idea that has been on the web for a while, search ‘math alarm clock’. For me, sort of a lets learn how this how C thing works project…

In a few sections of my code I have wanted to only run the operation once. For instance my math problem is created by the random(). So I would run random() once and then allow the user to input an answer. Presently I use a For(i=0,i<1,i++). Is this the best way to do this?

How about posting the code so we can see what builds up to that point and how to best achieve the outcome you want.

Here is the code of my ‘math’ routine:

int domath() {  //display math problem
  int i;
  buttons();
  for(oneProb; oneProb<1 ; oneProb++) {  //set problem the first time only
    x = int(random(20));
    y = int(random(20));
    z = x + y;
    ans = int(random(5,15));
    if (ans == z) {
      ans = z-4;
    }
    ansOrg = ans;
  }
  
  if (minsButIn == LOW) {
    ans++;
  }  
  if (hrsButIn == LOW) {
    ans--;
  }
  
  //Display Math problem
  itoa(ans, buffans, 10);
  itoa(x, buffx, 10);
  itoa(y, buffy, 10);
  
  lcd.cursorTo(2,3);
  lcd.printIn(buffx);
  lcd.print('+');
  lcd.printIn(buffy);
  lcd.printIn("=?");
  lcd.printIn(buffans);
  
  for(i=0; i<4 ; i++) { 
  buffans[i] = '0';
  buffx[i] = '0';
  buffy[i] = '0';
  }

  return ans;
  return z;
}

oneProb is declared global so I can reset it to 0 for a new problem when I call this routine again.

Change:

for(oneProb; oneProb<1 ; oneProb++) {  //set problem the first time only
    x = int(random(20));
    y = int(random(20));
    z = x + y;
    ans = int(random(5,15));
    if (ans == z) {
      ans = z-4;
    }
    ansOrg = ans;
  }

to:

if(oneProb == 1)
 {  //set problem the first time only
    x = int(random(20));
    y = int(random(20));
    z = x + y;
    ans = int(random(5,15));
    if (ans == z)
   {
      ans = z-4;
    }
    ansOrg = ans;
 }