Advertisement
Guest User

Untitled

a guest
Jun 10th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. boolean toggle1 = 0;                                                // a value to toggle on and off to drive the stepper
  2. const int interval = 200;                                           // the interval over which to get each new speed value
  3. unsigned previousMillis = 0;                                        // will store the last time the speed value was updated
  4. const int numReadings = 30;                                         // the number of readings to be averaged out
  5. int RPM = 0;                                                        // a single RPM reading
  6. int RPMArrayIndex = 0;                                              // position within the array
  7. int RPMArray[numReadings];                                          // an array of RPM readings
  8. int RPMTotal = 0;                                                   // running total of the last numReadings RPM values
  9. int averageRPM = 0;                                                 // the average of all numReadings values in the array
  10. int toggleFrequency = 1;                                            // default frequency at which to toggle the output
  11.  
  12. #include "src/LiquidCrystal_I2C/LiquidCrystal_I2C.h"
  13.  
  14. LiquidCrystal_I2C lcd(0x27, 16, 2);
  15.  
  16. void setup() {
  17.  
  18.   Serial.begin(9600);
  19.  
  20.   pinMode(LED_BUILTIN, OUTPUT);
  21.  
  22.   pinMode(2, INPUT_PULLUP);
  23.   pinMode(3, INPUT_PULLUP);
  24.   pinMode(5, OUTPUT);
  25.   pinMode(6, OUTPUT);
  26.  
  27.   for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  28.     RPMArray[thisReading] = 0;                                      // set every reading in the array to 0
  29.   }
  30.  
  31.   lcd.init();                                                       // initialize the LCD panel
  32.   lcd.backlight();                                                  // turn the LCD backlight on
  33.  
  34.   cli();                                                            // stop interrupts
  35.  
  36.   TCCR1A = 0;                                                       // set entire TCCR1A register to 0
  37.   TCCR1B = 0;                                                       // same for TCCR1B
  38.   TCNT1  = 0;                                                       // initialize counter value to 0
  39.   OCR1A = 233;                                                      // set compare match register (CMR) to 66.6 Hz interrupts
  40.   TCCR1B |= (1 << WGM12);                                           // turn on CTC mode
  41.   TCCR1B |= (1 << CS12) | (1 << CS10);                              // Set CS10 and CS12 bits for 1024 prescaler
  42.   TIMSK1 |= (1 << OCIE1A);                                          // enable timer compare interrupt
  43.  
  44.   sei();                                                            // allow interrupts
  45.  
  46.   lcd.setCursor(2, 0);
  47.   lcd.print("Set:");
  48.   lcd.setCursor(10, 0);
  49.   lcd.print(" RPM");
  50.   lcd.setCursor(10, 1);
  51.   lcd.print(" RPM");
  52.   lcd.setCursor(0, 1);
  53.   lcd.print("Actual:");
  54.  
  55. }
  56.  
  57. ISR(TIMER1_COMPA_vect)
  58. {
  59.   if (toggle1) {                                                    // toggle the pulse for the stepper on and off again
  60.     digitalWrite(LED_BUILTIN, HIGH);
  61.     toggle1 = 0;
  62.   }
  63.   else {
  64.     digitalWrite(LED_BUILTIN, LOW);
  65.     toggle1 = 1;
  66.   }
  67. }
  68.  
  69. void loop() {
  70.  
  71.   int currentMillis = millis();
  72.  
  73.   if (currentMillis - previousMillis >= interval) {                 // if the interval has elapsed, then execute everything below
  74.     previousMillis = currentMillis;
  75.  
  76.     RPMTotal = RPMTotal - RPMArray[RPMArrayIndex];                  // remove old value from running total
  77.  
  78.     if (digitalRead(2) == LOW) {                                    // either speed up, slow down, or
  79.       RPM = RPM + 10;                                               // neither depending on input
  80.     }
  81.  
  82.     else if (digitalRead(3) == LOW) {
  83.       RPM = RPM - 10;
  84.     }
  85.  
  86.     else {
  87.       RPM = RPM;
  88.     }
  89.  
  90.     if (RPM > 380) {
  91.       RPM = 380;
  92.     }
  93.  
  94.     else if (RPM < 10) {
  95.       RPM = 10;
  96.     }
  97.  
  98.     else {
  99.       RPM = RPM;
  100.     }
  101.  
  102.     RPMArray[RPMArrayIndex] = RPM;                                 // add new speed value to array
  103.  
  104.     RPMTotal = RPMTotal + RPMArray[RPMArrayIndex];                 // add new speed value to running total
  105.  
  106.     RPMArrayIndex = RPMArrayIndex + 1;                             // advance to next position in array
  107.  
  108.     if (RPMArrayIndex >= numReadings) {                            // if at end of array, return to beginning
  109.       RPMArrayIndex = 0;
  110.     }
  111.  
  112.     averageRPM = RPMTotal / numReadings;
  113.  
  114.     toggleFrequency = ((averageRPM * 200 / 60) * 2);
  115.     long cmr = (1.6 * 10e6) / (1024 * toggleFrequency) - 1;
  116.     OCR1A = cmr;
  117.  
  118.     Serial.print("rpm: ");
  119.     Serial.println(averageRPM);
  120.     Serial.print("freq: ");
  121.     Serial.println(toggleFrequency);
  122.     Serial.print("cmr: ");
  123.     Serial.println(cmr);
  124.     Serial.println();
  125.  
  126.     lcd.setCursor(7, 0);
  127.     lcd.print(RPM);
  128.  
  129.     lcd.setCursor(7, 1);
  130.     lcd.print(averageRPM);
  131.  
  132.   }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement