Hi! Can someone help me with this code? I tried to write this code for the ultrasonic sensor component in order to understand how to use the registers of arduino. Someone can look at it, because there is obviously something wrong? (I apologize in advance if i write something no sense).
const uint8_t echoPin=3;
volatile double distance=0.0;
volatile uint8_t stato=0;
volatile long duration;
void setup() {
Serial.begin(9600);
DDRD = 0xF7; // sets all pins on PORTD to input, except pin 3 which is set to output
DDRB = 0x3F; // sets all pins on PORTB to input, except pins 0-5 which are set to output
TCCR1A = 0;
TCCR1B |= _BV(CS11); // sets prescaler for Timer/Counter1 to 8
EICRA |= _BV(ISC10); // sets external interrupt request 1 (INT1) to trigger on falling edge
EICRA &= ~_BV(ISC11); // sets external interrupt request 1 (INT1) to trigger on falling edge
EIMSK |= _BV(INT1); // enables external interrupt request 1 (INT1)
PORTB |= _BV(triggerPin-8); // sets triggerPin-8 pin on PORTB to high
PORTB &= ~_BV(echoPin-8); // sets echoPin-8 pin on PORTB to low
}
void loop() {
// Send trigger pulse
PORTB |= _BV(PB0);
delayMicroseconds(10);
PORTB &= ~_BV(PB0);
// Wait for echo pulse to start
while (!(PINB & _BV(PB1)));
// Start timer
TCNT2 = 0;
// Wait for echo pulse to end
while (PINB & _BV(PB1));
// Stop timer
duration = TCNT2;
// Calculate distance
distance = (duration / 2) * 0.034;
// Print distance to serial monitor
Serial.println(distance);
// Wait before next measurement
delay(1000);
}
// Interrupt service routine for echo pin
ISR(PCINT0_vect) {
if (PINB & _BV(PB1)) {
TCNT2 = 0;
} else {
duration = TCNT2;
}
}
Fix the mistakes that have been pointed out. Fix any mistakes you find on your own. If the code still doesn't work, post your latest code here and we'll look for any remaining mistakes.