|
#include <EEPROM.h>
int PWM_Pin = 9;
int Speed_Pin = 3;
int count;
char failed;
volatile unsigned int rotate_count;
void rotate()
{
rotate_count++;
}
void setup()
{
pinMode(Speed_Pin, INPUT);
pinMode(PWM_Pin, OUTPUT);
digitalWrite(PWM_Pin, LOW);
failed = 0;
Serial.begin(9600);
count = EEPROM.read(2);
int value = EEPROM.read(1);
count = count*256 + value;
}
void run_a_speed(unsigned char pwm, int second, char print_speed)
{
analogWrite(PWM_Pin, pwm);
delay(500);
rotate_count = 0;
attachInterrupt(1, rotate, FALLING);
delay(200);
detachInterrupt(1);
if(rotate_count<5)
{
failed = 1;
return;
}
if(print_speed)
{
Serial.print(150*rotate_count, DEC);
Serial.print(" ");
}
delay(300);
if(second==1) return;
for(int c=0;c<(second-1)*5;c++)
{
rotate_count = 0;
attachInterrupt(1, rotate, FALLING);
delay(200);
detachInterrupt(1);
if(rotate_count<5)
{
failed = 1;
return;
}
}
}
void loop()
{
Serial.print(count, DEC);
Serial.print(" ");
for(char c=0;c<6;c++)
{
run_a_speed(130, 30, 1); if(failed) break; // 21750 RPM
run_a_speed(150, 2, 0); if(failed) break; // 23100 RPM
run_a_speed(90, 3, 0); if(failed) break; // 18150 RPM
run_a_speed(160, 2, 0); if(failed) break; // 24000 RPM
run_a_speed(130, 10, 0); if(failed) break; // 21750 RPM
run_a_speed(90, 3, 0); if(failed) break; // 18150 RPM
}
Serial.println();
if(failed)
{
analogWrite(PWM_Pin, 0);
Serial.println("failed");
for(;;);
}
count++;
EEPROM.write(2, count/256);
EEPROM.write(1, count & 0xFF);
analogWrite(PWM_Pin, 0);
delay(60000);
}
|
|