Loop Question

Hi,

I am trying to get used to the Arduino programming but am finding it tough to debug a program once the error is a runtime type and it runs off the Board. Any better alternative to debugging?

Anyway, my real problem is around the loop functionality. I am used to using them in other environments but I cannot figure this one out. I noted that the loop runs once only and the while loop doesn’t repeat itself. I tried a much simpler listing and it worked, but not this one. I also tried For and Do loops to no avail. It seems to be with the way this program is setup. Could be bad programming as I’m a newbie in this environment.

I am running it off a Mega2560, lcd 16x2 and IIC2. The silly lcd.print statements in the listing were set up for me to determine on the lcd where it is getting stuck and it is displaying:
Row 1 - “1232” from column 0
Row 2 - “stuck1” from column 8

and repeating it with the delays. It is hence not incrementing xx variable (the “2” in column 3 row 1).

Code follows. Help is appreciated.

thanks

#include <Wire.h>
 #include <LCD.h>
 #include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library


#define  LED_OFF  0
#define  LED_ON  1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int xx;
int previousVal;
int minimumVal;
int mVal;
long totalVal;

void setup()
{
   lcd.begin (16,2);  // initialize the lcd
   lcd.backlight();

}


void loop()
{ 

       lcd.clear();
        totalVal=0;
//        x=1;
    while (xx<10){  
    mVal=analogRead(1);

    if(xx=1)
      {
        minimumVal=mVal;
       lcd.setCursor(8,1);
      lcd.print("stuck");
      lcd.print(xx);
      delay(300);
      lcd.clear();
      delay(300);
      }
    else
       {
       minimumVal=min(previousVal,mVal);
     
      lcd.setCursor(8,1);
      lcd.print("unstuck");
      lcd.print(xx);
      delay(300);
      lcd.clear();
      delay(300);
       
       }
    lcd.setCursor(0,0);
    lcd.print("1");

    previousVal=mVal;
    lcd.print("2");
    totalVal=totalVal+mVal;
    lcd.print("3");
    xx++;
    lcd.print(xx);
    delay(300);
  };
      lcd.setCursor(9,0);
      lcd.print("done");
      delay(300);
 
      totalVal=totalVal/100;
     
      lcd.setCursor(0,0);
      lcd.print(totalVal);
      lcd.setCursor(0,1);
      lcd.print(minimumVal);

      delay(30); 
      xx=1;
}
if(xx=1)

This is your loop index and you are resetting it to 1 every time. You probably mean xx==1

Hi, welcome to the forum.

In the menu of the Arduino IDE is an auto-text-format, that way we can read the sketch.

The lcd is not very good to print debug messages to. Please use the Serial.begin(), Serial.print() and Serial.println() to write messages to the serial monitor. Write everything you want to know to the serial monitor. If that is okay, then you can write data to the lcd.

The loop() function runs over and over again.

I don't understand what you want to do in the sketch. But I did notice a bug : "if ( xx = 1 )" sets the 'xx' to '1'. You want to compare 'xx', so use : "if ( xx == 1 )".

(marco_c already saw the bug while I was still writing this)

while (xx<10){

The first time loop( ) runs, this variable is uninitialized.

The first time loop( ) runs, this variable is uninitialized.

No xx is global and therefore it is initialized to 0.

Mark

michinyon and holmes4 are both right. The Arduino (avr-gcc compiler) fills unused variables with zeros, I think most 'c' compilers do that. But a good software engineer should not rely on that, and set the initial value to zero if it must be zero.

When using variables in flash memory (with a pointer or some other way), a mistake is easily made, since that is not pre-filled with zero. I don't know about a float variable... will it be 0.0 ? I don't think so. It is also different for variables on the stack. I worked with compilers that did not set it to zero. I think Turbo Pascal did, and later that was done in the 'c'-language as well.

According to the C++ Standard, global variables are required to be initialized to zero. Your main sketch is compiled as a C++ program.

3.6.2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

Hi all,

I'm impressed with the response.

Excuse my ignorance in C. I didn't know that the xx=1 in an if would move 1 into xx.

Also, what Peter_n said is also news to me. I never knew there was a serial monitor. Thanks a lot for the tip!

I think I will have to learn C!!!

I'll confirm that all works.

Thanks again a lot!

Chris

btw - it worked!