Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_PWMServoDriver.h>
- #include <FreqPeriodCounter.h>
- Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
- bool serialVerbose = false;
- uint16_t setPoint[16]; // number of set points
- uint16_t initsetPointVal = 512; // setPoint initialization value
- float setPointRPM = 320.0;
- float valueRPM = 0.0;
- float counterPeriod = 0.0;
- uint16_t pulsePerRev = 4; // May depend on fan motor design. Could be 3 for the 9 mm motors
- int numReadings = 1;
- #define maxReadings 1000
- float readings[maxReadings];
- uint16_t readIndex = 0;
- float total = 0;
- float average = 0;
- /*
- 512 = 320 RPM
- 640 = 350 RPM
- 768 = 660 RPM
- 896 = 925 RPM
- 1024 = 1150 RPM
- 1152 = 1400 RPM
- 1280 = 1670 PRM
- 1408 = 1930 RPM
- 1536 = 2225 RPM
- 1664 = 2682 RPM ** probable top stir speed
- 2048 = RPM
- 3072 = 7500 RPM ** Starting Speed **
- */
- const byte counterPin = 5;
- const byte counterInterrupt = 5; // not sure if this works with D5
- FreqPeriodCounter counter(counterPin, micros, 0);
- void setup() {
- SerialUSB.begin(115200);
- delay(1000);
- //while (!SerialUSB) {
- // delay(1); // wait for serial port to connect. Needed for native USB port only
- //}
- SerialUSB.println("16 channel PWM test Plus Period Counter!");
- //*/
- pinMode(counterPin, PIO_EXTINT);
- attachInterrupt(counterInterrupt, counterISR, CHANGE);
- for (int thisReading = 0; thisReading < numReadings; thisReading++) {
- readings[thisReading] = 0;
- }
- pwm.begin();
- // pwm.setOscillatorFrequency(27000000);
- pwm.setPWMFreq(1000); // This is the maximum PWM frequency
- // Wire.setClock(400000);
- // initialize setPoint array Values
- for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
- setPoint[pwmnum] = initsetPointVal;
- }
- // Set to zero for testing
- for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
- pwm.setPWM(pwmnum, 0, 0);
- }
- delay(5000);
- // Start Spinning
- startSequence(3072, initsetPointVal, 50);
- }
- void loop() {
- //delay(100);
- if(counter.ready()){//*/
- counterPeriod = counter.period;
- valueRPM = (1/((counterPeriod*pulsePerRev/1000)/1000))*60;
- SerialUSB.print(valueRPM, 1);
- total = total - readings[readIndex];
- readings[readIndex] = valueRPM;
- total = total + readings[readIndex];
- readIndex++;
- if (readIndex >= numReadings){
- readIndex = 0;
- }
- average = total/numReadings;
- SerialUSB.print("\t");
- SerialUSB.println(average, 1);
- if(serialVerbose){
- SerialUSB.print(counter.period);
- SerialUSB.print("\t");
- SerialUSB.print(counter.pulseWidth);
- SerialUSB.print("\t");
- SerialUSB.println(counter.pulseWidthLow);
- }
- }//*/
- }
- void startSequence(int startVal, int finalVal, int startDuration){
- for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
- pwm.setPWM(pwmnum, 0, startVal);
- }
- delay(startDuration);
- for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
- pwm.setPWM(pwmnum, 0, setPoint[pwmnum]);
- }
- }
- void counterISR(){
- counter.poll();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement