olikraus / u8glib

Arduino Monochrom Graphics Library for LCDs and OLEDs
https://github.com/olikraus/u8glib/wiki
Other
1.25k stars 314 forks source link

oled 128x64_i2c with HC-SR04 sensor #316

Open olikraus opened 9 years ago

olikraus commented 9 years ago

Originally reported on Google Code with ID 317

hello 8) , i just wanna do a simple rangrefinder with a hc-sr04 and a tiny oled 128x64_i2c
but no sucess ....
i try to combine 2 sketchs, 1 for the ultrasonic sensor and 1 for the screen.

So on the screen there is "hello world" and if i check the serial monitor on my computeur
i can see the range finder working well...

But i wanna have the value of the sensor on the screen and i cannot find my way to
it...

thanks and sorry for my english (french guy )

#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("Hello World!");
}

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
}

Reported by creazyweb on 2015-02-05 06:22:43

olikraus commented 9 years ago
u8g.print is identical to Serial.print
So yu can use u8g.print(distance)

Reported by olikraus on 2015-02-05 21:12:58

olikraus commented 9 years ago
thank you so much for the advise!!!!!!!!it work noww :-)

Reported by creazyweb on 2015-02-06 05:14:50

olikraus commented 9 years ago
ok, i will close this issue

Reported by olikraus on 2015-02-06 05:43:41