/*
Uno with Scope https://github.com/Dlloydev/Wokwi-Chip-Scope
and https://github.com/Dlloydev/Wokwi-Chip-PWM
Wokwi Uno https://wokwi.com/projects/390819301187622913
Wokwi Mega: https://wokwi.com/projects/390819455604080641
*/
/*
Running Blink.ino
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://docs.arduino.cc/hardware/
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://docs.arduino.cc/built-in-examples/basics/Blink/
*/
extern uint32_t timer0_overflow_count;
extern uint32_t timer0_millis;
const byte Trig = 9;
const byte Echo = 7;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Trig, OUTPUT);
Serial.begin(115200);
}
// the loop function runs over and over again forever
void loop() {
const bool useMicros = true;
uint32_t t0, t1, t2;
digitalWrite(Trig, HIGH); digitalWrite(Trig, LOW);
t0 = useMicros? micros() : timer0count();
while (digitalRead(Echo) != HIGH);
t1 = useMicros? micros() : timer0count();
while (digitalRead(Echo) != LOW);
t2 = useMicros? micros() : timer0count();
Serial.print(" count:");
Serial.print( t0);
Serial.print(" PauseDuration:");
Serial.print(t1 - t0);
Serial.print(" PulseDuration:");
Serial.print(t2 - t1);
Serial.print(" OverallDuration:");
Serial.print(t2 - t0);
Serial.println();
//Serial.println(pulseIn(7,HIGH));
delay(1000);
}
uint32_t timer0count(void) {
uint32_t count;
cli();
uint8_t reg = TCNT0;
count = (uint16_t)timer0_overflow_count;
sei();
// count = micros() ;//| 0*reg;
count = count << 8 | reg;
return count;
}
Wokwi UnoScope
https://wokwi.com/projects/390819301187622913