Program stack when trying to open serial port

Hello all.

Let me try again :blush:

I wrote a program for my project which works with a motor and LCD And every time i'm trying to start serial port "Serial.begin(9600);" for Serial Print, it seems to stack in the Loop Function.

  • Its 16x2 LCD I2C with <LiquidCrystal_I2C.h> Lib.
  • The motor controlled threw the "Arduino Motor Shield"
  • I also use rotary Encoder with <Encoder_Polling_V2.h> Lib.

The serial print is to check the encoder. When i wrote a code to check my Encoder, Every thing worked well and the serial print worked just fine, also when i tried to use the port at another program. But when i copied the code to the original project things went wrong.
Moreover, the print worked in all the "Initialization" functions and then stacked in the LOOP func.
For example: I Can see the message ""Hello" at the Setup Func, and also "Init..... DONE" at the getInitAngle() Func.
I Have putted "Serial.print("E ");" at the beginning of the LOOP() just for check, and few time it managed to show "E " 2 times before it stacked.

The program needs to control motor direction based on potenziometer Values (the Encoder will replace it at the future).

What can mess up??

This is the LOOP()

void loop()
{
Serial.print("E ");  // Trying to display "E "
  // Direction State
  angleWorkinkg = analogRead(resistor_pin); // perform callculation to determine the angle(not voltage)
        //lcd.clear();
        //lcd.setCursor(0,lcd_first_row);
        //lcd.print("Angle: ");
       // lcd.print(angleWorkinkg);
       // delay(200);
  int dir_0 = encoder_data(0);  //Read First encoder
  //  Serial.print("E ");
  
  if(dir_0 != 0)  // Check for rotation
  {
    //Serial.print("Encoder 0: ");
    //Serial.println(dir_0);
    encoder_angle_counter = encoder_angle_counter + dir_0;
    lcd.clear();
    lcd.setCursor(0,lcd_first_row);
    lcd.print("Angle: ");
    lcd.print(encoder_angle_counter);
    delay(200);
  }


    if(startStopMotorState == true) 
    {
      if(angleWorkinkg<=angleMinHexa)  // Direction Forwards if(startStopMotorState == true) 
      {
        if(directionState == false)
        {
        directionState = true;
        motorStop();
        digitalWrite(direction_pin, HIGH);
        if(firstRun == false) delay(3000);
        motorMove();              
        }
      } 
      if(angleWorkinkg>=angleMaxHexa)  // Direction Backwars
      {
        if(directionState == true)
        {
          directionState = false;
          motorStop();
          digitalWrite(direction_pin, LOW); 
          if(firstRun == false) delay(3000);
          motorMove();      
        }
      }
      if((angleWorkinkg < angleMaxHexa) && (angleWorkinkg > angleMinHexa)) motorMove();  
      if(getButtonStatus(angle_init_pin) == HIGH)
      {
        motorStop();
        startStopMotorState = false;
        lcd.clear();
        lcd.setCursor(0,lcd_first_row);
        lcd.print("Stopped");
        lcd.setCursor(0,lcd_second_row);
        lcd.print("From ");
        lcd.print(angleMin);
        lcd.write(byte(0));
        lcd.print(" To ");
        lcd.print(angleMax);
        lcd.write(byte(0));
      }
      firstRun = false;
    }

      

  // Start Stop MotorState
  if(getButtonStatus(button_start_stop) == HIGH){
  	startStopMotorState = !startStopMotorState;
  	if(startStopMotorState == false) {
  		motorStop();
  		lcd.clear();
   		lcd.setCursor(0,lcd_first_row);
  	    lcd.print("Stopped");
	    lcd.setCursor(0,lcd_second_row);
	    lcd.print("From ");
      lcd.print(angleMin);
	    lcd.write(byte(0));
	    lcd.print(" To ");
      lcd.print(angleMax);
	    lcd.write(byte(0));
      //firstRun = true;

  	}
  	else if(startStopMotorState == true) {
  	//	motorMove();
      //startStopMotorState = !startStopMotorState;
  		lcd.clear();
   		lcd.setCursor(0,lcd_first_row);
  	    lcd.print("Running   ");
      lcd.setCursor(0,lcd_second_row);
	    lcd.print("From ");
	    lcd.print(angleMin);
	    lcd.write(byte(0));
	    lcd.print(" To ");
	    lcd.print(angleMax);
	    lcd.write(byte(0));
      }
  }
  if((startStopMotorState == false)&&(getButtonStatus(button_menu)== HIGH)) deviceInit();

} // End Of LOOP

Tank you.
Evgeny.

What can mess up??

You, and the incorrect way you posted some of your code. Go read the stickies at the top of the forum - the ones you were supposed to read BEFORE you blundered in here.

This is the rest of the code
:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder_Polling_V2.h>

const int pin_A = 10                                             ;  // Encoder input pins
const int pin_B = 13;
int encoder_angle_counter = 0;
// _________ Variables ___________ //
// ------ Motor ------------
#define pwm_pin 11
#define motorStop() analogWrite(pwm_pin,0)
#define direction_pin 13
#define break_pin 8

int motorDutyCicle = 150; // (0-100)%
int motorSpeed = 1;
boolean startStopMotorState = false; // false - Stop, true - Start
boolean directionState = false;
boolean firstRun = true; // false - Backwards

// ------ LCD ---------------
#define delay_5s 5000
#define delay_2s 2000
#define delay_1s 1000
#define lcd_first_row 0
#define lcd_second_row 1
// LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
 //set the LCD address to 0x27 for a 16 chars  and 2 line display
byte degree[8] = {
  B01000,
  B10100,
  B01000,
  B00000,
  B00000,
  B00000,
  B00000,
};

// ------ Angle ------------
#define resistor_pin A3
#define angle_init_pin 6
#define lcdRowClear() lcd.print("                ");
int angleMin=165;
int angleMax=80;
int angleInit=0;
int angleWorkinkg=0;
int angleMinHexa=10;
int angleMaxHexa=90;

//------- Menu -------------
#define button_plus 5
#define button_minus 4
#define button_menu 2
#define button_start_stop 1
boolean menuButtonState      = false;

// _________ Functions ___________ // 
void setup()
{
  //Serial.begin(9600);
  //encoder_begin();  // Start the library
  //attach_encoder(0, pin_A, pin_B);  // Attach an encoder to pins A and B
  //Serial.print("Hello");
// ----- Motor -----
  pinMode(pwm_pin, OUTPUT);
  pinMode(direction_pin, OUTPUT);
  //pinMode(break_pin, OUTPUT);
  digitalWrite(break_pin, LOW);
  digitalWrite(direction_pin, HIGH);
  motorStop(); // prevent from moving with the default value

// ----- Angle -----
  pinMode(resistor_pin, INPUT);
  pinMode(angle_init_pin, INPUT);

// ----- LCD -------
  lcd.begin(16,2);
  lcd.createChar(0, degree);
  lcd.print("Welcome"); // For testing it may be disabled
  lcd.noCursor();
  //lcd.autoscroll();
  delay(delay_5s);
  lcd.clear();
  lcd.home();
  
  pinMode(button_menu, INPUT);
  pinMode(button_plus, INPUT);
  pinMode(button_minus, INPUT);
  pinMode(button_start_stop, INPUT);
  
// ------ Init ------
  getInitAngle();  
  deviceInit();
  
}

void loop()
{
.
.
.
}

void motorMove()
{
 //if(getButtonStatus(button_start_stop) == LOW ) analogWrite(pwm_pin, motorDutyCicle);
 //else motorStop();

 motorDutyCicle = motorSpeed *20+100;
 analogWrite(pwm_pin, motorDutyCicle);
}

void motorCalc(int dc)
{
  dc = constrain(dc, 10, 90);
  motorDutyCicle = map(dc, 10, 90, 0, 255); 
}


/*
  Function: initialize the angle and correct error mesuring
*/
int getInitAngle()
{
  // Need to define the reverse direction of the motor
  //int angleInit;
 
  lcd.clear();
  lcd.setCursor(0,lcd_first_row);
  lcd.print("Initializing...");
  
  digitalWrite(direction_pin, LOW);
  analogWrite(pwm_pin, motorDutyCicle);
  while(digitalRead(angle_init_pin) != HIGH);
  motorStop();
  //digitalWrite(break_pin, HIGH);
  
  lcd.clear();
  lcd.setCursor(0,lcd_first_row);
  lcd.print("ok");
  
  angleInit = analogRead(resistor_pin); // perform callculation to determine the angle(not voltage)
  lcd.setCursor(0,lcd_second_row);
  lcd.print(angleInit);
  delay(delay_2s);
  
encoder_angle_counter = 76;

Serial.print("Init..... DONE");
 return angleInit;
}

/*
 function: initialize the preference of the device
           min/max angle, speed
*/
void deviceInit()
{
   String initMesage[] = {"Start angle:","End angle:","Speed:"};
   int initValues[3]; // {Start,End,Speed}
   boolean buttonPressed = false;
   
   initValues[0] = angleMin;
   initValues[1] = angleMax;
   initValues[2] = motorSpeed;
         
   for(int i=0; i<3; i++)
   {
     lcd.clear();
     lcd.setCursor(0,lcd_first_row);
     lcd.print(initMesage[i]);
     lcd.setCursor(0,lcd_second_row);
     lcd.print(initValues[i]);
     while(true)
     {
       if(digitalRead(button_plus) == HIGH)
       { 
         initValues[i] = i<2 ? initValues[i]+5 : initValues[i]+1;
         buttonPressed = true;
       }
       else if(digitalRead(button_minus) == HIGH)
       {
         initValues[i] = i<2 ? initValues[i]-5 : initValues[i]-1 ;
         buttonPressed = true;
       }
       if(buttonPressed == true)
       {
         buttonPressed = false;
         if(i==0)      initValues[0] = constrain(initValues[0],135,165); // boundaries Start angle 
         else if(i==1) initValues[1] = constrain(initValues[1],80,initValues[0]-15); // boundaries End angle
         else if(i==2) initValues[2] = constrain(initValues[2],1,5); // boundaries of speed
         lcd.clear();
         lcd.setCursor(0,lcd_first_row);
         lcd.print(initMesage[i]);
         lcd.setCursor(0,lcd_second_row);
         lcd.print(initValues[i]);
         delay(400);
       }
       if(digitalRead(button_menu) == HIGH){
        delay(30);
        while(digitalRead(button_menu) == HIGH);
        delay(30);
        break;
       }
     }
   }
   
   // Initial angle in Degree
   angleMin = initValues[0]; 
   angleMax = initValues[1];
   motorSpeed = initValues[2];

   // Initial angle in Hexa
   angleMinHexa = (((angleMin/5)*4)+angleInit);
   angleMaxHexa = (((angleMax/5)*4)+angleInit);
   
   lcd.clear();
   lcd.setCursor(0,lcd_first_row);
   lcd.print("Setup complete.");
   delay(delay_1s);
   //lcd.clear();
   lcd.setCursor(0,lcd_second_row);
   lcd.print("  press START ");
  
}

boolean getButtonStatus(int buttonPin)
{
  int state;
  
  state = digitalRead(buttonPin);
  if(state == HIGH){
  	delay(30);
  	while(digitalRead(buttonPin) == HIGH);
  	delay(30);// prevent from missreading
  }
  
  return state;

Start by auto formatting the code, Always post all the code as a single block, I won't look at it othe wise. And most important of all scrap and rewrite the code it has delays in it. See blink wihout delay and look up finite state machines in the playground.

Mark

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

You can't use the Serial port pins for the LCD and for Serial, unless you WANT weird problems. If you do, don't whine when they happen.

Thank's for your help.

holmes4: Thank's for the "delay" advise, it was helpful :wink:
Can you explain please:

auto formatting the code, Always post all the code as a single block

PaulS:

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

You can't use the Serial port pins for the LCD and for Serial, unless you WANT weird problems. If you do, don't whine when they happen.

So it means that i cant use the LCD_I2C and print together? No matter what?

auto formatting the code, Always post all the code as a single block

On the IDE menu under tools is an auto formatting program. Always use it before posting.

Mark

zab_evgeny:
So it means that i cant use the LCD_I2C and print together? No matter what?

No. It just means don't use pins 0 and 1 for the LCD thingy (or for anything else, for that matter, apart from serial I/O).

...R

O.K. Tank you all.