// [4:36 PM]Dominion:
const int trig = 3;
const int echo = 2;
int duration = 0;
int distance = 0;
int cm = 0;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
cm = (duration/2) / 29.1;
Serial.print(cm); Serial.print(" ");
delay(250);
}
// can someone tell me why this code is working in tinkercad but not with my hardware
// [4:36 PM]Dominion: i get a 0 in serial monitor
// [4:40 PM]nis: cm isn't a float .. you write to trig twice with no effect the second time you do so.
// [4:41 PM]nis: Try it on wokwi instea