OK, so I just got my arduino from robotshop.ca/arduino-experimentation-kit-hs422-gp2y0a02yk-3.html and I've started playing around. I really don't know what I'm doing but I took some code from different places and mashed it all together, but I'm not sure how to code the last part.
Most of the code came from the bar graph example, but here it is:
// these constants won't change:
const int IRpin = 1; // the pin that the potentiometer is attached to
const int ledCount = 6; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7 }; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
Serial.begin(9600); // start the serial port
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance);
delay(100);
// I know this isn't right.
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (distance > 30) {
digitalWrite(ledPins[thisLed], HIGH);
}
if (distance > 40) {
digitalWrite(ledPins[thisLed], HIGH);
}
if(distance > 50) {
digitalWrite(ledPins[thisLed], HIGH);
}
if(distance > 60) {
digitalWrite(ledPins[thisLed], HIGH);
}
if(distance > 70) {
digitalWrite(ledPins[thisLed], HIGH);
}
if(distance > 80) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
ok, I got it to work now, I guess all I had to do was actually look at it.
Here's the updated code:
// these constants won't change:
const int IRpin = 1; // the pin that the potentiometer is attached to
const int ledCount = 6; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7 }; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
Serial.begin(9600); // start the serial port
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
/* // read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
*/
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance);
delay(100);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (distance > 30) {
digitalWrite(ledPins[0], HIGH);
}
if (distance > 40) {
digitalWrite(ledPins[1], HIGH);
}
if(distance > 50) {
digitalWrite(ledPins[2], HIGH);
}
if(distance > 60) {
digitalWrite(ledPins[3], HIGH);
}
if(distance > 70) {
digitalWrite(ledPins[4], HIGH);
}
if(distance > 80) {
digitalWrite(ledPins[5], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
Now I just need to edit some stuff to make it better. Thanks for the help ;D