need help with my code plz (motor and heater) project

please tell me if this code needs any modifications , the project is a variable speed motor and on/off heater by a variable set temperature , i have 5 pushbuttons (PIN_SET ,PIN_DEC,PIN_INC ,startbutton,stopbutton)

PIN_SET > to choose to set variable to speed or temp
PIN_DEC > to decreament a variable (speed or temp)
PIN_INC >to increament a variable (speed or temp)
startbutton> to start the process (motor & heater) according to the already set values.
stopbutton > to stop the process

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display


#define PIN_SET 8
#define PIN_DEC 1
#define PIN_INC 0
int buttonState = 0;          // current state of the button
int lastButtonState  = 0;     // previous state of the button

#define motorpin  11
#define heaterpin  9

int startbutton = 6;
int stopbutton = 7;
unsigned long Temp = 0;
unsigned long Speed = 0;
int Sensorpin = A0;
unsigned long  tempsensor = 0 ;

void setup()
{
   pinMode(motorpin, OUTPUT);
   pinMode(heaterpin, OUTPUT);
   pinMode(startbutton, INPUT);
   pinMode(stopbutton, INPUT);
   pinMode(PIN_INC, INPUT);
   pinMode(PIN_DEC, INPUT);
   pinMode(PIN_SET, INPUT);
   Wire.begin();
   Wire.beginTransmission(0x27);
   lcd.begin(16, 2 );
   lcd.setCursor(0, 0);
   lcd.print(" Kemetch Sealer ");
   delay(3000);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Speed:");
   lcd.setCursor(10, 0);
   lcd.print("%");
   lcd.setCursor(0, 1);
   lcd.print("TEMP:");
   lcd.setCursor(9, 1);
   lcd.print("C");
}

void loop()
{
   buttonState = digitalRead(PIN_SET);   // read digital pin 2
   if (buttonState != lastButtonState)
   {
      //*****************************************************************//
      if (buttonState = HIGH)
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Speed < 10)
            {
               Speed ++;
               delay(100);
               lcd.setCursor(7, 0);
               lcd.print(Speed);
            }
            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Speed >= 0)
               {

                  Speed --;
                  lcd.setCursor(7, 0);
                  lcd.print(Speed);

                  delay(100);
               }
            }
         }
      }
      
      //*****************************************************************//
      if (buttonState == LOW) // THIS WAS POINTED OUT BY  jimLee AND NEEDS 
                             // FIXING (= FOR ASSIGNEMENT == FOR COMPARE).
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Temp < 400)
            {
               Temp ++;
               delay(100);
               lcd.setCursor(6, 1);
               lcd.print(Temp);
            }

            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Temp >= 0)
               {
                  Temp --;
                  delay(100);
                  lcd.setCursor(6, 1);
                  lcd.print(Temp);
               }
            }
         }
      }
   }
   //}   THIS EXTRA } CLOSES THE LOOP FUNNCTION.  EVERY THING 
   //    AFTER THIS IS OUTSIDE OF A FUNCTION AND NOT LEGAL

   tempsensor = analogRead(A0);
   unsigned long tempvalue = tempsensor * 0.48828125;

   if (Temp > tempvalue)
   {
      if ((digitalRead(startbutton) == HIGH) && (digitalRead( stopbutton) == LOW))
      {
         analogWrite(motorpin, (Speed * 25.5));
         digitalWrite(heaterpin, HIGH);
      }

      if ((digitalRead(startbutton) == LOW) && (digitalRead( stopbutton) == HIGH))
      {
         analogWrite(motorpin, LOW);
         digitalWrite(heaterpin, LOW);

      }
   }
}

This

    if (buttonState = HIGH)

is an assignment, not a comparison (==) so I doubt it is doing what you want.

Also, by using pin 0 and pin 1, you are eliminating the possibility of debugging your code with Serial.print() statements. I'd suggest using different pins and keeping those free.

You also don't specify how you are connected to the motor and heater. An arduino pin can not drive them directly because they can't supply enough current. Are you controlling a relay or something? A schematic would be helpful.

Hi,
Is this related to this thread?

How have you got your buttons wired?
Does it work?
What is it supposed to do?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hisham_Ramadan:
please tell me if this code needs any modifications

Why don't you start us off by telling us if the code compiles and uploads and what it does when you test it that's different from what you want it to do.

After all, if it already does what you want it to then it doesn't need any modification. And you'll be able to tell that much easier than we can.

Steve

i have modified the buttons pins but i cant see if it’s working or not as the lcd display not showing any thing . we can try this with eachother to find a solution . you can find the schematic in the attachments.

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display


#define PIN_SET 8
#define PIN_DEC 5
#define PIN_INC 4
int buttonState = 0;          // current state of the button
int lastButtonState  = 0;     // previous state of the button

#define motorpin  10
#define heaterpin 11

int startbutton = 6;
int stopbutton = 7;
unsigned long Temp = 0;
unsigned long Speed = 0;
int Sensorpin = A0;
unsigned long  tempsensor = 0 ;

void setup()
{
   pinMode(motorpin, OUTPUT);
   pinMode(heaterpin, OUTPUT);
   pinMode(startbutton, INPUT);
   pinMode(stopbutton, INPUT);
   pinMode(PIN_INC, INPUT);
   pinMode(PIN_DEC, INPUT);
   pinMode(PIN_SET, INPUT);
   Wire.begin();
   Wire.beginTransmission(0x27);
   lcd.begin(16, 2 );
   lcd.setCursor(0, 0);
   lcd.print(" Kemetch Sealer ");
   delay(3000);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Speed:");
   lcd.setCursor(10, 0);
   lcd.print("%");
   lcd.setCursor(0, 1);
   lcd.print("TEMP:");
   lcd.setCursor(9, 1);
   lcd.print("C");
}

void loop()
{
   buttonState = digitalRead(PIN_SET);   // read digital pin 2
   if (buttonState != lastButtonState)
   {
      //*****************************************************************//
      if (buttonState = HIGH)
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Speed < 10)
            {
               Speed ++;
               delay(100);
               lcd.setCursor(7, 0);
               lcd.print(Speed);
            }
            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Speed >= 0)
               {

                  Speed --;
                  lcd.setCursor(7, 0);
                  lcd.print(Speed);

                  delay(100);
               }
            }
         }
      }
      
      //*****************************************************************//
      if (buttonState == LOW) // THIS WAS POINTED OUT BY  jimLee AND NEEDS 
                             // FIXING (= FOR ASSIGNEMENT == FOR COMPARE).
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Temp < 400)
            {
               Temp ++;
               delay(100);
               lcd.setCursor(6, 1);
               lcd.print(Temp);
            }

            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Temp >= 0)
               {
                  Temp --;
                  delay(100);
                  lcd.setCursor(6, 1);
                  lcd.print(Temp);
               }
            }
         }
      }
   }
   //}   THIS EXTRA } CLOSES THE LOOP FUNNCTION.  EVERY THING 
   //    AFTER THIS IS OUTSIDE OF A FUNCTION AND NOT LEGAL

   tempsensor = analogRead(A0);
   unsigned long tempvalue = tempsensor * 0.48828125;

   if (Temp > tempvalue)
   {
      if ((digitalRead(startbutton) == HIGH) && (digitalRead( stopbutton) == LOW))
      {
         analogWrite(motorpin, (Speed * 25.5));
         digitalWrite(heaterpin, HIGH);
      }

      if ((digitalRead(startbutton) == LOW) && (digitalRead( stopbutton) == HIGH))
      {
         analogWrite(motorpin, LOW);
         digitalWrite(heaterpin, LOW);

      }
   }
}

schematic.pdf (53 KB)

TomGeorge:
Hi,
Is this related to this thread?

http://forum.arduino.cc/index.php?topic=586804.0

How have you got your buttons wired?
Does it work?
What is it supposed to do?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

yes it’s related to my later post but i have changed the lcd library as i have the same problem with out someone helping so i decided to post a new one hoping that to get this project working .

i have attached the schematic .

i have 5 pushbuttons (PIN_SET ,PIN_DEC,PIN_INC ,startbutton,stopbutton)

PIN_SET > to choose to set variable to speed or temp
PIN_DEC > to decreament a variable (speed or temp)
PIN_INC >to increament a variable (speed or temp)
startbutton> to start the process (motor & heater) according to the already set values.
stopbutton > to stop the process

i’ve tried the code with another lcd library but not worked well , as the lcd was not showing the variables (speed / temp ) values .

here is the modified code but nothing is shown by lcd so that i dont knw whether if its working or not .

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display


#define PIN_SET 8
#define PIN_DEC 5
#define PIN_INC 4
int buttonState = 0;          // current state of the button
int lastButtonState  = 0;     // previous state of the button

#define motorpin  10
#define heaterpin 11

int startbutton = 6;
int stopbutton = 7;
unsigned long Temp = 0;
unsigned long Speed = 0;
int Sensorpin = A0;
unsigned long  tempsensor = 0 ;

void setup()
{
   pinMode(motorpin, OUTPUT);
   pinMode(heaterpin, OUTPUT);
   pinMode(startbutton, INPUT);
   pinMode(stopbutton, INPUT);
   pinMode(PIN_INC, INPUT);
   pinMode(PIN_DEC, INPUT);
   pinMode(PIN_SET, INPUT);
   Wire.begin();
   Wire.beginTransmission(0x27);
   lcd.begin(16, 2 );
   lcd.setCursor(0, 0);
   lcd.print(" Kemetch Sealer ");
   delay(3000);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Speed:");
   lcd.setCursor(10, 0);
   lcd.print("%");
   lcd.setCursor(0, 1);
   lcd.print("TEMP:");
   lcd.setCursor(9, 1);
   lcd.print("C");
}

void loop()
{
   buttonState = digitalRead(PIN_SET);   // read digital pin 2
   if (buttonState != lastButtonState)
   {
      //*****************************************************************//
      if (buttonState = HIGH)
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Speed < 10)
            {
               Speed ++;
               delay(100);
               lcd.setCursor(7, 0);
               lcd.print(Speed);
            }
            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Speed >= 0)
               {

                  Speed --;
                  lcd.setCursor(7, 0);
                  lcd.print(Speed);

                  delay(100);
               }
            }
         }
      }
      
      //*****************************************************************//
      if (buttonState == LOW) // THIS WAS POINTED OUT BY  jimLee AND NEEDS 
                             // FIXING (= FOR ASSIGNEMENT == FOR COMPARE).
      {
         if (PIN_INC == HIGH && PIN_DEC == LOW)
         {
            if (Temp < 400)
            {
               Temp ++;
               delay(100);
               lcd.setCursor(6, 1);
               lcd.print(Temp);
            }

            if (PIN_INC == LOW && PIN_DEC == HIGH)
            {

               if (Temp >= 0)
               {
                  Temp --;
                  delay(100);
                  lcd.setCursor(6, 1);
                  lcd.print(Temp);
               }
            }
         }
      }
   }
   //}   THIS EXTRA } CLOSES THE LOOP FUNNCTION.  EVERY THING 
   //    AFTER THIS IS OUTSIDE OF A FUNCTION AND NOT LEGAL

   tempsensor = analogRead(A0);
   unsigned long tempvalue = tempsensor * 0.48828125;

   if (Temp > tempvalue)
   {
      if ((digitalRead(startbutton) == HIGH) && (digitalRead( stopbutton) == LOW))
      {
         analogWrite(motorpin, (Speed * 25.5));
         digitalWrite(heaterpin, HIGH);
      }

      if ((digitalRead(startbutton) == LOW) && (digitalRead( stopbutton) == HIGH))
      {
         analogWrite(motorpin, LOW);
         digitalWrite(heaterpin, LOW);

      }
   }
}

schematic.pdf (53 KB)

You didn’t correct the error I pointed out and you introduced another one by only including part of the lcd example code where you test to see if it is present or not

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display


const int PIN_SET = 8;
const int PIN_DEC = 5;
const int PIN_INC  = 4;
int buttonState = 0;          // current state of the button
int lastButtonState  = 0;     // previous state of the button

const int motorpin = 10;
const int heaterpin = 11;

const int startbutton = 6;
const int stopbutton = 7;
unsigned long Temp = 0;
unsigned long Speed = 0;
const int Sensorpin = A0;
unsigned long  tempsensor = 0 ;

void setup()
{
  Serial.begin(9600);
  while( !Serial );
  pinMode(motorpin, OUTPUT);
  digitalWrite(motorpin, LOW );
  pinMode(heaterpin, OUTPUT);
  digitalWrite(heaterpin, LOW );
  pinMode(startbutton, INPUT);
  pinMode(stopbutton, INPUT);
  pinMode(PIN_INC, INPUT);
  pinMode(PIN_DEC, INPUT);
  pinMode(PIN_SET, INPUT);

  Wire.begin();
  Wire.beginTransmission(0x27);
  int error = Wire.endTransmission();
  if (error == 0) {
    Serial.println(": LCD found.");
  } else {
    Serial.println(": LCD not found.");
    while(1); // loop forever
  }

  lcd.begin(16, 2 );
  lcd.setCursor(0, 0);
  lcd.print(" Kemetch Sealer ");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Speed:");
  lcd.setCursor(10, 0);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("TEMP:");
  lcd.setCursor(9, 1);
  lcd.print("C");
}

void loop()
{
  buttonState = digitalRead(PIN_SET);   // read digital pin 2
  if (buttonState != lastButtonState)
  {
    //*****************************************************************//
    if (buttonState == HIGH)
    {
      if (PIN_INC == HIGH && PIN_DEC == LOW)
      {
        if (Speed < 10)
        {
          Speed++;
          delay(100);
          lcd.setCursor(7, 0);
          lcd.print(Speed);
        }
        if (PIN_INC == LOW && PIN_DEC == HIGH)
        {
          if (Speed >= 0)
          {
            Speed--;
            lcd.setCursor(7, 0);
            lcd.print(Speed);
            delay(100);
          }
        }
      }
    }

    //*****************************************************************//
    if (buttonState == LOW)
    {
      if (PIN_INC == HIGH && PIN_DEC == LOW)
      {
        if (Temp < 400)
        {
          Temp++;
          delay(100);
          lcd.setCursor(6, 1);
          lcd.print(Temp);
        }
        if (PIN_INC == LOW && PIN_DEC == HIGH)
        {
          if (Temp >= 0)
          {
            Temp--;
            delay(100);
            lcd.setCursor(6, 1);
            lcd.print(Temp);
          }
        }
      }
    }
  }
  tempsensor = analogRead(Sensorpin);
  unsigned long tempvalue = tempsensor * 0.48828125;

  if (Temp > tempvalue)
  {
    if ((digitalRead(startbutton) == HIGH) && (digitalRead( stopbutton) == LOW))
    {
      analogWrite(motorpin, (Speed * 25.5));
      digitalWrite(heaterpin, HIGH);
    }
    if ((digitalRead(startbutton) == LOW) && (digitalRead( stopbutton) == HIGH))
    {
      analogWrite(motorpin, LOW);
      digitalWrite(heaterpin, LOW);
    }
  }
}

blh64:
You didn’t correct the error I pointed out and you introduced another one by only including part of the lcd example code where you test to see if it is present or not

#include <Wire.h>

#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display

const int PIN_SET = 8;
const int PIN_DEC = 5;
const int PIN_INC  = 4;
int buttonState = 0;          // current state of the button
int lastButtonState  = 0;    // previous state of the button

const int motorpin = 10;
const int heaterpin = 11;

const int startbutton = 6;
const int stopbutton = 7;
unsigned long Temp = 0;
unsigned long Speed = 0;
const int Sensorpin = A0;
unsigned long  tempsensor = 0 ;

void setup()
{
  Serial.begin(9600);
  while( !Serial );
  pinMode(motorpin, OUTPUT);
  digitalWrite(motorpin, LOW );
  pinMode(heaterpin, OUTPUT);
  digitalWrite(heaterpin, LOW );
  pinMode(startbutton, INPUT);
  pinMode(stopbutton, INPUT);
  pinMode(PIN_INC, INPUT);
  pinMode(PIN_DEC, INPUT);
  pinMode(PIN_SET, INPUT);

Wire.begin();
  Wire.beginTransmission(0x27);
  int error = Wire.endTransmission();
  if (error == 0) {
    Serial.println(": LCD found.");
  } else {
    Serial.println(": LCD not found.");
    while(1); // loop forever
  }

lcd.begin(16, 2 );
  lcd.setCursor(0, 0);
  lcd.print(" Kemetch Sealer “);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(“Speed:”);
  lcd.setCursor(10, 0);
  lcd.print(”%");
  lcd.setCursor(0, 1);
  lcd.print(“TEMP:”);
  lcd.setCursor(9, 1);
  lcd.print(“C”);
}

void loop()
{
  buttonState = digitalRead(PIN_SET);  // read digital pin 2
  if (buttonState != lastButtonState)
  {
    //*****************************************************************//
    if (buttonState == HIGH)
    {
      if (PIN_INC == HIGH && PIN_DEC == LOW)
      {
        if (Speed < 10)
        {
          Speed++;
          delay(100);
          lcd.setCursor(7, 0);
          lcd.print(Speed);
        }
        if (PIN_INC == LOW && PIN_DEC == HIGH)
        {
          if (Speed >= 0)
          {
            Speed–;
            lcd.setCursor(7, 0);
            lcd.print(Speed);
            delay(100);
          }
        }
      }
    }

//*****************************************************************//
    if (buttonState == LOW)
    {
      if (PIN_INC == HIGH && PIN_DEC == LOW)
      {
        if (Temp < 400)
        {
          Temp++;
          delay(100);
          lcd.setCursor(6, 1);
          lcd.print(Temp);
        }
        if (PIN_INC == LOW && PIN_DEC == HIGH)
        {
          if (Temp >= 0)
          {
            Temp–;
            delay(100);
            lcd.setCursor(6, 1);
            lcd.print(Temp);
          }
        }
      }
    }
  }
  tempsensor = analogRead(Sensorpin);
  unsigned long tempvalue = tempsensor * 0.48828125;

if (Temp > tempvalue)
  {
    if ((digitalRead(startbutton) == HIGH) && (digitalRead( stopbutton) == LOW))
    {
      analogWrite(motorpin, (Speed * 25.5));
      digitalWrite(heaterpin, HIGH);
    }
    if ((digitalRead(startbutton) == LOW) && (digitalRead( stopbutton) == HIGH))
    {
      analogWrite(motorpin, LOW);
      digitalWrite(heaterpin, LOW);
    }
  }
}

which error u pointed out at ?
you mentioned the (==HIGH) in the if statement and i 've changed it .also i changed the 0,1 pins connecting it’s pushbuttons to another pins illustrated in the code .

what else should i do to see this project working?

if (PIN_INC == HIGHPIN_INC has the value 4.
HIGH has the value 1.

See the problem?

Hisham_Ramadan:
which error u pointed out at ?
you mentioned the (==HIGH) in the if statement and i 've changed it

No, you didn't. The followup code you posted was unchanged

what else should i do to see this project working?

Did you try the code I posted? I pointed out why the lcd was probably not working and added some code to possibly make it work but the real test is to try the code. Compile it, upload it, try to figure out any errors and then ask for help.

You have the relay coils on the wrong side of the transistors and should have freewheeling diodes across relay coils AND motor. Keep your base resistors at 220 Ω
relayTrans.png

i gonna modify the schematic according to ur guiding pic but the real problem now is that the lcd in proteus is not working sothat i dont know the project works well or not . any guide please .
i wanna connect the lcd as i2c lcd in proteus before i assemble it in real .

Hisham_Ramadan:
i gonna modify the schematic according to ur guiding pic but the real problem now is that the lcd in proteus is not working sothat i dont know the project works well or not . any guide please .
i wanna connect the lcd as i2c lcd in proteus before i assemble it in real .

So you are running all this on a simulator, not hardware?

Can I suggest you start building, in stages and coding in stages in the REAL world.

First write some code, using the library examples, to get your LCD working, in the REAL world.

Then write some code just to read your buttons, and construct the hardware in the REAL world just for the buttons, get them working.

Then write some code just for the sensor, and get them on the LCD.

Then write some code to just operate the relay.

When you have each of these stages working, then combine them, one at a time and get them working before adding the next.

Do not become fixated with a simulator, get into the REAL world, get your tools out and build.

By building in stages and getting those stages to work as you go, you will learn more about your project and how to fix any problems.

Trying to get ALL the program problems completely solved in a simulator, before lifting a component and constructing the project is a very long and confusing path.

Sorry, but get building, in stages.
Tom.... :slight_smile: