Condition check in for loop is not working :D - panic error
Posted: Tue Mar 08, 2016 7:46 pm
Hi, here is a fragment of my code:
And here is a fragment of the serial output:
What's going on? "j" gets higher value than "n-i" and still the loop never ends.
Any ideas on how to solve this?
Code: Select all
Serial.println("Starting...");
int i,j,n,mediana;
float median,a[5],t,mediany=0;
for (mediana=1; mediana <= 5; mediana++)
{
Serial.println("Mediana: "+ String(mediana));
n=5;
for (i = 1; i <= n ; i++)
{
digitalWrite(pin, LOW);
delayMicroseconds(280);
a[i] = analogRead(A0); //100 microseconds (on ESP8266 too?)
//delayMicroseconds(100);
Serial.println(a[i]);
digitalWrite(pin, HIGH);
delayMicroseconds(720);
delay(9); //9
}
Serial.println("here ok");
/* Sorting begins */
for (i = 1 ; i <= n-1 ; i++)
{ /* Trip-i begins */
Serial.println("i="+String(i));
Serial.println("n-i="+String(n-i));
for (j = 1 ; j <= n-i ; j++)
{
Serial.println("j="+String(j)+" n-i="+String(n-i));
if (a[j] <= a[j+1])
{ /* Interchanging values */
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
} /* sorting ends */
Serial.println("still ok");
/* calculation of median */
if ( n % 2 == 0)
median = (a[n/2] + a[n/2+1])/2.0 ;
else
median = a[n/2 + 1];
Serial.println("Mediana: "+String(median));
mediany = mediany+median;
}
mediany = round(mediany/10);
Serial.println("Final median: " + String(mediany));
And here is a fragment of the serial output:
Code: Select all
Starting...
Mediana: 1
137.00
113.00
110.00
93.00
100.00
here ok
i=1
n-i=4
j=1 n-i=4
j=2 n-i=4
j=3 n-i=4
j=4 n-i=4
j=5 n-i=4
j=6 n-i=4
j=7 n-i=4
j=8 n-i=4
j=9 n-i=4
j=10 n-i=4
j=11 n-i=4
j=12 n-i=4
j=13 n-i=4
j=14 n-i=4
j=15 n-i=4
j=16 n-i=4
j=17 n-i=4
j=18 n-i=4
j=19 n-i=4
j=20 n-i=4
j=21 n-i=4
j=22 n-i=4
j=23 n-i=4
j=24 n-i=4
j=25 n-i=4
j=26 n-i=4
j=27 n-i=4
j=28 n-i=4
j=29 n-i=4
j=30 n-i=4
j=31 n-i=4
j=32 n-i=4
j=33 n-i=4
j=34 n-i=4
j=35 n-i=4
j=36 n-i=4
j=37 n-i=4
j=38 n-i=4
j=39 n-i=4
j=40 n-i=4
Panic C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\cores\esp8266\core_esp8266_main.cpp:114 loop_task
ctx: sys
sp: 3ffffdd0 end: 3fffffb0 offset: 01b0
>>>stack>>>
3fffff80: 40217eee 3fffdab0 00000000 3ffefcf0
3fffff90: 00000000 3fffdc20 3ffefce0 4020cf7d
3fffffa0: 40000f49 40000f49 3fffdab0 40000f49
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
What's going on? "j" gets higher value than "n-i" and still the loop never ends.
Any ideas on how to solve this?