Motor control based on temperature variable

hi,
I’m trying to set my motor so that if the temperature is less than or equal target_temperature then the motor will not move.
if the temperature is above the target_temperature then the motor moves 45 degrees with the motor control function.
finally i want my motor to close 45 degrees if the window had been opened on the previous loop and the temperature has dropped below the target_temperature.

when the system loops back to the begining and the window had been opened then it just states that “no adjustment is required”

any help would be much appreciated,
thanks.

#include “mbed.h”
#include “TextLCD.h”

BusOut Steppermotor(p11, p12, p13, p14); // (IN1) - (IN2) - (IN3) - (IN4)
AnalogIn LM35(p15); // Temperature Sensor
DigitalOut GreenLED(p7); //
DigitalOut RedLED(p5);
DigitalOut OrangeLED(p6);
InterruptIn Button(p25); // Temperature check
InterruptIn Button2(p24); // Motor Control - Window open
InterruptIn Button3(p23); // Motor control - Window close
Ticker flipper; // Ticker used to open window
// when manual over ride isn’t initiated

TextLCD lcd(p21, p22, p27, p28, p29, p30, TextLCD::LCD16x2); // LCD Display Wiring

// variable declartations

float tempC; // Angle in degrees and temperature in celcius
float target_temp = 20;
float target_temp1 = 24; // Target temperature
float angle;

int step = 0; // variable integer values for the system.
int dir=1;
int i=0;
void Auto_Window_Control(float, int direction);

void Auto_Window_Control(float, int direction)
{
float single_step_angle = 0.18; // 360 degrees / 2048 total steps = 0.18 degrees per step
float total_steps=250;
int i=0;
int position=0;

total_steps = angle/single_step_angle;

while (i<=total_steps) { //assuming rounding down to nearest step due to float->int comparison
    switch(position)
    {
        case 0: Steppermotor = 0x1; break;  //0001
        case 1: Steppermotor = 0x2; break;  //0010
        case 2: Steppermotor = 0x4; break;  //0100
        case 3: Steppermotor = 0x8; break;  //1000
        
        default: Steppermotor = 0x0; break; //0000
        }
        
        if(direction==1) position++;    else position--;
        if(position>3)position=0;
        if(position<0)position=3;               //
        
        
        wait(0.005);    //speed
        i++;
    }

}

void Temp_check() { // Temperature check interrupt
tempC=LM35.read();
tempC=(tempC3.3100); //LM35 Thermistor

    // Display temperature
    
    lcd.locate(0,0);
    lcd.printf("cT      sT");
    lcd.locate(0,1);
    
    // Display current and target temperature
     
    lcd.printf("%.1f    %.1f",tempC,target_temp);
    wait(5);
    lcd.cls();
    }

void Manual_Override(){ // System over ride control

    i=56;                                                  
                                    
    if(i>56) {GreenLED=0; RedLED=0;} 
                                    
    if(Button3==1)  {i=0;dir=0;}   // These are the variable inputs that will
    if(Button2==1)  {i=0;dir=1;}   // determine the movement of the motor
    
    while (i<=56)   {
        switch(step)
        {
            case 0: Steppermotor = 0x1; break; //0001 
            case 1: Steppermotor = 0x2; break; //0010
            case 2: Steppermotor = 0x4; break; //0100
            case 3: Steppermotor = 0x8; break; //1000
            
            
            default: Steppermotor = 0x0; break; //0000
        }
            
        if(dir==1) {step++;} else {step--;}
        if(step>3) {step=0;}
        if(step<0) {step=3;}
            
        if(dir==1) {GreenLED=1; RedLED=0;} else {GreenLED=0; RedLED=1;}
            
        wait(0.005);    //speedooo
        i++;
        }
    }

int main(){

Button.rise(&Temp_check); // System interrupts
Button2.rise(&Manual_Override);
Button3.rise(&Manual_Override);

// welcome message

lcd.cls(); 
wait(2);

GreenLED = 1;
wait(0.5);
GreenLED = 0;
wait(0.5);
GreenLED = 1;
wait(0.5);
GreenLED = 0;
wait(0.5);
GreenLED = 1;
wait(0.5);
GreenLED = 0;
wait(3);

lcd.printf("AD Automated\nControl Systems\n");
wait(3);
lcd.cls();
wait(3);
lcd.cls();

while(1){ // Temperature readings
lcd.printf(“Current Readings\n”);
wait(3);
lcd.cls();
tempC=LM35.read();
tempC=(tempC3.3100); //LM35 Thermistor

        lcd.locate(0,0);
        lcd.printf("cT      sT");
        lcd.locate(0,1);
    
        lcd.printf("%.1f    %.1f",tempC,target_temp); 
        wait(5);
                
            if(tempC<=target_temp == true){                     // If temp lower than target temp
            
                        GreenLED = 1;
                        lcd.cls();
                        wait(1);
                        lcd.printf("No Adjustment\nRequired\n");
                        wait(3);
                        lcd.cls();
                        wait(5);
                        }
         
            if(tempC>target_temp == true){                // If temp higher than target temp
                        OrangeLED = 1;
                        lcd.cls();
                        wait(1);
                        lcd.printf("Window Opening");
                        wait(3);
                        lcd.cls();
                        wait(1);
                        Auto_Window_Control(angle = 45 ,1);
                        wait(3);
                        GreenLED = 0;
                        wait(5);
                        }
                        
                 else if (tempC==target_temp){          
                           lcd.cls();
                           lcd.printf("Window Closing");
                           OrangeLED = 1;
                           wait(2);
                           OrangeLED = 0;
                           Auto_Window_Control(angle = 45 ,0);
                           wait(3);
                           GreenLED = 1;
                           }
             
                  
            wait(10);
            
    }

}

Hello,

please be so kind and use these 3 ``` symbols before and after your code in your post above for correct code formatting for better view.

You have a target what you want to achieve, ok. You also posted a piece of code but an info about what you have already achieved is missing.
So, with what do you need help? What is not working?

BR, Jan

hi johnnyK,

so when my code runs, if the temperature is below target_temp then the motor will not move which is fine.
then when its above target_temp the motor moves or opens 45 degrees. if this is the case and the system loops again i dont want the motor to open again.

when the system loops and the temperature has become equal to or below the target_temp then i want the motor to close or reverse 45 degrees.

does this make sense?
thanks

#include "mbed.h"       
#include "TextLCD.h"


BusOut Steppermotor(p11, p12, p13, p14);    // (IN1) - (IN2) - (IN3) - (IN4) 
AnalogIn LM35(p15);                         // Temperature Sensor
DigitalOut GreenLED(p7);                    //
DigitalOut RedLED(p5);
DigitalOut OrangeLED(p6);
InterruptIn Button(p25);                    // Temperature check
InterruptIn Button2(p24);                   // Motor Control - Window open
InterruptIn Button3(p23);                   // Motor control - Window close
Ticker flipper;                             // Ticker used to open window
                                            // when manual over ride isn't initiated

TextLCD lcd(p21, p22,  p27, p28, p29, p30, TextLCD::LCD16x2); // LCD Display Wiring

// variable declartations

float tempC;                    // Angle in degrees and temperature in celcius
float target_temp = 20; 
float target_temp1 = 24;                    // Target temperature
float angle;

int step = 0;                           // variable  integer values for the system. 
int dir=1;                              
int i=0;  
void Auto_Window_Control(float, int direction);
               


void Auto_Window_Control(float, int direction)
{
    float single_step_angle = 0.18; // 360 degrees / 2048 total steps = 0.18 degrees per step
    float total_steps=250;
    int i=0;
    int position=0;
    
    total_steps = angle/single_step_angle;
    
    while (i<=total_steps) { //assuming rounding down to nearest step due to float->int comparison
        switch(position)
        {
            case 0: Steppermotor = 0x1; break;  //0001
            case 1: Steppermotor = 0x2; break;  //0010
            case 2: Steppermotor = 0x4; break;  //0100
            case 3: Steppermotor = 0x8; break;  //1000
            
            default: Steppermotor = 0x0; break; //0000
            }
            
            if(direction==1) position++;    else position--;
            if(position>3)position=0;
            if(position<0)position=3;               //
            
            
            wait(0.005);    //speed
            i++;
        }
}             

                                    
void Temp_check() {                       // Temperature check interrupt
      tempC=LM35.read();
      tempC=(tempC*3.3*100);              //LM35 Thermistor
        
        // Display temperature
        
        lcd.locate(0,0);
        lcd.printf("cT      sT");
        lcd.locate(0,1);
        
        // Display current and target temperature
         
        lcd.printf("%.1f    %.1f",tempC,target_temp);
        wait(5);
        lcd.cls();
        }
        

        
void Manual_Override(){                   // System over ride control
         
        i=56;                                                  
                                        
        if(i>56) {GreenLED=0; RedLED=0;} 
                                        
        if(Button3==1)  {i=0;dir=0;}   // These are the variable inputs that will
        if(Button2==1)  {i=0;dir=1;}   // determine the movement of the motor
        
        while (i<=56)   {
            switch(step)
            {
                case 0: Steppermotor = 0x1; break; //0001 
                case 1: Steppermotor = 0x2; break; //0010
                case 2: Steppermotor = 0x4; break; //0100
                case 3: Steppermotor = 0x8; break; //1000
                
                
                default: Steppermotor = 0x0; break; //0000
            }
                
            if(dir==1) {step++;} else {step--;}
            if(step>3) {step=0;}
            if(step<0) {step=3;}
                
            if(dir==1) {GreenLED=1; RedLED=0;} else {GreenLED=0; RedLED=1;}
                
            wait(0.005);    //speedooo
            i++;
            }
        }
    


int main(){ 
             
Button.rise(&Temp_check);                   // System interrupts
Button2.rise(&Manual_Override);
Button3.rise(&Manual_Override);

 // welcome message
    
    lcd.cls(); 
    wait(2);
    
    GreenLED = 1;
    wait(0.5);
    GreenLED = 0;
    wait(0.5);
    GreenLED = 1;
    wait(0.5);
    GreenLED = 0;
    wait(0.5);
    GreenLED = 1;
    wait(0.5);
    GreenLED = 0;
    wait(3);
    
    lcd.printf("AD Automated\nControl Systems\n");
    wait(3);
    lcd.cls();
    wait(3);
    lcd.cls();
  
   
 while(1){                                              // Temperature readings
            lcd.printf("Current Readings\n");
            wait(3);
            lcd.cls();
            tempC=LM35.read();
            tempC=(tempC*3.3*100);                          //LM35 Thermistor
        
            lcd.locate(0,0);
            lcd.printf("cT      sT");
            lcd.locate(0,1);
        
            lcd.printf("%.1f    %.1f",tempC,target_temp); 
            wait(5);
                    
                if(tempC<=target_temp){                     // If temp lower than target temp
                
                            GreenLED = 1;
                            lcd.cls();
                            wait(1);
                            lcd.printf("No Adjustment\nRequired\n");
                            wait(3);
                            lcd.cls();
                            wait(5);
                            }
             
                if(tempC>target_temp == true){                // If temp higher than target temp
                            OrangeLED = 1;
                            wait(2);
                            OrangeLED = 0;
                            lcd.cls();
                            wait(1);
                            lcd.printf("Window Opening");
                            wait(3);
                            lcd.cls();
                            wait(1);
                            Auto_Window_Control(angle = 45 ,1);
                            wait(3);
                            GreenLED = 0;
                            wait(5);
                            }
                            
                     else if (tempC==target_temp){          
                               lcd.cls();
                               lcd.printf("Window Closing");
                               OrangeLED = 1;
                               wait(2);
                               OrangeLED = 0;
                               Auto_Window_Control(angle = 45 ,0);
                               wait(3);
                               GreenLED = 1;
                               }
                 
                      
                wait(10);
                
        }
}



    

sorry about the incorrect format, ive replied with it corrected

Ok

 if(tempC > target_temp == true){ 

That tell me - If the temperature is higher than target temperature then open the window.
But you want open the window only when is the window closed, because already opened window can not be open again.
So you need another argument like a variable with state of your previous loop.

enum window {
   closed = 0,
   opened
};

int previousWindowState = closed;
while(1){
   if(tempC > target_temp && previousWindowState == closed){ 
     // code for openning
     previousWindowState = opened;
   }else if (tempC==target_temp && previousWindowState == opened){
     // code for closing
     previousWindowState = closed;
   }else{
     // do nothing
   }
}

I hope I understand correctly your issue and it will help.

BR, Jan

thanks for your reply,

can i ask where i put these in my code.

Both can be declared as global or before the while loop in main

int main(){
  enum window {
     closed = 0,
     opened
  };

  int previousWindowState = closed;
  while(1){
  //rest of code...

Of course you not need to use enum you can use standart bool (boolean - true/false) variable but enum can have more states bool only two.

BR, Jan

Johhnyk,

You have cracked the code. from what i can see this is working. Thank you very much.