Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Programs >> Program won't enter loop method
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1180009229

Message started by Dave on 05/24/07 at 12:20:27

Title: Program won't enter loop method
Post by Dave on 05/24/07 at 12:20:27
Hi out there,

I've started to code on the wiring board 2 weeks or so ago. Now I've come across a problem with one of my programs which I don't quite understand.

This is my code:


Code:
//Params////////////////////////
const int history_size = 1000;
                         
int outpin_low = 0;      
int outpin_med = 1;
int outpin_high = 2;

int inpin_low = 0;
int inpin_med = 1;
int inpin_high = 2;

////////////////////////////////

//Other Variables
int value_low = 0;
int value_med = 0;
int value_high = 0;

int low_history[history_size];
int med_history[history_size];
int high_history[history_size];

int average_low = 0;
int average_med = 0;
int average_high = 0;

int history_counter = 0;


//Setup//////////////////////////////
void setup()
{
 pinMode(48, OUTPUT);

 int i;
 //clear history
 for ( i = 0; i < history_size; i++)
 {
    low_history[i] = 0;
    med_history[i] = 0;
    high_history[i] = 0;
 }
 digitalWrite(48, HIGH); //First Digitalwrite
}

//Loop//////////////////////////////////////////////
void loop()
{
 digitalWrite(48, HIGH);  //Second Digitalwrite

 readValues();
 

 
 if (value_low > average_low) analogWrite(outpin_low, 250);
 else  analogWrite(outpin_low, 0);
 
 if (value_med > average_med) analogWrite(outpin_med, 250);
 else  analogWrite(outpin_med, 0);
 
 if (value_high > average_high) analogWrite(outpin_high, 250);
 else  analogWrite(outpin_high, 0);
 
 updateHistory();
}

//Read values from the pins/////////////
void readValues()
{
 value_low = analogRead(inpin_low);
 value_med = analogRead(inpin_med);
 value_high = analogRead(inpin_high);
}

//Calc Average of int array and return//////////
int average(int array[], int size)
{
 int i;
 int result = 0;
 for(i = 0; i < size; i++) result += array[i];
 return result / size;
}

//Calculate the history averages ///////////////////
void calcAverages()
{
  average_low = average(low_history, history_size);
  average_med = average(med_history, history_size);
  average_high = average(high_history, history_size);
}

void updateHistory()
{
 low_history[history_counter] = value_low;
 med_history[history_counter] = value_med;
 high_history[history_counter] = value_high;
 
 history_counter++;
 if (history_counter >= history_size) history_counter = 0;
}


It verifies and uploads alright. Now the problem is, it doesn't seem to enter the loop method. If I switch on the LED at pint 48 right at the end of setup() it lights up after starting the wiring board. If I switch on the same LED right at the beginning of loop() it doesn't light up, and no other code inside loop() seems to be executed. Does anyone see a problem in my code?

Title: Re: Program won't enter loop method
Post by barragan on 05/24/07 at 20:00:55
hey try to add this to the loop so the boad led should be going ON at the beginning and OFF at the end of the loop:

void loop()
{
 digitalWrite(48, HIGH);  //Second Digitalwrite

 readValues();



 if (value_low > average_low) analogWrite(outpin_low, 250);
 else  analogWrite(outpin_low, 0);

 if (value_med > average_med) analogWrite(outpin_med, 250);
 else  analogWrite(outpin_med, 0);

 if (value_high > average_high) analogWrite(outpin_high, 250);
 else  analogWrite(outpin_high, 0);

 updateHistory();
 digitalWrite(48, LOW);
 delay(100);
 
}

Title: Re: Program won't enter loop method
Post by Dave on 05/30/07 at 10:58:43
Hello Hernando,

thanks a lot for replying so fast. I wasn't home during whitsun.
As I say, the program won't enter the loop method. I did put in the
2 lines you suggested, but nothing happens. As I wrote if I take
out the the first digitalWrite in the setup() method, the light doesn't
go on at all, because loop() is never executed. Have you tried my
code on your board? Because if it works for you but not for me, something
else probably is wrong then.

Thanks

Dave

Title: Re: Program won't enter loop method
Post by Dave on 05/30/07 at 12:03:43
Ok seems like I found the problem. I was assigning 3 arrays with the capacity of 1000. Looks like that is too much. How much memory can I assign on the wiring?

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.