shawnlee4 / u8glib

Automatically exported from code.google.com/p/u8glib
Other
0 stars 0 forks source link

oled 128x64_i2c with HC-SR04 sensor #317

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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
}

Original issue reported on code.google.com by creazy...@gmail.com on 5 Feb 2015 at 6:22

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

Original comment by olikr...@gmail.com on 5 Feb 2015 at 9:12

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

Original comment by creazy...@gmail.com on 6 Feb 2015 at 5:14

GoogleCodeExporter commented 8 years ago
ok, i will close this issue

Original comment by olikr...@gmail.com on 6 Feb 2015 at 5:43