Go Down

Topic: LED Dice with Arduino posting by Switchblade_46 June 14, 2010 (Read 3455 times) previous topic - next topic

Av8ter

I have built up a shield using a copper clad board which works well.  I'm having a problem with the original code copied verbatum from the posting by Switchblade_46.  Die number 1 rolls only 5's. I can not find the problem, and if there is a corrected version out there somewhere, I haven't been able to find it.  Any help will be greatly appreciated.  Thanks for your time and help.

Av8ter

Grumpy_Mike


Av8ter

Hi Karma,
Sorry.  Thanks for you help.

http://forum.arduino.cc/index.php/topic,8109.0.html

Grumpy_Mike

Wow what god dam awful code that is. As a general rule if you find yourself coding the same sort of line over and over then it it time you learnt some more about programming.

The first thing to do is to see if your hardware is correct. Just write a simple sketch that will put each pattern in turn on both of the dice. If that works then it is time to delve into that code.

Av8ter

It looked like a decent exercise in LED manipulation, but it proves that I don't know what I'm doing and need help with the coding.  I'm an amateur radio operator and have built a good number of kits and homebrew devices but have had very little programming on PC.  The last language I wrote in was NEAT 3 for NCR Century 100 mainframes in the 1970's.For this sketch,  I followed the schematic, and built a crowded but electronicly clean device. With each "toss",  die 1 goes thru three sequences, "rolls", 1-5 stops and gives random results of 1-6 , but die 2 does 3 sequences and stops at 5 each time. The hardware, I would declare, is functioning well, it's just the code that seems to be screwing up  Do you think I need to write a short sketch or would this action, so far, suffice?  I appreciate your interest and your time.  Thanks again.

runaway_pancake

Not sure how any of that applies, but,
Quote

The hardware, I would declare, is functioning well,

believe it or don't, it wouldn't be the first time that a first-timer did everything right and nothing wrong - only to have botched it.
Mike just wanted to establish that the display is sound; pretty reasonable.

Please add the following line to your void setup ( ) --
Serial.begin(9600);

And modify your void loop ( ) by adding the lines I've commented ** NEW LINE ** --
Code: [Select]

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){ // checks state of pushbutton to start loop
    ran1 = random(1, 7); // a number between 1 to 6 is randomized for die 1
    Serial.print("Ran_1 = ");   // ** NEW LINE **
    Serial.println(ran1, DEC);  // ** NEW LINE **
    ran2 = random(1, 7); // a number between 1 to 6 is randomized for die 2
    Serial.print("Ran_2 = ");   // ** NEW LINE **
    Serial.println(ran2, DEC);  // ** NEW LINE **


I hope you can get the Serial Monitor going.  The additions will help determine if Ran2 ever comes up with a new number.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

Grumpy_Mike

Quote
The hardware, I would declare, is functioning well

But you have no proof of this. As RP says, the problem could be in the software or the hardware and the software does not look too bad in that respect.

Quote
With each "toss",  die 1 goes thru three sequences, "rolls", 1-5 stops and gives random results of 1-6 , but die 2 does 3 sequences and stops at 5 each time.

Yes I can see that but you need to test that when your random number is say 6 then the 6 lights up.
Another way of testing this is to replace the lines:-
Code: [Select]
   
   ran1 = random(1, 7); // a number between 1 to 6 is randomized for die 1
   ran2 = random(1, 7); // a number between 1 to 6 is randomized for die 2

with
Code: [Select]
   
   ran1 = 6;
   ran2 = 6;

and seeing if it displays a double 6.

The important thing is that you must test in order to find out what is wrong. As it is much easier to copy and paste code than to copy hardware my guess is that you screwed up the hardware rather than the OP screwed up the copy and paste for the code. But this is only my guess based on 40 years experience so what do I know. I only know if it were my project I would check the hardware first before blaming any code.

When ever you have a fault that is hard to find then it turns out the problem is always the thing that is obviously correct.

Av8ter

Ah! I understand.  Thanks very much, gentlemen, for you help.

Av8ter

Wow, of course,  RP and Mike you were both absolutely correct. Mike, I followed your advice first.  I commented out the two lines with // a hand added ran1=6; and ran2 = 6;.  Nothing changed with the shield ouput.  I then did the changes RP suggested to learn about the Serial Monitor and it's functions, and it showed that random numbers were being generated for each die, but the output for die 1 was still 5.  I stated before the each die was sequencing thru 1-5, but that was wrong. The sequence is: The center LED then a left diagonal LEDs, vertical, and right diagonal.  It does this twice then stops with the constant 5 and random number on die 1.  My next question is, if all lights are functioning, how can I determine where the hardware problem is.  I've spent some time building this shield and would certainly like the darned thing to work right.  Thanks again for any help.

runaway_pancake

This sketch turns on each of the pinLeds groups for two seconds each, in succession, with a note in Serial Monitor as to where it's at / what you should be looking for:

Code: [Select]

// dice troubleshooting

const int pinLeds1 = 4;  // assign group of leds1 to digital pin 4
const int pinLeds2 = 6;  // assign group of leds2 to digital pin 6
const int pinLeds3 = 7;  // assign group of leds3 to digital pin 7
const int pinLed4 = 5;   // assign led4 to digital pin 5
const int buttonPin = 3; // assign push-button digital pin 3
const int pinLeds5 = 8;  // assign group of leds5 to digital pin 8
const int pinLeds6 = 10; // assign group of leds6 to digital pin 10
const int pinLeds7 = 11; // assign group of leds7 to digital pin 11
const int pinLed8 = 9;   // assign led8 to digital pin 9
int buttonState;         // define pushbutton state
int ran1;                // define random number for die 1
int ran2;                // define random number for die 2
int time1 = 100;         // define time1 interval as 0.1 seconds
int time2 = 200;         // define time2 interval as 0.2 seconds
int time3 = 300;         // define time2 interval as 0.3 seconds

// The setup() method runs once, when the sketch starts
void setup ()
{
  pinMode (pinLeds1, OUTPUT); // declare pinLeds1 as output
  pinMode (pinLeds2, OUTPUT); // declare pinLeds2 as output
  pinMode (pinLeds3, OUTPUT); // declare pinLeds3 as output
  pinMode (pinLed4, OUTPUT);  // declare pinLed4 as output
  pinMode (buttonPin, INPUT); // declare buttonPin as input
  pinMode (pinLeds5, OUTPUT); // declare pinLeds5 as output
  pinMode (pinLeds6, OUTPUT); // declare pinLeds6 as output
  pinMode (pinLeds7, OUTPUT); // declare pinLeds7 as output
  pinMode (pinLed8, OUTPUT);  // declare pinLed8 as output
  randomSeed(analogRead(0));  // to avoid arduino to follow any pattern
 
  Serial.begin(9600);
}

// the loop() method runs over and over again, tosses the dice until you are bored or you are finished with it.
void loop()
{
  Serial.println("pinLeds1");
  digitalWrite(pinLeds1,HIGH);
  delay(2000);
  digitalWrite(pinLeds1,LOW);

  Serial.println("pinLeds2");
  digitalWrite(pinLeds2,HIGH);
  delay(2000);
  digitalWrite(pinLeds2,LOW);
 
  Serial.println("pinLeds3");
  digitalWrite(pinLeds3,HIGH);
  delay(2000);
  digitalWrite(pinLeds3,LOW);
 
  Serial.println("pinLed4");
  digitalWrite(pinLed4,HIGH);
  delay(2000);
  digitalWrite(pinLed4,LOW);
}


It's just for one "die". 
I'll let you tackle the other.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

Av8ter

The Saga continues.  I copied your sketch and set up  die  2.  The Serial Monitor displayed groups1,2,3 and the single 4 and displayed groups 5,6,7 and the single 8 and the appropriate LEDs  turned on with a 2 second pause between.  Doesn't that mean the hardware is ok?  Now what? If it's the software, I'm afraid I haven't been able to spot the errors.  Any thoughts?  Thanks.

runaway_pancake

Everytime I look at it, I get a headache.  I would have written this with an output for each die dot, 7 each, 14 in total; the pinLed/pinLeds thing is aggravating.

I stripped out all of the "rolling" jazz.
Does this work out any better?

Code: [Select]

const int pinLeds1 = 4;  // assign group of leds1 to digital pin 4
const int pinLeds2 = 6;  // assign group of leds2 to digital pin 6
const int pinLeds3 = 7;  // assign group of leds3 to digital pin 7
const int pinLed4 = 5;   // assign led4 to digital pin 5
const int buttonPin = 3; // assign push-button digital pin 3
const int pinLeds5 = 8;  // assign group of leds5 to digital pin 8
const int pinLeds6 = 10; // assign group of leds6 to digital pin 10
const int pinLeds7 = 11; // assign group of leds7 to digital pin 11
const int pinLed8 = 9;   // assign led8 to digital pin 9
int buttonState;         // define pushbutton state
int ran1;                // define random number for die 1
int ran2;                // define random number for die 2
int time1 = 100;         // define time1 interval as 0.1 seconds
int time2 = 200;         // define time2 interval as 0.2 seconds
int time3 = 300;         // define time2 interval as 0.3 seconds

// The setup() method runs once, when the sketch starts
void setup ()
{
  pinMode (pinLeds1, OUTPUT); // declare pinLeds1 as output
  pinMode (pinLeds2, OUTPUT); // declare pinLeds2 as output
  pinMode (pinLeds3, OUTPUT); // declare pinLeds3 as output
  pinMode (pinLed4, OUTPUT);  // declare pinLed4 as output
  pinMode (buttonPin, INPUT); // declare buttonPin as input
  pinMode (pinLeds5, OUTPUT); // declare pinLeds5 as output
  pinMode (pinLeds6, OUTPUT); // declare pinLeds6 as output
  pinMode (pinLeds7, OUTPUT); // declare pinLeds7 as output
  pinMode (pinLed8, OUTPUT);  // declare pinLed8 as output
  randomSeed(analogRead(0));  // to avoid arduino to follow any pattern
}

// the loop() method runs over and over again, tosses the dice until you are bored or you are finished with it.
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  { // checks state of pushbutton to start loop
    ran1 = random(1, 7); // a number between 1 to 6 is randomized for die 1
    ran2 = random(1, 7); // a number between 1 to 6 is randomized for die 2
   
    // ----- clear dice -----
    digitalWrite (pinLeds1, LOW);
    digitalWrite (pinLeds2, LOW);
    digitalWrite (pinLeds3, LOW);
    digitalWrite (pinLed4, LOW);
    digitalWrite (pinLeds5, LOW);
    digitalWrite (pinLeds6, LOW);
    digitalWrite (pinLeds7, LOW);
    digitalWrite (pinLed8, LOW);
    delay (time3);
    // ----------------------
    if (ran1 == 1)
    {
      digitalWrite (pinLed8, HIGH); // turns on led8 if ran1 equals to 1
    }
    if (ran1 == 2)
    {
      digitalWrite (pinLeds5, HIGH); // turns on leds5 if ran1 equals to 2
    }
    if (ran1 == 3)
    {
      digitalWrite (pinLeds7, HIGH);
      digitalWrite (pinLed8, HIGH); // turns on leds7 and led8 if ran1 equals to 3
    }
    if (ran1 == 4)
    {
      digitalWrite (pinLeds5, HIGH);
      digitalWrite (pinLeds7, HIGH); // turns on leds5 and leds7 if ran1 equals to 4
    }
    if (ran1 == 5)
    {
      digitalWrite (pinLeds5, HIGH);
      digitalWrite (pinLeds7, HIGH);
      digitalWrite (pinLed8, HIGH); // turns on leds5 and leds7 and led8 if ran1 equals to 5
    }
    if (ran1 == 6)
    {
      digitalWrite (pinLeds5, HIGH);
      digitalWrite (pinLeds6, HIGH);
      digitalWrite (pinLeds7, HIGH); // turns on leds5 and leds6 and leds7 if ran1 equals to 6
    }
    // --------------------------------------
    if (ran2 == 1)
    {
      digitalWrite (pinLed4, HIGH); // turns on led4 if ran2 equals to 1
    }
    if (ran2 == 2)
    {
      digitalWrite (pinLeds1, HIGH); // turns on leds1 if ran2 equals to 2
    }
    if (ran2 == 3)
    {
      digitalWrite (pinLeds3, HIGH);
      digitalWrite (pinLed4, HIGH); // turns on leds3 and led4 if ran2 equals to 3
    }
    if (ran2 == 4)
    {
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds3, HIGH); // turns on leds1 and leds3 if ran2 equals to 4
    }
    if (ran2 == 5)
    {
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds3, HIGH);
      digitalWrite (pinLed4, HIGH); // turns on leds1 and leds3 and led4 if ran2 equals to 5
    }
    if (ran2 == 6)
    {
      digitalWrite (pinLeds1, HIGH);
      digitalWrite (pinLeds2, HIGH);
      digitalWrite (pinLeds3, HIGH); // turns on leds1 and leds2 and leds3 if ran2 equals to 6
    }     
  }
}
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

Av8ter

Thank you very much for all your time and trouble!!!  It works just fine without all the roll junk.  I take your point on the "headache" thing.  Thanks to GM and especially you, I'm learning something and hope to write my own sketches and I will certainly try to rewrite this sketch using your suggestions.  Thanks once again.

Go Up