Advertisement
j0h

IRSys.ino

j0h
Mar 25th, 2023
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Include the Arduino Stepper Library
  2. #include <Stepper.h>
  3. #include <IRremote.h>
  4.  
  5. const int RECV_PIN = 2;
  6. IRrecv irrecv(RECV_PIN);
  7. decode_results results;
  8. const int stepsPerRevolution = 200;
  9.  
  10. Stepper myStepper(stepsPerRevolution, 4,5,6,7);
  11.  
  12. int lim = 39; //lim is the max number of linear axis steps
  13. int count = 0;
  14. int minstep=28; //~2 degrees at28 to 30
  15. int OldX=0;
  16. int OldY=0;
  17. int NewX=0;
  18. int NewY=0;
  19. int IsHome=0;
  20. int buttonState = 0;
  21.  
  22. int nav(int x, int y);
  23. int goHome();
  24.  
  25. void setup(){
  26.   irrecv.enableIRIn(); // Start the receiver
  27.   irrecv.blink13(true);
  28.  
  29.   myStepper.setSpeed(30);
  30. pinMode(14, OUTPUT); //DC motor
  31. pinMode(15, OUTPUT); //dc motor other direction
  32. pinMode(10, INPUT_PULLUP);
  33.   Serial.begin(9600);
  34.   delay(6000);
  35. //if(IsHome==0){
  36. //goHome();
  37. //}
  38.  
  39. }
  40.  
  41.  
  42.  
  43. void loop(){
  44.   if (IrReceiver.decode())
  45.    {
  46.       Serial.println(IrReceiver.decodedIRData.command);
  47.       if(IrReceiver.decodedIRData.command==21){
  48.         goHome();
  49.         //isHome now set to 1
  50.         }
  51.       if(IrReceiver.decodedIRData.command==64){
  52.         //y axis +
  53.          myStepper.step(10);
  54.          
  55.         }
  56.       if(IrReceiver.decodedIRData.command==25){
  57.         //move towards center
  58.          myStepper.step(-10);
  59.         }
  60.       if(IrReceiver.decodedIRData.command==7){
  61.         //counter clock wize
  62.       analogWrite(14,0);
  63.       analogWrite(15, 150); //motor beeps if its too low
  64.       delay(30);
  65.       analogWrite(15,0);
  66.         }
  67.       if(IrReceiver.decodedIRData.command==9){
  68.         //step clockwize
  69.           analogWrite(15,0);
  70.       analogWrite(14, 150); //motor beeps if its too low
  71.       delay(30);
  72.       analogWrite(14,0);
  73.       }
  74.       IrReceiver.resume();
  75.    }
  76. } //end void loop()
  77.  
  78.  
  79.  
  80.  
  81. int nav(int x, int y){
  82. //x is infinite, y is limited. if y is negative the limit is 0.
  83. // y is allso max limited at IDK, like 70 turns, so figure out the limit there too.
  84. int difX=NewX-OldX;
  85. int difY=NewY-OldY;
  86.  
  87.   if(NewX > OldX){
  88.        analogWrite(14, 55); //motor beeps if its too low
  89. delay(difX*60);
  90. analogWrite(14,0);  
  91.     }
  92.    if(NewX < OldX){
  93.       analogWrite(15, 55); //motor beeps if its too low
  94.       delay(difX*60);
  95.       analogWrite(15,0);
  96.     }
  97.    
  98.     if(NewX == OldX){
  99.        analogWrite(14,0);
  100.        analogWrite(15,0);
  101.       }
  102.  
  103.  if(NewY < OldY){
  104.   if(difY<0){
  105.     difY=0;
  106.     goHome();
  107.     }else{
  108.      myStepper.step(difY);
  109.   }
  110.  }
  111.  if(NewY > OldY){
  112.      myStepper.step(difY);
  113.   }
  114.      if(NewY == OldY){
  115.         myStepper.halt();
  116.       }
  117.  OldX=NewX;
  118. OldY=NewY;
  119.  
  120.   return 0;
  121.   }
  122.  
  123. int goHome(){
  124.   buttonState = digitalRead(10);
  125. while(buttonState==HIGH){
  126.   //Serial.println("HIGH");
  127.    myStepper.step(stepsPerRevolution); //going home
  128.     Serial.println("High");
  129.  
  130.    buttonState = digitalRead(10);
  131.   if(buttonState==LOW) {
  132.   Serial.println("LOW");
  133.      //move to center
  134.    for (int i =0; i<20; i++){
  135.    myStepper.step(-stepsPerRevolution);
  136.    Serial.print("i step count: ");
  137.    Serial.println(i);
  138.   }
  139.   myStepper.halt();
  140.   IsHome=1;
  141.   }
  142. }
  143.   return IsHome;
  144.   }    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement