I have a Ultrasonic (HCSR04) connected to a Raspberry PI 2015. I have the code for the Ultrasonic using C++ which I previously did in Arduino but I am having trouble translating it into Python code in order to use it in Raspberry PI. Could someone help me with this? Here are my code that are written in Arduino C++:
#include <math.h>
const int trigPin = 10;
const int echoPin = 9;
int g = 12; //green led
int r = 13; //red led
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
pinMode(g, OUTPUT);
pinMode(r, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin
delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
// Reads the echoPin, returns the ultrasound wave travel time in microsec
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Calculating the distance
// Prints the distance on the Serial Monitor
Serial.print ("Distance(cm): ");
Serial.println(distance);
delay(1000);
if(distance > 10)
{
digitalWrite(r, LOW);
digitalWrite(g, HIGH);
delay(250);
}
else
{
digitalWrite(g, LOW);
digitalWrite(r, HIGH);
delay(100);
}
}