python - How to interfacing -
this code use controlling led using ultrasound sensor (hc-sr04). when upload arduino, set red led on , green led off(default). furhtermore, if put in front of sensor in radius 20cm green led turn on , red led off. if take it, it'll set default.
#define trigpin 8 #define echopin 7 #define ledred 11 //red #define ledgreen 12 //green void setup() { serial.begin (9600); pinmode(trigpin, output); pinmode(echopin, input); pinmode(ledred, output); pinmode(ledgreen, output); } void loop() { long duration, distance; digitalwrite(trigpin, low); // added line delaymicroseconds(2); // added line digitalwrite(trigpin, high); // delaymicroseconds(1000); - removed line delaymicroseconds(10); // added line digitalwrite(trigpin, low); duration = pulsein(echopin, high); distance = (duration/2) / 29.1; //formula distance in cm //default led condition digitalwrite(ledred,high); digitalwrite(ledgreen,low); if (distance <20) { // led on/off happens digitalwrite(ledred,low); // when red condition met, green led should turn off digitalwrite(ledgreen,high); } serial.print(distance); delay(1000); }
need more help, how controll in python. using pyserial or pyfirmata library simply?
Comments
Post a Comment