/* Made by Jefferson Sandoval during the Embedded programming for the FabAcademy2021 * * This is just a simple code in Arduino for testing a HC-SR04 connected to my own * board, MyMiniBoard_X14, based on the ATtiny1614. * The code takes the position of the Joystick and converts it to a percentage value. * * Documentation: http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week11/ */ //Declare variables linked to the pins where the ultrasonic is connected. const int Trigger = 0; const int Echo = 1; void setup() { Serial.begin(9600); //initialize serial communication //Ultrasonic Sensor setup pinMode(Trigger, OUTPUT); //Set Trigger as Output pinMode(Echo, INPUT); //Set Trigger as Input digitalWrite(Trigger, LOW); //keep Trigger off, it'll be activated during the process (in the loop) } void loop() { long t; //echo width long d; //distance in cm //Send 10us pulses to the Trigger digitalWrite(Trigger, HIGH); delayMicroseconds(10); digitalWrite(Trigger, LOW); t = pulseIn(Echo, HIGH); //get the pulse width d = t/59; //get distance in cm //Print distance value on Serial monitor Serial.print("Distance: "); Serial.print(d); Serial.println("cm"); delay (100); //Wait for 0.1 seconds }