Hello all.
Let me try again ![]()
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.