In: Electrical Engineering
witches »
 
HW-20: Arduino - Motor Speed.problem
Arduino - Measuring Motor Speed
Arduino sketch with corresponding line numbers per
coding line.
Match line # 1 through #17 with the corresponding instruction
purpose.
_____________________________________________________________________
1.   int count = 0;
     float f = 0;
     float PPR = 500;
     float RPM= 0;
2.   void setup()
3.      pinMode(2,INPUT);
4.     
attachInterrupt(0,freq,RISING);
5.      Serial.begin(9600);
6.    void loop()
      {
7.      count=0;
8.      delay(100);
9.      f=10*count;
10.    RPM=f/PPR;
11.    Serial.print(count);
12.    Serial.print(" Pulses/100ms");
13.    Serial.print("\t");
14.    Serial.print(f);
15.    Serial.println(" Hz");
       }
16.   void freq()
        { 
17.      count=count++; 
        }
 1 2 3 4 5  Establish inputs and
outputs 
 1 2 3 4 5  Declare
Variables 
 1 2 3 4 5  Establish output
communications with the monitor 
 1 2 3 4 5  Declare digital
input pin 
 1 2 3 4 5  Establish interrupt
digital input pin, name and mode 
 Tries 0/2
 6 7 8 9 10  Establish initial
count value within the running program loop 
 6 7 8 9 10  Infinite loop -
running program 
 6 7 8 9 10  Calculate
revolutions per minute 
 6 7 8 9 10  Calculate
freqency 
 6 7 8 9 10  Delay 100 ms while
counting interrupt pulses 
 Tries 0/2
 11 12 13 14 15  Print text
"Pulses/100ms" to monitor 
 11 12 13 14 15  Print count
value to monitor 
 11 12 13 14 15  Print text "
Hz" to monitor then line feed 
 11 12 13 14 15  Print frequency
value to monitor 
 11 12 13 14 15  Print tab
spacing to monitor 
 Tries 0/2
 16 17  Interrupt subroutine 
 16 17  Add one to the count which is
accumulating the number of pulses in 100ms 
 Tries 0/2
1.  int count = 0;
     float f = 0;
     float PPR = 500;
     float RPM= 0; // Declare Variables
2.   void setup() // Establish output communications
with the monitor
3.   pinMode(2,INPUT); // Establish inputs and
outputs
4.   attachInterrupt(0,freq,RISING); // Establish
interrupt digital input pin, name and mode
Tries 0/2
5.    Serial.begin(9600); // Declare digital
input pin
6.    void loop() // Infinite loop - running
program
      {
7.    count=0; // Establish initial count value
within the running program loop
8.    delay(100); // Delay 100 ms while
counting interrupt pulses
Tries 0/2
9.      f=10*count; // Calculate
freqency
10.    RPM=f/PPR; // Calculate revolutions per
minute
11.    Serial.print(count); // Print count value to
monitor
12.    Serial.print(" Pulses/100ms"); // Print text
"Pulses/100ms" to monitor
13.    Serial.print("\t"); // Print tab spacing to
monitor
Tries 0/2
14.    Serial.print(f); // Print frequency value to
monitor
15.    Serial.println(" Hz"); // Print text " Hz" to
monitor then line feed
       }
16.   void freq() // Interrupt subroutine
        {
17.      count=count++; // Add one to the
count which is accumulating the number of pulses in 100ms
Tries 0/2
        }