/*
Attiny85 ile jikle koruması için yeni program
Attiny pin 5 (PB2) alıcıdan giriş
Attiny pin 6 (PB1) ateşleme sensörü girişi
Attiny pin 7 (PB0) Servo çıkışı
Attiny pin 2 Analog pot girişi ADC3
Attiny pin 3 Buton girişi PB4
*/
byte x = 0; //Port okuma değişkeni
volatile unsigned long rpm1 = 0;
volatile unsigned long rpm2 = 0;
unsigned long srpm1 = 0;
unsigned long srpm2 = 0;
unsigned int servopwm = 1500;
unsigned int offposition = 1500;
volatile byte rpmx = 0;
volatile byte prevrpmx = 0;
volatile byte rflag = 0;
volatile byte sregvalue;
const int pwmpin = 2;
const int rpmpin = 1;
const int servopin = 0;
const int potin = 3;
const int buton = 4;
unsigned long devir = 0;
unsigned long frame;
long period;
void setup()
{
noInterrupts();
pinMode(servopin, OUTPUT);
pinMode(pwmpin, INPUT);
pinMode(rpmpin, INPUT_PULLUP);
pinMode(buton, INPUT_PULLUP);
offposition = 1000 + analogRead(potin);
GIMSK |= B00100000; // Pin Change Interrupt enable
PCMSK |= B00000010; // PB1 için Pin Change Interrupt enable
interrupts();
}
void loop()
{
while (digitalRead(buton) == 0)
{
setoffposition();
}
servopwm = pulseIn(pwmpin, HIGH, 22000);
if (devir > 1000)
{
servopwm = offposition;
}
if((servopwm < 800) || (servopwm > 2200)) servopwm = offposition;
servoout();
if (rflag = 1)
{
noInterrupts();
srpm1 = rpm1;
srpm2 = rpm2;
interrupts();
calcrpm();
}
}
ISR(PCINT0_vect)
{
sregvalue = SREG;
x = PINB & B00000010;
rpmx = x & B00000010;
if (rpmx != prevrpmx)
{
prevrpmx = rpmx;
if (rpmx > 0)
{
rpm2 = rpm1;
rpm1 = millis();
rflag = 1;
}
}
SREG = sregvalue;
}
void servoout()
{
digitalWrite(servopin, HIGH);
delayMicroseconds(servopwm);
digitalWrite(servopin, LOW);
}
void calcrpm()
{
rflag = 0;
period = srpm1 - srpm2;
if (period > 100 || period <= 0)
{
devir = 0;
return;
}
devir = 60000 / period;
}
void setoffposition()
{
frame = millis();
// noInterrupts();
offposition = 1000 + analogRead(potin);
servopwm = offposition;
servoout();
// interrupts();
while((millis() - frame) < 20)
{
}
}