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);
}
}