I am trying to create a two dice arduino project with an accelerometer where you jiggle or turn the breadboard and two sets of leds (in the shape of dice) light up two different numbers. I've got it working... some of the time. Sometimes both sets light up different numbers and other times only one lights up. They alternate back and forth for which one lights up, too, so it's not a problem with just one set of leds. I can't figure out what I'm doing wrong. Can someone look at my code and give me any suggestions?
//Led pins
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
int led8 = 8;
int pinLeds1 = 10;
int pinLeds2 = 9;
int pinLeds3 = 7;
int pinLeds4 = 8;
int pinLeds1a = 11;
int pinLeds2a = 12;
int pinLeds3a = 4;
int pinLeds4a = 5;
//Button pin don't need this
int buttonPin = 6;
int buttonState;
//Ran will be randomized from 1 to 6
long ran;
long ran2;
//Time is the time of delay
int time = 2000;
void setup ()
{
//Set the pins of the leds as Output
pinMode (pinLeds1, OUTPUT);
pinMode (pinLeds2, OUTPUT);
pinMode (pinLeds3, OUTPUT);
pinMode (pinLeds4, OUTPUT);
pinMode (pinLeds1a, OUTPUT);
pinMode (pinLeds2a, OUTPUT);
pinMode (pinLeds3a, OUTPUT);
pinMode (pinLeds4a, OUTPUT);
//This code line is necessary for a correct random
randomSeed(analogRead(0));
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop()
{
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin, HIGH);
pulseY = pulseIn(yPin, HIGH);
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
if (accelerationX > 100)
{
ran = random(7);
ran2 = random(7);
//Number 1
if (ran == 1){
digitalWrite (pinLeds4, HIGH);
}
//Number 2
if (ran == 2){
digitalWrite (pinLeds1, HIGH);
}
//Number 3
if (ran == 3){
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLeds4, HIGH);
}
//Number 4
if (ran == 4){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
}
//Number 5
if (ran == 5){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLeds4, HIGH);
}
//Number 6
if (ran == 6){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH);
}
//Number 1
if (ran2 == 1){
digitalWrite (pinLeds4a, HIGH);
}
//Number 2
if (ran2 == 2){
digitalWrite (pinLeds1a, HIGH);
}
//Number 3
if (ran2 == 3){
digitalWrite (pinLeds3a, HIGH);
digitalWrite (pinLeds4a, HIGH);
}
//Number 4
if (ran2 == 4){
digitalWrite (pinLeds1a, HIGH);
digitalWrite (pinLeds3a, HIGH);
}
//Number 5
if (ran2 == 5){
digitalWrite (pinLeds1a, HIGH);
digitalWrite (pinLeds3a, HIGH);
digitalWrite (pinLeds4a, HIGH);
}
//Number 6
if (ran2 == 6){
digitalWrite (pinLeds1a, HIGH);
digitalWrite (pinLeds2a, HIGH);
digitalWrite (pinLeds3a, HIGH);
}
delay (time);}
//If the button is not pressed, sets off the leds
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLeds4, LOW);
digitalWrite (pinLeds1a, LOW);
digitalWrite (pinLeds2a, LOW);
digitalWrite (pinLeds3a, LOW);
digitalWrite (pinLeds4a, LOW);
}