Serial & display help please

Hi, i have a big Problem with a Skatch. I use a freeduino and a sparkfun lcd shild.

The skatch worked well befor i included "Serial.print("a");". If the Skatch comes to a line with Serialprint (any wher it is) the arduino restarts.

I have no idea where the problem is, i search now some days but nothing worked.

Would be grat if somone could help me.
Thank you so much.

#include "LCD_driver.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"

int value_up;
int value_down;
int value_back;
int value_enter;
int value_analog5;
int value_analog6;

int line0=1;
int line1=1;
int ebene=0;
int contrast = 35;
int contrastbalken =0;
int aktuell=0;
int bewegen=1;

int automatik =0;
int fahren =0;
int licht =0;

int up =0;
int down =1;
int back =2;
int enter =3;
int analog5 =4;
int analog6 =5;

int IO_up =0;
int IO_down =0;
int IO_back =0;
int IO_enter =0;
int IO_analog5 =0;
int IO_analog6 =0;

int changed_up =0;
int changed_down =0;
int changed_back =0;
int changed_enter =0;
int changed_analog5 =0;
int changed_analog6 =0;

int old_up;
int old_down;
int old_back;
int old_enter;
int old_analog5;
int old_analog6;

//************************************************************************
//					Main Code
//************************************************************************
void	setup()
{
        Serial.begin(9600);
	ioinit();           //Initialize I/O
	LCDInit();	    //Initialize the LCD
        LCDContrast(contrast);
        LCDClear(BLACK);    // Clear LCD to a solid color
        LCDPutStr(" MCC Hohenlohe", 0, 4, YELLOW, BLACK); // Write instructions on display
        
        LCDPutStr("   RC-Remote", 20, 4, BLUE, BLACK);
        LCDPutStr("  Designed and ", 40, 4, BLUE, BLACK);
        LCDPutStr("    Scripted", 56, 4, BLUE, BLACK);
        LCDPutStr("by Dixi & Roman", 72, 10, BLUE, BLACK);
        LCDPutStr("      Mcc-", 94, 4, YELLOW, BLACK);
        LCDPutStr("  Hohenlohe.de", 110, 4, YELLOW, BLACK);
        contrastbalken = contrast;
        delay (3000);
        LCDClear(BLUE);
}
void	menu()
{
  
  
  if(ebene == 0)
  {
    
    LCDPutStr(" MCC Hohenlohe", 0, 4, YELLOW, BLUE);
    //LCDPutStr("Automatik", 16, 4, WHITE, BLUE);
    if(line0 == 1)
    {
      LCDPutStr("Automatik", 32, 4, ORANGE, BLUE);
    }
    else
    {
      LCDPutStr("Automatik", 32, 4, WHITE, BLUE);
    }
    if (automatik == 0)
    {
      LCDPutStr("Aus", 32, 85, WHITE, BLUE);
    }
    if (automatik == 1)
    {
      LCDPutStr("Ein", 32, 85, GREEN, BLUE);
    }
    
    if(line0 == 2)
    {
      LCDPutStr("Bewegen", 48, 4, ORANGE, BLUE);
    }    
    else
    {
      LCDPutStr("Bewegen", 48, 4, WHITE, BLUE);
    }
    if (bewegen == 1)
    {
      LCDPutStr("Slow  ", 48, 85, GREEN, BLUE);
    }
    if (bewegen == 2)
    {
      LCDPutStr("Medium", 48, 85, YELLOW, BLUE);
    }
    if (bewegen == 3)
    {
      LCDPutStr("High  ", 48, 85, RED, BLUE);
    }
    
    if(line0 == 3)
    {
      LCDPutStr("Fahren", 64, 4, ORANGE, BLUE);
    }
    else
    {
      LCDPutStr("Fahren", 64, 4, WHITE, BLUE);
    }
    if (fahren == 0)
    {
     LCDPutStr("Aus", 64, 85, WHITE, BLUE);
     }
    if (fahren == 1)
    {
      LCDPutStr("Ein", 64, 85, GREEN, BLUE);
    }
  
    if(line0 == 4)
    {
      LCDPutStr("Licht", 80, 4, ORANGE, BLUE);
    }
    else
    {
      LCDPutStr("Licht", 80, 4, WHITE, BLUE);
    }
    if (licht == 0)
    {
      LCDPutStr("Aus", 80, 85, WHITE, BLUE);
    }
    if (licht == 1)
    {
      LCDPutStr("Ein", 80, 85, GREEN, BLUE);
    }
    
    if(line0 == 5)
    {
      LCDPutStr("Einstellungen >", 96, 4, ORANGE, BLUE);
    }
    else
    {
      LCDPutStr("Einstellungen >", 96, 4, WHITE, BLUE);
    }
    if(line0 == 6)
    {
      LCDPutStr("Display       >", 110, 4, ORANGE, BLUE);
    }
    else
    {
      LCDPutStr("Display       >", 110, 4, WHITE, BLUE);
    }
  }
  if (ebene ==1)
  {
  }
  if(ebene ==2)
  {
    LCDPutStr("    Contrast", 0, 4, YELLOW, BLUE);
    LCDPutStr("+", 28, 6, WHITE, BLUE);
    LCDPutStr("-", 104, 6, WHITE, BLUE);
//    LCDSetRect(32, 4, 118, 16, 1, WHITE);
    LCDSetRect(32, 20, 75, 70, 0, WHITE);
    LCDSetRect(32, 71, 75, 120, 1, RED);
    LCDSetRect(76, 20, 118, 70, 1, GREEN);
    LCDSetRect(76, 70, 118, 120, 1, YELLOW);
    contrastbalken = 108 - contrast;
    LCDSetRect(45, 4, 108, 16, 1, WHITE);
    LCDSetRect(45, 4, contrastbalken, 16, 1, GREEN);
  }
  aktuell =1;
}

void analog()
{
  value_up = analogRead(up);
  value_down = analogRead(down);
  value_back = analogRead(back);
  value_enter = analogRead(enter);
  value_analog5 = analogRead(analog5);
  value_analog6 = analogRead(analog6);
  
  if (value_up >= 500)
  {
    IO_up = 1;
  }
  else
  {
    IO_up = 0;
  }
  if (value_down >= 500)
  {
    IO_down = 1;
  }
  else
  {
    IO_down = 0;
  }
  if (value_back >= 500)
  {
    IO_back = 1;
  }
  else
  {
    IO_back = 0;
  }
  if (value_enter >= 500)
  {
    IO_enter = 1;
  }
  else
  {
    IO_enter = 0;
  }
  if (value_analog5 >= 500)
  {
    IO_analog5 = 1;
  }
  else
  {
    IO_analog5 = 0;
  }
  if (value_analog6 >= 500)
  {
    IO_analog6 = 1;
  }
  else
  {
    IO_analog6 = 0;
  }
  //delay(100);
}
void changed()
{
  
  
  changed_up =0;
  changed_down =0;
  changed_back =0;
  changed_enter =0;
  changed_analog5 =0;
  changed_analog6 =0;
  
  if (old_up == 0 && IO_up == 1)    // taste "up"
  {
    aktuell =0;
    changed_up = 1;
    if(ebene==0)
    {
      if(line0 >= 2 )
      {
        line0--;
      }
      else
      if (line0 == 1)
      {
        line0 =6;
      }
    }
    if(ebene==2)
    {
      if(contrast <=55)
      {
        contrast++;
        LCDContrast(contrast);
        contrastbalken = 108 - contrast;
        LCDSetRect(45, 4, 108, 16, 1, WHITE);
        LCDSetRect(45, 4, contrastbalken, 16, 1, GREEN);
      }
    }
      
  }
  if (old_down == 0 && IO_down == 1)  // taste "down"
  {
    aktuell =0;
    changed_down = 1;
    if(ebene ==0)
    {
      if(line0 <= 5 )
      {
        line0++;
      }
      else
      if(line0 == 6)
      {
          line0 = 1;
      }
    }
    if(ebene==2)
    {
      if(contrast >= 1)
      {
        contrast--;
        LCDContrast(contrast);
        contrastbalken = 108 - contrast;
        LCDSetRect(45, 4, 108, 16, 1, WHITE);
        LCDSetRect(45, 4, contrastbalken, 16, 1, GREEN);
      }
    }
  }
  if (old_back == 0 && IO_back == 1)    // taste "back"
  {
    aktuell=0;
    changed_back = 1;
    ebene = 0;
    LCDClear(BLUE);
  }
  if (old_enter == 0 && IO_enter == 1)    //taste "enter"
  {
    aktuell=0;
    changed_enter = 1;
    
    if( ebene == 0)
    {
      if (line0 == 1)
      {
       if (automatik == 0)
       {
         automatik = 1;
       }
       else
       if (automatik ==1) 
       {
         automatik = 0;
       }
      }
      if (line0 ==2)
      {
        if (bewegen == 1)
        {
          bewegen++;
        }
        else
        if (bewegen == 2)
        {
          bewegen++;
        }
        else
        if (bewegen == 3)
        {
          bewegen=1;
        }      
      }
      if (line0 == 3)
      {
        if(fahren == 0)
        {
          fahren++;
        }
        else
        if (fahren == 1)
        {
          fahren --;
        }
      }
      if(line0 == 4)
      {
        if(licht == 0)
        {
          licht++;
        }
        else 
        if(licht == 1)
        {
          licht--;
        }
      }
      if(line0 == 5)
      {
        ebene = 1;
      }
      if(line0 == 6)
      {
        ebene = 2;
      }
    }
    if(ebene == 1)
    {
      LCDClear(BLUE);
    }
    if(ebene == 2)
    {
      LCDClear(BLUE);
      
    }
      
  }
  
  
  old_up = IO_up;
  old_down = IO_down;
  old_back = IO_back;
  old_enter = IO_enter;
  old_analog5 = IO_analog5;
  old_analog6 = IO_analog6;
//  delay(100);
  
}
void loop()
{
  
  analog();
  changed();
  if (aktuell == 0)
  {
    menu();
  }
   Serial.print("a");   [font=Verdana][color=red]//the arduino restarts if it comes to this line[/color][/font]
}

It's a good job you posted this before the sketch got too big.

Go back, click on "modify", highlight the code, then click on the # icon on the editor's toolbar.

Probably running short of RAM

The Problem is the flowing if i remove a "void" anyway wich one it works, if i make it shorter it works very well but if i upload it with all this i showed it rebootes the arduino so i dont know in wich part the problem is.

Very sorry for the big sketch but i have no idea how to show it in a shorter way.

Unfortunatly the way this compiler/runtime works, any
** **"text"** **
you use - like in the menu - is stored in RAM. Even though it is never changed. So the first change you can make is to remove all the pretty leading text, like [b]"Designed by"[/b]. There are ways with PROGMEM to work around this problem, but lets first make sure that is the problem. There where you print values, reduce the leading text "Bewegen" to "B". If your sketch then works, we can discuss using PROGMEM.

instead of :

if (value_down >= 500)
{
IO_down = 1;
}
else
{
IO_down = 0;
}

you can write :

IO_down = (value_down >= 500)?1:0;

you can do it a lot of time in your code.

This way of writing code will gain 2 bytes !!!!

if( ebene == 0)
  {
    if (line0 == 1)
    {
      if (automatik == 0)
      {
        automatik = 1;
      }
      else
        if (automatik ==1) 
        {
          automatik = 0;
        }
    }
    if (line0 ==2)
    {
      if (bewegen == 1)
      {
        bewegen++;
      }
      else
        if (bewegen == 2)
        {
          bewegen++;
        }
        else
          if (bewegen == 3)
          {
            bewegen=1;
          }      
    }
  }

can be changed by :

if( ebene == 0)
  {
    if (line0 == 1)
    {
      automatik = automatik?0:1;
    }
    if (line0==2)
    {
      switch( bewegen ) 
      {
      case 1:
      case 2:
        bewegen++;
        break;
      case 3:
        bewegen=1;
      }
    }
  }

will gain 18 extra bytes

instead of :

if (value_down >= 500)
{
IO_down = 1;
}
else
{
IO_down = 0;
}

you can write :

IO_down = (value_down >= 500)?1:0;

Or even:

IO_down = (value_down >= 500);

Yes ;o) this is the top of optimization. Sometimes we forgot the simplest way of doing things. Isn't it ?

First i want to say thanks to all try to help me. How you can see i am new with scripting C.

I will try to change everthing you told me.

i will tell you if it worked :slight_smile:

Ok i tryed to remove the boot screen in the void setup.

void	setup()
{
        Serial.begin(9600);
	ioinit();           //Initialize I/O
	LCDInit();	    //Initialize the LCD
        LCDContrast(contrast);
        //LCDClear(BLACK);    // Clear LCD to a solid color
        //LCDPutStr(" MCC Hohenlohe", 0, 4, YELLOW, BLACK); // Write instructions on display
        
        //LCDPutStr("   RC-Remote", 20, 4, BLUE, BLACK);
        //LCDPutStr("  Designed and ", 40, 4, BLUE, BLACK);
        //LCDPutStr("    Scripted", 56, 4, BLUE, BLACK);
        //LCDPutStr("by Dixi & Roman", 72, 10, BLUE, BLACK);
        //LCDPutStr("      Mcc-", 94, 4, YELLOW, BLACK);
        //LCDPutStr("  Hohenlohe.de", 110, 4, YELLOW, BLACK);
        contrastbalken = contrast;
        //delay (3000);
        LCDClear(BLUE);
}

And ........ IT WORKS. Thank you all so much.

but ther is my next question, is ther a way to have a bootscreen without the problems? If not it is ok but i would like to have one. I do not really understand why it dont work, could somone explain me what the problem is?

is ther a way to have a bootscreen without the problems?

Yes, you can put the strings into PROGMEM
http://www.arduino.cc/en/Reference/PROGMEM

I do not really understand why it dont work

The issue is simply that the Arduino doesn't have very much RAM & it looks like you used it all. Getting rid of those strings pulled you back from the brink. As AWOL says, you can get around it using PROGMEM.