serial.available + button = not work

Hi All!

I use serial.available + Fan button, but not working, until does not start lcdproc software in PC.
So: lcdproc+fan=WORK but do not run lcdproc software the Fan NOT work!

Where do I find the bug?

Thank you!

// Fan
int buttonPinH = 5;    
int Fan =  6;
int counter = 0;
int stateButton = 0;
int buttonState = 0;
 
// LCD controller
#ifndef HD44780_LCDOBJECT
// hd44780 with hd44780_I2Cexp i/o class
#include <Wire.h>
#include <hd44780.h> // include hd44780 library header file
#include <hd44780ioClass/hd44780_I2Cexp.h> // i/o expander/backpack class
hd44780_I2Cexp lcd(0x20, I2Cexp_PCF8574, 4,5,6,0,1,2,3,7,LOW); // Electrofun & PNP transistor for BL
#define WIRECLOCK 400000
#endif
/*
 * Define your LCD  parameters
 * These must match what you configured in LCDd.conf on the linux host
 */
#ifndef BAUDRATE
#define BAUDRATE 115200
#endif
 
#ifndef LCD_COLS
#define LCD_COLS 16
#endif
 
#ifndef LCD_ROWS
#define LCD_ROWS 2
#endif
 
void setup(){
  SetupFan();
  SetupLcdController();
}
void SetupFan(){
  //Input or output?
  pinMode(Fan, OUTPUT);      
  pinMode(buttonPinH, INPUT);
  analogWrite(Fan, 80);
}
 
void SetupLcdController()
{
  Serial.begin(BAUDRATE);
  // set up the LCD's number of columns and columns:
  lcd.begin(LCD_COLS, LCD_ROWS);
  #ifdef WIRECLOCK
  #if (ARDUINO > 10507) && !defined(MPIDE)
  #endif
  #endif
  Wire.setClock(WIRECLOCK); // set i2c clock bit rate, if asked
 
  // ------- Quick 3 blinks of backlight  -------------
  for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on
 
  // print out a banner that indicates lcd proc device
  // and pramameters like baudrate and geometry
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("    Welcome!");
  lcd.setCursor(0,1);
  lcd.print("Multi Controller");
  delay(1000);
  }
 
uint8_t serial_getch()
{
int incoming;  
 
  while (!Serial.available()){ }
  // read the incoming byte:
  incoming = Serial.read();
 
  return (incoming & 0xff);
}
 
 
void loop(){
  FanController();
  LcdController();
}
 
void FanController(){
buttonState = digitalRead(buttonPinH);
if (buttonState == LOW)
{
  counter++;
  delay(100);
}
if(counter == 0)
{
 analogWrite(Fan, 80);
}
 
  else if(counter == 1)
{
  digitalWrite(Fan,HIGH);
}
else   {
  counter =0;
}
}
 
void LcdController(){
uint8_t rxbyte;
 
  rxbyte = serial_getch(); // Fetch byte
   
  if(rxbyte==0xFE) // If command
    lcd.command(serial_getch()); // Pass through raw hd44780 command
   
  else
    lcd.write(rxbyte); //Otherwise send it as text character
}

How IS your switch wired?

What is sending serial data to the Arduino?

I use this thematics: hd44780/lcdproc.ino at master · duinoWitchery/hd44780 · GitHub

drcyberg:
I use this thematics: hd44780/examples/hd44780examples/lcdproc/lcdproc.ino at master · duinoWitchery/hd44780 · GitHub

That answered neither of my questions.

(deleted)

Like spycatcher2k said, that line waits for serial input which won't happen until you run the software on the PC.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thank you for all! The probleme is solved(momentarily)! :smiley:
Here is (beta)code:

void LcdController(){
 if ( Serial.available())
{

uint8_t rxbyte;
// read the incoming byte:
rxbyte = Serial.read();  
rxbyte &= 0xff; // ??? no sense
if (rxbyte == 0xFE) // If command 
lcd.command(serial_getch()); // Pass through raw hd44780 command

else
lcd.write(rxbyte); //Otherwise send it as text character
}
}