Bonjours,
je souhaiterais afficher la valeur en cm récupérer avec le senseur hc-sr04 pour l'afficher sur l'ecran oled (en dessous de "distance")...
Mais rien ni fait
Quelqu'un aurait il une idée?
merci d'avance
ps : pour info a l'heure actuelle le code affiche " distance " sur l'ecran oled et on peut consulter le hs-sr04 a partir du moniteur serial
#include "U8glib.h"
int triggerPin = 7;
int echoPin = 8;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
u8g.print("Distance");
}
void setup(void) {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(void) {
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(500);
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
}
#include "U8glib.h"
int triggerPin = 7;
int echoPin = 8;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
void setup(void) {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void draw(void) {
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
char cm[5];
dtostrf(distance,5,1,cm);
// graphic commands to redraw the complete screen should be placed here
u8g.drawStr(4,24,"Distance:");
u8g.drawStr(76,24,cm);
}
void loop(void) {
// rebuild the picture after some delay
delay(500);
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
Je n'ai pas le matériel, je n'ai pas la librairie u8g et je ne sais pas si les positions dans la méthode drawstr correspondent à ton attente donc il faudra que tu adaptes.
Mais l'utilisation d'itoa devrait t'aider à résoudre ton problème.
Bon après multiple tentative infructueuse, enfin sa marche plus ou moins .La distance apparais bien a l’écran mais des pixel indésirable apparaisse en dessous bref plus cas affiner
le truc qui ma débloquer c'est quand j'ai compris que u8g.print c'est identique a serial.print!!!
En tout cas MERCI BEAUCOUP infobarquee et fdufnews c'est cool les gens sympa