Here is all the code, but atm. its a bit of a mess, because I try different stuff out..
// Pin connections defined.
int ENA=3; //Connect on Arduino, Pin 3
int IN1=4; //Connect on Arduino, Pin 4
int IN2=5; //Connect on Arduino, Pin 5
int ENB=6; //Connect on Arduino, Pin 6
int IN3=7; //Connect on Arduino, Pin 7
int IN4=8; //Connect on Arduino, Pin 8
// The time the motor will be on when either raising or lowering the curtains (here 10 seconds = 10000 millis).
unsigned long TimeMotorOn = 10000;
long previousMillis = 0;
// A variable is created to hold the status of the curtains. true = curtains lowered, false = curtains raised.
curtainStatus = false;
void setup() {
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
// Activate motor A and B
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
}
void loop(){
/*
The following is just notes for myself... Jump to HERE WE GO AGAIN..
// Når motorA skal tændes sættes IN1 til HIGH og IN2 til LOW og motoren drejer en vej rundt.
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
//Når motorA skal dreje den anden vej rundt sættes IN1 til LOW og IN2 til HIGH.
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
//Når motorA skal slukkes sættes enten både IN1 og IN2 til LOW eller HIGH eller også slukker man ved at sætte ENA til LOW.
// Det samme gælder motorB som bare bruger IN3 og IN4.
//PWM, såfremt at motorerne har brug for at blive sænket i hastighed, så skal ENA og ENB sættes til pins på arduino boarded der understøtter PWM. PWM pins 3,5,6,9,10,11.
// Følgende kode kan bruges til at sænke hastigheden på motorerne:
analogWrite(ENA, (4/255)); //værdi mellem 0 og 255. Læg ud med at prøve med 1/4 hastighed
*/
// HERE WE GO AGAIN:
// Here we start controlling the curtains behavior:
// If there is to much light and the temperature is 25 or higher and the curtains are not down, the following code will be executed..
if(lightValue >= 300 && indetermometer >= 25 && curtainStatus == false) {
// Set PWM speed - MAYBE I CAN MOVE THIS BIT TO THE TOP WHERE I DEFINE THE DIFFERENT VARIABLES?
analogWrite(ENA, (4/255)); // 1/4 motor speed to begin with. Maybe it can go faster, but lets go slow..
// Lower curtains
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
// HOW DO I DO THIS PART...... I can make the reference point, but the code will be running through and changing that currentMillis variable so I never get a triggering...
// Make a time reference point when the motor started lowering the curtains
unsigned long currentMillis = millis(); //f.eks. 60000
if(currentMillis - previousMillis > TimeMotorOn) { // 60000-0>10000 = true
previousMillis = currentMillis;
//65000-60000 > 10000 = false
// Stop lowering the curtains
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
if(curtainStatus == false) {
curtainStatus = true;}
else {
curtainStatus = false;
};
// If there is not much light and the temperature is below 25 and the curtains are down, the following code will be executed..
if(lightValue < 300 && indetermometer < 25 && curtainStatus == true) {
// Set PWM speed
analogWrite(ENA, (4/255));
// Raise curtains
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
// Make a time reference point when the motor started raising the curtains
unsigned long currentMillis = millis();
if(currentMillis >= currentMillis + TimeMotorOn) {
// Stop curtain motor
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
if(curtainStatus == true) {
curtainStatus = false;}
else {
curtainStatus = true;
};