const int input = 2; const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A) const int enablePin = 6; // H-bridge enable pin const int override = 5; const int pin1 = 1; char val; // variable to store the data from the serial port int p=0, r=0, q=0, t=0, s=0; void setup() { Serial.begin(9600); // connect to the serial port // set the switch as an input: pinMode(override, INPUT); pinMode(input, INPUT); // set all the other pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT); pinMode(pin1, OUTPUT); // set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH); }void loop () { // activate motor only when you receive data: if (Serial.available()) { val= Serial.read(); if (val == '1') { if (r==1) { //if r =1; set motor for forward for q seconds. and also set r back to 0 digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high delay(q); r=0; t=1; } if (t==0) { // this will set the motor to forward (clockwise direction) digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high delay(3000); } //stop motor for 1 second digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low delay(1000); p=1; unsigned long REDMillis = millis();//save the current millisecond when motor will be reverse digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low if ((digitalRead(override) ==HIGH) && (p=1)) { unsigned long ORMillis = millis();//save current millisecond when override switch was pressed q=ORMillis-REDMillis; //seconds that the motor operated on reverse //turn off motor digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low r=1; s=1; } if(s==0) { delay(3000);//the motor should be on reverse (counterclockwise) for 3 secs //motor stops digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } } // if there is no "1" in serial port, motor will turn off else { digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high } }}
// if there is no "1" in serial port, motor will turn off else { digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high }
const int input = 2;const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)const int enablePin = 6; // H-bridge enable pinconst int override = 5;const int pin1 = 1;boolean bl_CommandRecieved = FALSE; unsigned long DoorOpenTimer; unsigned long ElapsedDoorOpen; /*** rename nyo na lang into something more understandable ****/char val; // variable to store the data from the serial portint p=0, r=0, q=0, t=0, s=0;/**************************************************************/void setup(){ Serial.begin(9600); // connect to the serial port // set the switch as an input: pinMode(override, INPUT); pinMode(input, INPUT); // set all the other pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT); pinMode(pin1, OUTPUT); // set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH); }void loop(){ // activate motor only when you receive data: if (Serial.available()) { val= Serial.read(); // run the motors if (val == '1') { bl_CommandRecieved = true; DoorOpenTimer = millis(); //mark start of timer } // turn off the motors else { bl_CommandRecieved = false; } } if(bl_CommandRecieved && !digitalRead(override)) { ElapsedDoorOpen = (millis() - DoorOpenTimer); //reset everything back to initial position if(digitalRead(override)) { doorReset(); } else { doorOpen(); } } else { } }void doorReset(){ }void doorOpen(){ // Clockwise for 3Sec if(ElapsedDoorOpen < 3) // continue clockwise until 3sec has lapsed { motorsForward(); } // Stop for 1Sec else if(ElapsedDoorOpen < 4 ) // pause for 1sec, 3sec opening + 1sec pause { motorsStopped(); } // Counter Clockwise for 3Sec else if(ElapsedDoorOpen) < 7 ) // counter clockwise { motorsReversed(); } // clear command afte 7 seconds else { bl_CommandRecieved = false; } }//*** Low Level Motor Controls *************************************//void motorsClockwise()void motorsForward(){ digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high }//void motorsCntrClockwise()void motorsReversed(){ digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low }void motorsStopped(){ digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high }
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A) const int enablePin = 6; // H-bridge enable pin const int override = 5; int SerialValue; // variable to store the data from the serial port int DoorClosing=0, ElapsedClosing=0, NoPrevOR=0, ORset=0; long threesec = 3000; long previousMillis=0; long onesec=1000; unsigned long ClosingMillis = 0; unsigned long ORMillis = 0; unsigned long currentMillis = 0; void setup() { Serial.begin(9600); // connect to the serial port // set the switch as an input: pinMode(override, INPUT); // set all the other pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT); // set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH);}void loop () { // send data only when you receive data: if (Serial.available()) { SerialValue= Serial.read(); if (SerialValue == '1') { currentMillis = millis(); if (ORset==1)//if r =1; set motor for forward for ElapsedClosing seconds. and also set r back to 0 { while(currentMillis - previousMillis <= ElapsedClosing) { digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high currentMillis = millis(); } if(currentMillis - previousMillis > ElapsedClosing) { previousMillis = millis();// save the last time you blinked the LED digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } ORset=0; NoPrevOR=1; } previousMillis = millis(); if (NoPrevOR==0) { while(currentMillis - previousMillis <= threesec) { // this will set the motor to forward (clockwise direction) digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high currentMillis = millis(); } if(currentMillis - previousMillis > threesec) { previousMillis = millis();// save the last time you blinked the LED currentMillis = millis(); while(currentMillis - previousMillis <= onesec) { digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low currentMillis = millis(); } } } currentMillis = millis(); //stop motor for 1 second if(currentMillis - previousMillis > onesec) { previousMillis = millis();// save the last time you blinked the LED currentMillis = millis(); DoorClosing=1; while(currentMillis - previousMillis <= threesec) { ClosingMillis = millis();//save the current millisecond when motor will be reverse digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low currentMillis = millis(); } } if ((digitalRead(override) ==HIGH) && (DoorClosing=1)) { ORMillis = millis();//save current millisecond when override switch was pressed ElapsedClosing=ORMillis-ClosingMillis; //seconds motor operated on reverse /*save number of seconds red operated=q. get system time when red on. then get system time when override on. subtract them to get q; convert hour minute second and milliseconds to milliseconds ==REDmillis same s ORmillis. //then turn off red. */ //turn off motor digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low ORset=1; } /*ga2win ng override mgggreen patay c red; mggreen qng ilang seconds ng red knina bgo mg override save number of seconds in variable q..and set variable r to 1. p=0;*/ if(ORset==0) { currentMillis = millis(); if(currentMillis - previousMillis > threesec) { previousMillis = millis();// save the last time you blinked the LED //the motor should be on reverse (counterclockwise) for 3 secs //motor stops digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } } } // if there is no "1" in serial port, motor inactive else { digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } }}