Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <SoftPWM.h>
  2.  
  3. #define POTPIN A0    // select the input pin for the potentiometer
  4. #define PWMPIN A1 // Output pin to control PWM for motor controller
  5. #define DIRPIN A2 // Output direction for motor controller
  6. #define SENSORPIN A3 // position sensor pin
  7. //#define DIRSW A3 // direction switch
  8. //int dirSwState = 0; // last state of the direction switch
  9. //int dirLast = 0; // last state to compare direction switch against
  10. int potValue = 0;  // variable to store the value coming from the sensor
  11. int sensorValue = 0; // value of position sensor
  12. long prevPos1 = 0;
  13. long prevPos2 = 0;
  14. long curPos = 0;
  15. long prevSPos1 = 0;
  16. long prevSPos2 = 0;
  17. long curSPos = 0;
  18.  
  19. #define MIN_POT_VAL 0
  20. #define MAX_POT_VAL 1023
  21. #define MIN_SENSOR_VAL 32
  22. #define MAX_SENSOR_VAL 1018
  23. #define SLOW_RANGE 51 // number of encoder values away before decel begins
  24.  
  25. int mspeed = 0;
  26. int newspeed = 0;
  27.  
  28. void setup() {
  29.  
  30.   SoftPWMBegin();
  31.   SoftPWMSet(PWMPIN, 0);
  32.   SoftPWMSetFadeTime(ALL, 100, 400);
  33.   //pinMode(PWMPIN, OUTPUT);
  34.    
  35.   pinMode(DIRPIN, OUTPUT);
  36.  // pinMode(DIRSW, INPUT);
  37.  
  38.   Serial.begin(9600);
  39. }
  40.  
  41. void loop() {
  42.   // read the value from the sensor:
  43.   long newPos = analogRead(POTPIN);  
  44.   long newSPos = analogRead(SENSORPIN);    
  45.  
  46.   // debounce pot
  47.   if (newPos != prevPos1 && newPos != prevPos2) {
  48.     prevPos1 = prevPos2;
  49.     prevPos2 = newPos;
  50.     curPos = map(newPos, MIN_POT_VAL, MAX_POT_VAL, MIN_SENSOR_VAL, MAX_SENSOR_VAL);
  51.   }
  52.  
  53.   // debounce encoder
  54.   if (newSPos != prevSPos1 && newSPos != prevSPos2) {
  55.     prevSPos1 = prevSPos2;
  56.     prevSPos2 = newSPos;
  57.  
  58.     curSPos = map(newSPos, MIN_SENSOR_VAL, MAX_SENSOR_VAL, MIN_POT_VAL, MAX_POT_VAL);
  59.   }  
  60.  
  61.   if(curPos <= curSPos){ // Backwards
  62.     digitalWrite(DIRPIN, LOW);
  63.    
  64.     unsigned int in_range = curPos + SLOW_RANGE;
  65.  
  66.     if(curSPos > in_range){ // Max speed
  67.         SoftPWMSet(PWMPIN, 255);
  68.     } else {
  69.  
  70.       if((curPos+50) == curSPos) // 75%
  71.       {
  72.         SoftPWMSet(PWMPIN,190);
  73.       }
  74.       if((curPos+40) == curSPos) // 50%
  75.       {
  76.         SoftPWMSet(PWMPIN, 125);
  77.       }
  78.       if((curPos+30) == curSPos) // 25%
  79.       {
  80.         SoftPWMSet(PWMPIN, 60);
  81.       }
  82.       if((curPos+20) == curSPos) // 12%
  83.       {
  84.         SoftPWMSet(PWMPIN, 31);
  85.       }
  86.        if((curPos+10) == curSPos) // 12%
  87.       {
  88.         SoftPWMSet(PWMPIN, 15);
  89.       }
  90.       if((curPos+3) == curSPos) // 6%
  91.       {
  92.         SoftPWMSet(PWMPIN, 0);
  93.       }
  94.     }
  95.   }
  96.  
  97.   if(curPos >= curSPos){ // Forward
  98.     digitalWrite(DIRPIN, HIGH);
  99.  
  100.     unsigned int in_range = curPos - SLOW_RANGE;
  101.  
  102.     if(curSPos < in_range){ // Max speed
  103.         SoftPWMSet(PWMPIN, 255);
  104.     } else {
  105.  
  106.       if((curPos-50) == curSPos) // 75%
  107.       {
  108.         SoftPWMSet(PWMPIN,190);
  109.       }
  110.       if((curPos-40) == curSPos) // 50%
  111.       {
  112.         SoftPWMSet(PWMPIN, 125);
  113.       }
  114.       if((curPos-30) == curSPos) // 25%
  115.       {
  116.         SoftPWMSet(PWMPIN, 60);
  117.       }
  118.       if((curPos-20) == curSPos) // 12%
  119.       {
  120.         SoftPWMSet(PWMPIN, 31);
  121.       }
  122.        if((curPos-10) == curSPos) // 12%
  123.       {
  124.         SoftPWMSet(PWMPIN, 15);
  125.       }
  126.       if((curPos-3) == curSPos) // 6%
  127.       {
  128.         SoftPWMSet(PWMPIN, 0);
  129.       }
  130.      
  131.     }
  132.   }
  133.  
  134.   if(curPos == curSPos){ // Stop
  135.     SoftPWMSet(PWMPIN, 0);
  136.   }
  137.  
  138.  // if(dirLast != dirSwState){
  139.  //   Serial.println(dirSwState);
  140.  //   dirLast = dirSwState;
  141.  //}
  142.  
  143.   //analogWrite(pwmPin, val);  
  144.   //SoftPWMSet(PWMPIN, val);          
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement