Actually for a project I am using Arduino Mega 2560, in which I am checking about 9 if conditions which just checks numbers high or less (no complicated algorithm inside if condition) for about 90 seconds without any kind of delay or delaymicroseconds, and I am in need of checking all conditions simultaneously and I am doing it using millis function, It just gives me a random behaviour like sometimes it is ok, and sometimes it is not as fast as i Expect it to be, So, What kind of arduino board do you suggest and what time does it take to check these 9 if conditions which checks just whether the given integer is high or low ?
So it looks like you are doing something wrong with your code.
My guess is that it is line 23. However my crystal ball is on the blink at the moment so just to be safe:-
Advice
In case you didn't understand @Grumpy_Mike, post your code. Without that we are blind to your problem.
...R
... in which I am checking about 9 if conditions ...
Sounds complex. Much worse than checking 7 conditions.
int led[12] = {2,3,4,5,6,7,8,9,10,11,12,13}; //declaring pins for 12 LEDS
int pb[12] = {22,23,24,25,26,27,28,29,30,31,32,33}; // declaring pins for 12 ir
int coinboxsensor1 = 34; // push button for starting the program
int coinboxsensor2 = 35;
int lcdons = 36;
int increment = 37;
int decrement = 38;
int reset = 39;
unsigned long gameLength=90000;//games last 30 seconds
unsigned long timePerLed=3000;// allow 2 seconds per light
unsigned int maxPointsPerLed=100;
boolean playing=false;
unsigned long gameStart;
unsigned long ledStart[3];
int currentLed[3];
unsigned long score;
unsigned long timerscore;
int i;
unsigned long timerscore1= 40;
void setup()
{
for(int n=0;n<12;n++)
{
pinMode(led[n], OUTPUT);
pinMode(pb[n], INPUT);
}
pinMode(coinboxsensor1, INPUT);// two sensors for switching the system on
pinMode(coinboxsensor2, INPUT);
pinMode(lcdons, OUTPUT);// switching the seven segment display for score
pinMode(increment, OUTPUT);// incrementing the seven segment display by one
pinMode(decrement, OUTPUT);// decrementing the seven segment display
pinMode(reset, OUTPUT);// resetting the display
Serial.begin(9600);
Serial.print("Press start button to start!");
}
void loop()
{
unsigned long t=millis();
//if not playing start a new game when startButton is pressed
if(playing == false)
{
int a= digitalRead(coinboxsensor1);
int b = digitalRead(coinboxsensor2);
if (a == 1 && b == 1)
{
playing=true;
//wait for button to be released
digitalWrite(lcdons, HIGH);
delay(1000);
digitalWrite(reset, HIGH);
delay(1000);
digitalWrite(reset, LOW);
gameStart=t;
score=0;
startlight();
}
}
//only continue on if we're in the midst of a game
//Check for end of game
if ( (t-gameStart) >= gameLength)
{
endGame();
return;
}
//Check for timeout on current led
for(i = 0; i<3; i++)
{
if( (t - ledStart[i]) > timePerLed)
{
nextLight();
return;
}
}
for(i = 0; i<3; i++)
{
if (digitalRead(pb[currentLed[i]]) == HIGH ) // check for appropriate ir sensor for the pressed led();
{
digitalWrite(increment, HIGH); // incrementing the score board
timerscore = millis();
//evaluate value of this hit based on time taken
nextLight();
//Serial.print("BANG + ");
//Serial.print (LEDvalue);
//Serial.println(" points");
}
}
if((t-timerscore>=timerscore1))
{
digitalWrite(increment, LOW);
}
}
void nextLight()
{
//extinguish current LED
digitalWrite (led[currentLed[i]], LOW);
//pick another
currentLed[i]=random(12);
//and light it.
digitalWrite(led[currentLed[i]], HIGH);
ledStart[i]=millis();
}
void endGame()
{
for(int n=0;n<12;n++)
{
pinMode(led[n], LOW);
}
for(int l = 0; l<3; l++)
{
digitalWrite(lcdons, HIGH);
delay(1000);
digitalWrite(lcdons, LOW);
delay(1000);
}
playing=false;
}
void startlight()
{
i = 0;
while(i<3)
{
currentLed[i] = random(12);
digitalWrite(led[currentLed[i]], HIGH);
ledStart[i] = millis();
i++;
}
}
I can't relate your original question to your code. Can you provide an explanation (a talk-through) of how the code is intended to work.
...R
I think you need to post a schematic as well. It sounds like you have floating inputs.
int coinboxsensor1 = 34; // push button for starting the program
That was my guess, based on the name of the variable. NOT!
Stupid names just don't cut it.
The code looked OK except for use of pinMode() in place of digitalWrite() in endGame(). Here is the code with some clean-up done:
const int NumLEDs = 12;
const int OnLEDs = 3;
const int led[NumLEDs] = {
2,3,4,5,6,7,8,9,10,11,12,13}; //declaring pins for 12 LEDS
const int pb[NumLEDs] = {
22,23,24,25,26,27,28,29,30,31,32,33}; // declaring pins for 12 ir
const int coinboxsensor1 = 34; // push button for starting the program
const int coinboxsensor2 = 35;
const int lcdons = 36;
const int increment = 37;
const int decrement = 38;
const int reset = 39;
const unsigned long gameLength=90000;//games last 30 seconds
const unsigned long timePerLed=3000;// allow 2 seconds per light
unsigned int maxPointsPerLed=100;
boolean playing=false;
unsigned long gameStart;
unsigned long ledStart[OnLEDs];
int currentLed[OnLEDs];
unsigned long score;
unsigned long timerscore;
// int i; // NOT an appropriate name for a global variable
const unsigned long timerscore1= 40;
void setup()
{
for(int n=0;n<NumLEDs;n++) {
pinMode(led[n], OUTPUT);
pinMode(pb[n], INPUT);
}
pinMode(coinboxsensor1, INPUT);// two sensors for switching the system on
pinMode(coinboxsensor2, INPUT);
pinMode(lcdons, OUTPUT);// switching the seven segment display for score
pinMode(increment, OUTPUT);// incrementing the seven segment display by one
pinMode(decrement, OUTPUT);// decrementing the seven segment display
pinMode(reset, OUTPUT);// resetting the display
Serial.begin(9600);
Serial.print("Press start button to start!");
}
void loop() {
unsigned long t=millis();
//if not playing start a new game when startButton is pressed
if(playing == false) {
int a = digitalRead(coinboxsensor1);
int b = digitalRead(coinboxsensor2);
if (a && b) {
playing=true;
//wait for button to be released
digitalWrite(lcdons, HIGH);
delay(1000);
digitalWrite(reset, HIGH);
delay(1000);
digitalWrite(reset, LOW);
gameStart=t;
score=0;
startlight();
}
}
//only continue on if we're in the midst of a game
//Check for end of game
if (t-gameStart >= gameLength) {
endGame();
return;
}
//Check for timeout on current leds
for(int i = 0; i<OnLEDs; i++) {
if( (t - ledStart[i]) > timePerLed) {
nextLight(i);
return; // ??? Don't care about other LEDs?
}
}
for(int i = 0; i<OnLEDs; i++) {
// check for appropriate ir sensor for the lit led();
if (digitalRead(pb[currentLed[i]]) == HIGH ) {
digitalWrite(increment, HIGH); // incrementing the score board
timerscore = t; // Set timer for turning pulse off
//evaluate value of this hit based on time taken
nextLight(i);
//Serial.print("BANG + ");
//Serial.print (LEDvalue);
//Serial.println(" points");
}
}
if(t-timerscore >= timerscore1) {
digitalWrite(increment, LOW);
}
}
void nextLight(int i) {
//extinguish current LED
digitalWrite(led[currentLed[i]], LOW);
//pick another
currentLed[i] = random(12);
//and light it.
digitalWrite(led[currentLed[i]], HIGH);
ledStart[i] = millis();
}
void endGame() {
// Turn off all the LEDs
for(int n=0; n<NumLEDs; n++)
// pinMode(led[n], LOW); // That should be digitalWrite()
digitalWrite(led[n], LOW);
// Blink the score three times
for(int l = 0; l<3; l++) {
digitalWrite(lcdons, HIGH);
delay(1000);
digitalWrite(lcdons, LOW);
delay(1000);
}
playing=false;
}
void startlight() {
for (int i=0; i<OnLEDs; i++) {
currentLed[i] = random(12);
digitalWrite(led[currentLed[i]], HIGH);
ledStart[i] = millis();
}
}
@john wasser, Thanks for your reply, code started at a different shape and evolved everyday, so comments might be chaotic, anyways thanks for that cleanup let me get you the update by working on it today, Thanks, And back to the original question, how many milli seconds once, will it check for the condition if I am using Arduino Mega 2560? and if I am using Arduino Uno? I just wanted to know how to calculate it? Again, Thanks
for(int i = 0; i<OnLEDs; i++) {
if( (t - ledStart[i]) > timePerLed) {
nextLight(i);
return; // ??? Don't care about other LEDs?
}
// ??? Don't care about other LEDs?// To check the particular condition for all three glowing led's I have used "for loop" actually, To be frank i don't know any other way in which it keeps checking for the condition all the time for all three glowing led's , if you have any other way, I am glad to receive it, Please ignore any coding mistakes I am beginner at it.