Go Down

Topic: Project 11 Upgrading with a hack? (Read 6341 times) previous topic - next topic

independenter

Apr 01, 2020, 02:56 am Last Edit: Apr 02, 2020, 01:07 am by independenter
Hi I want to improve on project 11 by making it return random answers to each line, not just the bottom line, but I'm not getting the result I want.
So far I've only be able to get both lines to return random answers that are seemingly permanently paired. Can anyone help me find an answer to this?

For example, the answers are always return like this:

Case 1
Case 1

or

Case 3
Case 3

What I want to achieve is:

Case 1
Case 5

or

Case 7
Case 3





The original code:

void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      reply = random( 8 );
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("the ball says:");
      lcd.setCursor(0, 1);
      switch (reply) {
        case 0:
          lcd.print("Yes");
          break;
        case 1:
          lcd.print("Most likely");
          break;

          ...continues...

        case 7:
          lcd.print("No");
          break;
          } 
        }
     }
   prevSwitchState = switchState;
}




And the changes I made:
void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
  if (switchState == LOW) {
    reply = random( 8 );
    lcd.clear();
    lcd.setCursor(0, 0);
    switch(reply){
      case 0:
        lcd.print("Yes");
        break;
      case 1:
        lcd.print("Most likely");
        break;
     
          ...continues...

      case 7:
        lcd.print("No");
        break;
    } 
    lcd.setCursor(0, 1);
    switch(reply){
      case 0:
        lcd.print("Yes");
        break;
      case 1:
        lcd.print("Most likely");
        break;
     
          ...continues...

      case 7:
        lcd.print("No");
        break;
        } 
      }
    }
  prevSwitchState = switchState;
}

pingi_j

#1
Apr 03, 2020, 03:32 pm Last Edit: Apr 03, 2020, 03:33 pm by pingi_j
Your variable reply = random( 8 ); will be assigned to example 1 and then your LCD will output the same answer for both rows on the LCD. To have two different random answers, try to initiate another random variable lets say reply2 = random( 8 ); and then assign that for row 2's switch cases.


Go Up