int led = PB4;
int ip = PB2;
int counter = 0;
static int prevState = 4 ;
static int delay_flag = 0;
const uint16_t t1_load = 0;
const uint16_t t1_comp = 62500;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
DDRB |= (1 << led); //setting led an output
DDRB &= ~(1 << ip);//ir sensor as input
PORTB |= (1 << ip); //setting 1 thus enabling pull up resistor
//reset timer control reg A
TCCR1A = 0;
//set prescaler of 256
TCCR1B |= (1 << CS12);
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS10);
//reset timer 1 and set compare values
TCNT1 = t1_load;
OCR1A = t1_comp;
//enable timer compare interrupts
TIMSK1 = (1 << OCIE1A);
//enable global interrupts
sei();
}
void loop() {
// put your main code here, to run repeatedly:
check();
if(delay_flag == 1){
PORTB |= (1 << led);
delay(3000);
PORTB &= ~(1 << led);
delay_flag = 0;
counter = 0;
}
}
void failsafe(){
PORTB &= ~(1 << led);
counter =0 ;
}
//4 not detected(1) 0 detected (0)
void check() {
PORTB &= ~(1 << led); //led low;
int currState = (PINB & B00000100);
Serial.print(currState);
if (prevState == 0 && currState == 4 )
{
PORTB |= (1 << led);
delay(3000);
PORTB &= ~(1 << led);
counter = 0;
}
prevState = currState;
}
ISR(TIMER1_COMPA_vect) {
counter = counter + 1;
if(counter == 20){
delay_flag = 1;
TCNT1 = t1_load;
}
}
See the blink without delay example...
File -> Examples -> 02. Digital -> Blink Without Delay
i tried using the millis function , but it still isnt working
void check() {
PORTB &= ~(1 << led); //led low;
int currState = (PINB & B00000100);
Serial.print(currState);
if (prevState == 0 && currState == 4 )
{
long currTime = millis();
PORTB |= (1 << led);
if(currTime - prevTime >= interval){
PORTB &= ~(1 << led);
counter = 0;
prevTime = currTime ;
}
}
prevState = currState;
}
Please read the sticky at the top of the forum: "Read this before posting a programming question ..."
If you follow the forum guidelines and then ask your question using complete sentences, you are likely to get more guidance.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.
And see Using millis() for timing. A beginners guide if you need more explanation.
...R
Probably because you start with...
PORTB &= ~(1 << led); //led low;
If every time you call function check() you turn the led off, what do you think the overall effect on the led is likely to be?
Also this code...
if (prevState == 0 && currState == 4 )
...seems to be checking for a state change.
If your millis() code is inside that if it is not going to work well. Millis() testing code needs to be where it executes continuously from loop().