Well now the ultrasonic sensor suddenly started working

I'm not sure how but it may have something to do with what pins I chose.
Here is a photo of the full setup showing the humidity/Temp, Light and Ultrasonic sensors all working and displayed on the OLED.
https://www.dropbox.com/s/gvcr4m1h8ot6h ... 1.jpg?dl=0
Arduino pinouts are as follows:
Sensors
Ultrasonic
Echo--8
Trigger--9
Photocell
Output--A0
Humidity/Temp
Signal--1
OLED (As per the second example on Freetronics website.)
1--Vin
2--Gnd
6 --11
7--13
8--7
9--2
10--3
Here is the sketch I have made so far.
/*Multisensor readout to OLED display by Paul Coller
Arduino Eleven from Freetronics running Freetronics Humidity/Temperature sensor, light sensor and generic ultrasonic sensor(HC-SR04)
with readings displayed to Freetronics OLED display.
Modified from original sketch for various DHT humidity/temperature sensors that was
written by ladyada.
All coding in public domain.
*/
#include <SPI.h>
#include <SD.h>
#include <FTOLED.h>
#include <fonts/SystemFont5x7.h>
#include "DHT.h"
#define DHTPIN 1 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
const byte pin_cs = 7;
const byte pin_dc = 2;
const byte pin_reset = 3;
OLED oled(pin_cs, pin_dc, pin_reset);
OLED_TextBox line1(oled, 0, 64, 128, 64); //Text box for title
OLED_TextBox line2(oled, 0, 54, 128, 64); //Text box for humidity reading
OLED_TextBox line3(oled, 0, 44, 128, 64); //Text box for the temperature reading
OLED_TextBox line4(oled, 0, 34, 128, 64); //Text box for the light level reading
OLED_TextBox line5(oled, 0, 24, 128, 64); //Text box for the distance reading
int photocellPin = 0; //Analog pin for the photocell
int photocellReading;
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
dht.begin();
oled.begin();
oled.selectFont(SystemFont5x7);
line1.setForegroundColour(BLUE);
line1.setBackgroundColour(BLACK);
line1.println("Environmental Status");//Set the title
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
oled.selectFont(SystemFont5x7);
line2.setForegroundColour(RED);
line2.setBackgroundColour(BLACK);
line2.println("Sensor Error"); //Displays if sensor not connected
delay(500);
line2.reset();
line3.setForegroundColour(RED);
line3.setBackgroundColour(BLACK);
line3.println("Sensor Error"); //Displays if sensor not connected
delay(500);
line3.reset();
} else {
oled.selectFont(SystemFont5x7);
line2.setForegroundColour(GREEN);
line2.setBackgroundColour(BLACK);
line2.print("Humidity: ");
line2.print(h);
line2.print(" %\t");
line2.reset();
oled.selectFont(SystemFont5x7);
line3.setForegroundColour(YELLOW);
line3.setBackgroundColour(BLACK);
line3.print("Temperature: ");
line3.print(t);
line3.println(" *C");
line3.reset();
}
{
photocellReading = analogRead(photocellPin);
Serial.print(photocellReading); // the raw analog reading, displayed to the serial monitor. Change to line4.print if required.
// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Dark");
line4.reset();
} else if (photocellReading < 200) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Dim");
line4.reset();
} else if (photocellReading < 500) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Light");
line4.reset();
} else if (photocellReading < 800) {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-Bright");
line4.reset();
} else {
oled.selectFont(SystemFont5x7);
line4.setForegroundColour(ORANGE);
line4.setBackgroundColour(BLACK);
line4.print("Light level:");
line4.println("-V bright"); //Changed from "-Very bright" to "-V bright" do to limited space on display.
line4.reset();
}
delay(200);
}
{
delay(50);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
oled.selectFont(SystemFont5x7);
line5.setForegroundColour(PURPLE);
line5.setBackgroundColour(BLACK);
line5.print("Distance: ");
line5.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
line5.println("cm");
line5.reset();
}
}
Eventually I hope to include more sensors and then mount this all in a box as a sortof "Tricorder"
*Note: I had to shorten one of the light level values because it displayed on the the next line and wouldn't reset when the value changed on the OLED.
I am not sure how to fix this issue.
Cheers