manhoosbilli1

Tilt motion with 2 limit switches

Aug 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. //defining pins
  2. boolean swStatus1=0;    //boolean to read the switch status and store it in here    
  3. boolean swStatus2=0;    //boolean to read the switch status and store it in here          
  4. const byte swPin1=6;  //switch 1 pin
  5. const byte swPin2=5;  //switch 2 pin
  6. const byte in1=4;    //motordirection 1 pin
  7. boolean  in1Status;
  8. boolean in2Status;      //l298N motor controller pins
  9. const byte in2=3;      //motordirection 2nd pin
  10. const byte buzzer = 10;  //buzzer's pin
  11.  
  12. //defining timing variables
  13. unsigned long currentMillis=0;    //stores the current milli seconds of arduino
  14. unsigned long lastReverse=0;      //stores the ms of the last time motor was reverse    //timer to go off and start action
  15. const int turnInterval= 5000;     //marks the interval of 1s for timing
  16.  
  17. void setup() {
  18.   pinMode(swPin1, INPUT);
  19.   pinMode(swPin2, INPUT);
  20.   pinMode(in1, OUTPUT);
  21.   pinMode(in2, OUTPUT);
  22.   pinMode(buzzer,OUTPUT);
  23.   Serial.begin(9600);
  24.   currentMillis = millis();
  25.   swStatus1 = digitalRead(swPin1);                  //buckets for switches to make it boolean
  26.   swStatus2 = digitalRead(swPin2);
  27.  
  28.   initialCalibration();
  29. }
  30.  
  31. void loop() {
  32.   // put your main code here, to run repeatedly:
  33.   currentMillis = millis();      
  34.   //marks the timer
  35.  
  36.   //buckets for switches to make it boolean
  37.   swStatus1 = digitalRead(swPin1);                  
  38.   swStatus2 = digitalRead(swPin2);             //will run backup to see if everything is correct. if yes then proceed.
  39.   in1Status = digitalRead(in1);                //will remember the status of these pins
  40.   in2Status = digitalRead(in2);
  41.  
  42.   //the motors will stay low until the period has expired which is 1 hour
  43.   //the main function is
  44.  
  45.   checkAndReverse();
  46.  
  47. }
  48.  
  49.  
  50. //                        FUNCTIONS                    //
  51.  
  52. void initialCalibration() {
  53.  
  54. if((swStatus1 == 0) && ( swStatus2 == 0)){     //the following program will not trigger if one of the switches is high
  55.     //and the this function should end when one of the switches becomes high    
  56.     //calibrates the tray at the beggining to get the initial point
  57.  
  58.  
  59.     digitalWrite(in1, HIGH);    
  60.     digitalWrite(in2, LOW);
  61.     //go in this direction until one of the switches becomes high and the tray is at 45 degree  
  62.                            
  63.     //JUST goes in this direction until either switch is pressed
  64.    
  65.    } else {        
  66.     //once the upper condition is no longer true, that 'if statement' will stop working and the following will work
  67.     digitalWrite(in1, LOW);
  68.     digitalWrite(in2, LOW);
  69.  
  70.     //if the starting if condition becomes false stop the motors
  71.     //change the states manually. i dont know if this is necessary
  72.     in1Status = LOW;
  73.     in1Status = LOW;
  74.     swStatus1 = digitalRead(swPin1);
  75.     swStatus2 = digitalRead(swPin2);
  76.    }
  77. }
  78.  
  79.  
  80. //           MAIN FUNCTION BREAK IT DOWN   //
  81.  
  82. void checkAndReverse() {
  83.   // i do not know if i should mark the timer here before the next command or should i do it after the switch has been deteced  
  84.    
  85.   //before executing the next commands it will wait for that 1 hour to expire
  86.   //motors should already be in the low positions; not moving one of the switches must be high
  87.   //and in1 and in2 should both be low
  88.   if(swStatus1 == HIGH && swStatus2 == LOW || swStatus1 == LOW && swStatus2 == HIGH) {
  89.          
  90.  
  91.     // i am doubtful about the syntax maybe it is wrong.          
  92.     //no matter which position it is in. once all of the above conditions are true the next code will begin
  93.    
  94.     digitalWrite(in1, LOW);
  95.     digitalWrite(in2, LOW);                     // the motor is already stopped no need for this  
  96.                                  
  97.     //stop the motor until the delay has expired, this is where problem starts
  98.     //idk why but i think if i set these variables everytime, it will be accurate
  99.     //i see no harm in it.
  100.    
  101.     swStatus1 = digitalRead(swPin1);                                            
  102.     swStatus2 = digitalRead(swPin2);                                              
  103.     in1Status = digitalRead(in1);
  104.     in2Status = digitalRead(in2);
  105.                                              
  106.     //i do not know if the 'if nested inside the if' statments will work once the switches are both LOW and the motor starts moving
  107.    
  108.    // if ((swStatus1 == LOW) && (swStatus2 == LOW)) {       //these can't be low because the tray is still in tilt position from initial calibration    
  109.   //wait for the delay to expire. which is 1 hour actually
  110.   //this is what the problem is. i want the motor to start in opposite direction
  111.   //if i do what i have done in the following code, since both of 'instates' are LOW already and when it plugs in the opposite value
  112.   //the motor would stop moving.
  113.   //how do i change only
  114.  
  115.   if(currentMillis - lastReverse >=  turnInterval) {
  116.     digitalWrite(in1, !in1Status);        //reverses the direction                  
  117.     digitalWrite(in2, !in2Status);    
  118.                      
  119.     //i just dont know how to time this. if i say make time the trigger what if the motor stops between the
  120.    
  121.     //motion when it hasn't reached the next button and if i choose to make the swstatuses the trigger then how do i tell it that when the delay is finished
  122.    
  123.     //opposite the direction until the switches change direction and one is high the other is low
  124.    
  125.     in1Status= digitalRead(in1);                                //reads the motor pins agains
  126.     in2Status= digitalRead(in2);
  127.     swStatus1 = digitalRead(swPin1);
  128.     swStatus2 = digitalRead(swPin2);                          //reads the sw pins again
  129.                                                              //i do not know if i should else if or else or just make another if statements
  130.  
  131.     if (swStatus1 == LOW && swStatus2 == HIGH || swStatus1 == HIGH && swStatus2 == LOW) {
  132.     digitalWrite(in1, !digitalRead(in1));
  133.     digitalWrite(in2, !digitalRead(in2));           //reverses both statuses which will automatically go the opposite direction
  134.     in1Status = digitalRead(in1);
  135.     in2Status = digitalRead(in2);
  136.     swStatus1 = digitalRead(in1);
  137.     swStatus2 = digitalRead(in2);
  138.    
  139.     }
  140.    }
  141.   }
  142. }
Add Comment
Please, Sign In to add comment