Go Down

Topic: Switch case problem.... (Read 937 times) previous topic - next topic

ze3one

Hello everyone, I'm new to programming world and I need some help for a small school project. I've done a shield that fit on my atmega1280 that reproduce two dices. here's a picture:

Each led has a different output pin. For the left dice :
34,32,30
    36
38,40,42

I've done a code that should make the left dice show a different number every time that I put my finger on the photoresistor, but it always show 5 and never exit case "5" to go back to default.

So here's my code (please don't laught)

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int res =0;

long dicea;
long diceb;


void setup() {               

  Serial.begin(9600);
 
  lcd.begin(16, 2);
 
  pinMode(30, OUTPUT);
  pinMode(32, OUTPUT); 
  pinMode(34, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(43, OUTPUT);

}


void loop() {
 
 
      res = analogRead(15);
      lcd.setCursor(0, 0);
      lcd.print("photoresistance : ");
      lcd.setCursor(0, 1);
      lcd.print(res);
     
      if (res < 350)
dicea = random(1,6);
diceb = random(1,6);


switch (dicea) {
  case 1: 
    digitalWrite(36,HIGH);
    delay(50);
    break;
  case 2:
     digitalWrite(30,HIGH);
     digitalWrite(38,HIGH);
     delay(50);
    break;
  case 3:   
    digitalWrite(30,HIGH);
    digitalWrite(36,HIGH);
    digitalWrite(38,HIGH);
    delay(50);
    break;
  case 4: 
    digitalWrite(30,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(42,HIGH);
    delay(50);
    break;
  case 5:   
    digitalWrite(30,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(36,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(42,HIGH);
    delay(50);
    break;
  case 6:   
    digitalWrite(30,HIGH);
    digitalWrite(32,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(40,HIGH);
    digitalWrite(42,HIGH);
    delay(50);
    break;
    default:
   
    digitalWrite(30,LOW);
    digitalWrite(32,LOW);
    digitalWrite(34,LOW);
    digitalWrite(36,LOW);
    digitalWrite(38,LOW);
    digitalWrite(40,LOW);
    digitalWrite(42,LOW);
    int dicea = 0;
  }

}


Thank you

stimmer

Look at the documentation for random - I think the upper limit is exclusive, so random(1,6) actually returns a random number between 1 and 5. You might want random(1,7).

I'd suspect what is actually happening is that the dice is very quickly displaying all random numbers from 1 to 5, it just looks like it's only showing a 5 because 6 is the only number that would light the other 2 LEDs.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

ze3one

You were right about the upper exclusive, I've done that to test it
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int res =0;

long dicea;



void setup() {               

  Serial.begin(9600);
 
  lcd.begin(16, 2);
 

}


void loop() {
 
 
      res = analogRead(15);
     
     
     
      if (res < 350) {
        dicea = random(1,7);
        lcd.setCursor(0, 0);
        lcd.print(dicea);
        delay(5000);
                     }
lcd.clear();

}





And I saw another problem, when I reset my program, it show the same numbers, how can I make it look like a true number generator...

thanks

WizenedEE

Try the randomSeed function on an unconnected analogRead pinor store a random value in EEPROM that's used to seed. If all else fails, get a geiger counter.

ze3one

Thank WizenedEE for the info

Ok I saw a mistake that I made, after my first analogRead(15), I didn't reset this value, but still I can't turn off the led, it seem that it doesn't do the default: section ( I had it in the default section )

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int res =0;

long dicea;
long diceb;


void setup() {               

  Serial.begin(9600);
 
  lcd.begin(16, 2);
 
  pinMode(30, OUTPUT);
  pinMode(32, OUTPUT); 
  pinMode(34, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(43, OUTPUT);

}


void loop() {
 
 
      res = analogRead(15);
      lcd.setCursor(0, 0);
      lcd.print("photoresistance : ");
      lcd.setCursor(0, 1);
      lcd.print(res);
     
      if (res < 350)
dicea = random(1,7);
diceb = random(1,6);


switch (dicea) {
  case 1: 
    digitalWrite(36,HIGH);
    delay(5000);
    break;
  case 2:
     digitalWrite(30,HIGH);
     digitalWrite(38,HIGH);
     delay(5000);
    break;
  case 3:   
    digitalWrite(30,HIGH);
    digitalWrite(36,HIGH);
    digitalWrite(38,HIGH);
    delay(5000);
    break;
  case 4: 
    digitalWrite(30,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(42,HIGH);
    delay(5000);
    break;
  case 5:   
    digitalWrite(30,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(36,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(42,HIGH);
    delay(5000);
    break;
  case 6:   
    digitalWrite(30,HIGH);
    digitalWrite(32,HIGH);
    digitalWrite(34,HIGH);
    digitalWrite(38,HIGH);
    digitalWrite(40,HIGH);
    digitalWrite(42,HIGH);
    delay(5000);
    break;
    default:
   
    digitalWrite(30,LOW);
    digitalWrite(32,LOW);
    digitalWrite(34,LOW);
    digitalWrite(36,LOW);
    digitalWrite(38,LOW);
    digitalWrite(40,LOW);
    digitalWrite(42,LOW);
   res = analogRead(15);
   
     
   
   
   
  }

}

 

PeterH

A few things strike me as odd about the code.

In the case for each dice value, you set some outputs high but you don't set the others low. Won't it simply end up with them all 'high'?

There is code in the default case to set them low again, but I don't understand when the 'default' case is expected to happen. You have cases for all values 1 .. 6. Perhaps you meant this code to execute after each case. In that case you should move it outside the switch statement.

Code: [Select]

if (res < 350)
dicea = random(1,7);
diceb = random(1,6);


I see you're using 'res < 350' as the condition to roll dicea again. Have you confirmed that you do get res < 350 when you cover the photocell?
I only provide help via the forum - please do not contact me for private consultancy.

ze3one

Yes it goes under 350 when I cover all the photocell, i'll try to put them low in each case instead of the default, ( I saw that i didn't set diceb (1,7) in your quote)
thanks guy for your help I'll post an update later.

PeterH

What do you want to happen when you take your hand off the photocell? Do you want the dice value to stay up for a few seconds and then disappear, or stay up until you 'roll' again? This will tell you whether you need to set all outputs high/low when you display the value, or only set the high ones and then clear them all after your delay.

I assume you don't want to be able to roll again during the delay.
I only provide help via the forum - please do not contact me for private consultancy.

ze3one

With this code, when I press once fast It shuffle one number and when I hold it we can see it shuffling until I removed my finger (impossible to remove it when you see 6 and get 6)

Thank you very much guy for your help, I'll make this code look better another day(using variable instead of pin number)

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int res =0;
long dicea;

void setup() {               

  Serial.begin(9600);
 
  lcd.begin(16, 2);
 
  pinMode(30, OUTPUT);
  pinMode(32, OUTPUT); 
  pinMode(34, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(43, OUTPUT);

}


void loop() {
 
  res = analogRead(15);
  lcd.setCursor(0, 0);
  lcd.print("photoresistance : ");
  lcd.setCursor(0, 1);
  lcd.print(res);
     
  if (res < 350) dicea = random(1,7);

  switch (dicea) {

    case 1: 

      digitalWrite(30,LOW);
      digitalWrite(32,LOW);
      digitalWrite(34,LOW); 
      digitalWrite(36,HIGH);
      digitalWrite(38,LOW);
      digitalWrite(40,LOW);
      digitalWrite(42,LOW);
      delay(50);
       
        break;
 
    case 2:
 
      digitalWrite(30,HIGH);
      digitalWrite(32,LOW);
      digitalWrite(34,LOW);
      digitalWrite(36,LOW);
      digitalWrite(38,HIGH);
      digitalWrite(40,LOW);
      digitalWrite(42,LOW);
      delay(50);
   
        break;
 
    case 3:   
 
      digitalWrite(30,HIGH);
      digitalWrite(32,LOW);
      digitalWrite(34,LOW);
      digitalWrite(36,HIGH);
      digitalWrite(38,HIGH);
      digitalWrite(40,LOW);
      digitalWrite(42,LOW);
      delay(50);
       
        break;
   
    case 4: 

      digitalWrite(30,HIGH);
      digitalWrite(32,LOW);
      digitalWrite(34,HIGH);
      digitalWrite(36,LOW);
      digitalWrite(38,HIGH);
      digitalWrite(40,LOW);
      digitalWrite(42,HIGH);
      delay(50); 
     
        break;
 
    case 5:   

      digitalWrite(30,HIGH);
      digitalWrite(32,LOW);
      digitalWrite(34,HIGH);
      digitalWrite(36,HIGH);
      digitalWrite(38,HIGH);
      digitalWrite(40,LOW);
      digitalWrite(42,HIGH);
      delay(50);
     
        break;
 
    case 6:   
   
      digitalWrite(30,HIGH);
      digitalWrite(32,HIGH);
      digitalWrite(34,HIGH);
      digitalWrite(36,LOW);
      digitalWrite(38,HIGH);
      digitalWrite(40,HIGH);
      digitalWrite(42,HIGH);
      delay(50);
     
        break;
  }

}

 

Go Up