Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is for arduino pro mini. if you use other arduino, check pwm pin numbers
- //Temperature sensor DS18B20. I put it inside greenhouse shady place.
- // Motor driver is LN-298N
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define win1MotorA 4
- #define win1MotorB 5
- #define win2MotorA 6
- #define win2MotorB 7
- #define inverter 8
- // Data wire is plugged into pin 3 on the Arduino
- #define ONE_WIRE_BUS 3
- // Setup a oneWire instance to communicate with any OneWire devices
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // Assign the addresses of your 1-Wire temp sensors.
- // See the tutorial on how to obtain these addresses:
- // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
- DeviceAddress thermometer = { 0x28, 0xC8, 0xDC, 0x51, 0x07, 0x00, 0x00, 0x72 };
- int X = 0; //for win 1
- int Y = 0; //for win 2
- int Z = 0; // for door
- int i = 0; // for....dog
- const int winAopenPin = 9;
- const int winAclosePin = 10;
- const int winBopenPin = 11;
- const int winBclosePin = 12;
- // variables will change:
- int win1openState = 0;
- int win1closeState = 0;
- int win2openState = 0;
- int win2closeState = 0;
- void setup(void)
- {
- pinMode (win1MotorA, OUTPUT);
- pinMode (win1MotorB, OUTPUT);
- pinMode (win2MotorA, OUTPUT);
- pinMode (win2MotorB, OUTPUT);
- pinMode (inverter, OUTPUT);
- pinMode (winAopenPin, INPUT);
- pinMode (winAclosePin, INPUT);
- pinMode (winBopenPin, INPUT);
- pinMode (winBclosePin, INPUT);
- digitalWrite (win1MotorA, LOW);
- digitalWrite (win1MotorB, LOW);
- digitalWrite (win2MotorA, LOW);
- digitalWrite (win2MotorB, LOW);
- digitalWrite (inverter, LOW);
- // start serial port
- Serial.begin(9600);
- // Start up the library
- sensors.begin();
- // set the resolution to 10 bit (good enough?)
- sensors.setResolution(thermometer, 10);
- }
- void printTemperature(DeviceAddress deviceAddress)
- {
- float tempC = sensors.getTempC(deviceAddress);
- if (tempC == -40.00) {
- Serial.print("Error getting temperature");
- } else {
- Serial.print("C: ");
- Serial.print(tempC);
- delay(1000);
- }
- //Window 1
- if (tempC >= 15 && X == 0) { // if temp => +15 1. window opens
- Serial.println("Temp on +15. Opening win 1.");
- digitalWrite(inverter, HIGH);
- digitalWrite(win1MotorA, HIGH); // motor direction if need other direction change
- digitalWrite(win1MotorB, LOW); // motor direction if need other direction change
- delay(66000); // time for motor
- digitalWrite(win1MotorA, LOW);
- digitalWrite(inverter, LOW);
- Serial.println("Win 1 is open. ");
- delay(1000);
- X = 1;
- }
- if (tempC <= 14 && X == 1) { // if temp <= 14 1. window closes
- X = 0;
- Serial.println("Temp on +14. Closing win 1.");
- digitalWrite(inverter, HIGH);
- digitalWrite(win1MotorA, LOW);
- digitalWrite(win1MotorB, HIGH);
- delay(65000); // time for motor full speed
- digitalWrite(win1MotorB, LOW);
- digitalWrite(inverter, LOW);
- Serial.println("Win 1 is closed.");
- delay(1000);
- }
- //window 2
- if (tempC >= 20 && Y == 0) { // if temp => +20 2. window opens
- Serial.println("Temp on +20. Opening win 2.");
- digitalWrite(inverter, HIGH);
- digitalWrite(win2MotorA, HIGH); // motor direction if need other direction change
- digitalWrite(win2MotorB, LOW); // motor direction if need other direction change
- delay(66000); // time for motor full speed
- digitalWrite(win2MotorA, LOW);
- digitalWrite(inverter, LOW);
- Serial.println("Win 2 is open.");
- delay(1000);
- Y = 1;
- }
- if (tempC <= 19 && Y == 1) { // if temp <= 21 2. window closes
- Y = 0;
- Serial.print("Temp on +19. Closing win 2.");
- digitalWrite(inverter, HIGH);
- digitalWrite(win2MotorA, LOW); // motor direction if need other direction change
- digitalWrite(win2MotorB, HIGH); // motor direction if need other direction change
- delay(65000); // time for motor full speed
- digitalWrite(win2MotorB, LOW);
- digitalWrite(inverter, LOW);
- Serial.println("Window 2 closed. ");
- delay(1000);
- }
- }
- void loop(void)
- {
- Serial.print("Getting temperatures...\n\r");
- sensors.requestTemperatures();
- delay(1000);
- Serial.print("Temperature is: ");
- printTemperature(thermometer);
- Serial.println("\n\r");
- delay(500);
- }
- {
- // read the state of the pushbutton value:
- win1openState = digitalRead(win1openPin);
- // check if the pushbutton is pressed.
- // if it is, the winopen or closeState is HIGH:
- if (win1openState == HIGH) {
- //open win1
- digitalWrite(inverter, HIGH);
- digitalWrite(win1MotorA, HIGH);
- } else {
- //do nothing
- digitalWrite(inverter, LOW);
- digitalWrite(win1MotorA, LOW);
- }
- win1closeState = digitalRead(win1closePin);
- if (win1closeState == HIGH) {
- //close win1
- digitalWrite(inverter, HIGH);
- digitalWrite(win1MotorB, HIGH);
- } else {
- //do nothing
- digitalWrite(inverter, LOW);
- digitalWrite(win1MotorB, LOW);
- }
- win2openState = digitalRead(win2openPin);
- if (win2openState == HIGH) {
- //open win2
- digitalWrite(inverter, HIGH);
- digitalWrite(win2MotorA, HIGH);
- } else {
- //do nothing
- digitalWrite(inverter, LOW);
- digitalWrite(win2MotorA, LOW);
- }
- win2closeState = digitalRead(win2closePin);
- if (win1closeState == HIGH) {
- //close win2
- digitalWrite(inverter, HIGH);
- digitalWrite(win2MotorB, HIGH);
- } else {
- //do nothing
- digitalWrite(inverter, LOW);
- digitalWrite(win2MotorB, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement