Author Topic: RGB Shield used to "output" range  (Read 6329 times)

Crump

  • Newbie
  • *
  • Posts: 5
RGB Shield used to "output" range
« on: February 04, 2013, 10:13:14 am »
I had an HC-SR04 Ultrasonic range detector laying around and wanted to do something fun with it and my new digi spark. I set it up so that as objects became closer to the HC-SR04 the RGB would go from Green to red. Still working out some issues for timing (the code that I had rock solid when set up again an atmega328p at 16khz is a little prone to jump around when the object isn't even moving and tends to go out of range at about 40cm (when it goes out of range the RGB goes blue to show that it isn't detecting anything.


« Last Edit: February 05, 2013, 07:03:50 am by Crump »

Crump

  • Newbie
  • *
  • Posts: 5
Re: RGB Shield used to "output" range
« Reply #1 on: February 04, 2013, 07:36:37 pm »

Here is the code that works fairly well now. I need to work on the delays to better refine it for further distances.

Code: [Select]
/*
HC-S04 on a DigiSpark with and RGB Shield
Author: Chris Crumpacker
Date: January 2013


Copyright (c) 2013 Chris Crumpacker.  All right reserved.


This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.


This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
                                                           
Sketch Notes: The HC-S04 Ultrasonic detector is attached to Pins 3 and 5.
The RGB shield is set to use pins 0, 1, and 4 for PWM.
As something comes closer to the detector the RGB goes from green to red.
 */


#define trigPin 3
#define echoPin 5
byte RED = 0;
byte GREEN = 1;
byte BLUE = 4;
int greenValue = 0;
int redValue = 0;
long distanceMax = 40;
long distanceMin = 0;


void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  //sonicCalibrate();
}


//Function to check the distance in centimeters, This explains the theory here: http://trollmaker.com/article3/arduino-and-hc-sr04-ultrasonic-sensor
long sonicDistance(){
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);                                    //Ping
  delayMicroseconds(1000);                                        //Need to futs with this value now to make it work better on digispark
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);                              //Boop
 
  distance = (duration/2) / 29.1;


  return distance;
}


//Function for setting the color of the RGB LED depending on the distance
void setColor(long distance){
  digitalWrite(BLUE, LOW);                                        //Make sure the Blue LED is off
 
  greenValue = map(distance, distanceMin, distanceMax, 0, 255);   //Map the distance range to a range of 0 to 255 to the pwm value for the Green LED
  redValue = 255 - greenValue;                                    //Make the pwm value for the Red LED the oppisite of the Green LED so it fades
  analogWrite(GREEN, greenValue);
  analogWrite(RED, redValue);
}


void loop() {


  long distance = sonicDistance();
 
  if (distance >= distanceMax || distance <= distanceMin){
    //Out of range, turn the LED blue
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, LOW);
    analogWrite(BLUE, 100);
  }
  else {
    setColor(distance);
  }
  delay(2000);
}

Crump

  • Newbie
  • *
  • Posts: 5
Re: RGB Shield used to "output" range
« Reply #2 on: February 05, 2013, 06:28:03 am »
Here it is on a breadboard. there is more than enough space to put this all onto one prototype shield but I don't really have a practical use for this really just testing.

Far:



Medium (the light is yellow/orange but it shows up poorly no mater how i tried to shoot it (with my phone)):



Close, red = danger!!!! lol:
« Last Edit: February 05, 2013, 06:30:13 am by Crump »

Crump

  • Newbie
  • *
  • Posts: 5
Re: RGB Shield used to "output" range
« Reply #3 on: February 05, 2013, 09:47:08 am »
OK All done with the code. I was able to use the "NewPing" library from teckel http://arduino.cc/forum/index.php?topic=106043.0 I did have to remove the timer functions from the CPP (mainly I had no plans for them but they wouldn't compile against the digispark) With that here is my new code. it works GREAT, just don't power it from a computer's USB port. you need to not have anything on pin 4, so I just powered it directly with 5volts to the 5v pin :)


Here is a quick video of it working: http://www.youtube.com/watch?v=QNZyMQC3maQ


Code: [Select]
/*
HC-S04 on a DigiSpark with and RGB Shield
Author: Chris Crumpacker
Date: January 2013

Copyright (c) 2013 Chris Crumpacker.  All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
                                                           
Sketch Notes: The HC-S04 Ultrasonic detector is attached to Pins 3 and 5.
The RGB shield is set to use pins 0, 1, and 4 for PWM.
As something comes closer to the detector the RGB goes from green to red.

Using the NewPing library from Teckel http://arduino.cc/forum/index.php?topic=106043.0
I did have to remove the timer functions to get it working against the digispark.

Other than other the pins for PWM nothing here is DigiSpark specific
and would work on any arduino once the pics are updated */
 
#include <NewPing.h>

#define TRIGGER_PIN 3
#define ECHO_PIN 5
#define MAX_DISTANCE 200

byte RED = 0;
byte GREEN = 1;
byte BLUE = 4;
int greenValue = 0;
int redValue = 0;
long distanceMax = 60;
long distanceMin = 1;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

//Function for setting the color of the RGB LED depending on the distance
void setColor(long distance){
  digitalWrite(BLUE, LOW);                                        //Make sure the Blue LED is off
 
  greenValue = map(distance, distanceMin, distanceMax, 0, 255);   //Map the distance range to a range of 0 to 255 to the pwm value for the Green LED
  redValue = 255 - greenValue;                                    //Make the pwm value for the Red LED the oppisite of the Green LED so it fades
  analogWrite(GREEN, greenValue);
  analogWrite(RED, redValue);
}

void loop() {

  long distance = sonar.ping_cm();
 
  if (distance >= distanceMax || distance < distanceMin){
    //Out of range, turn the LED blue
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, LOW);   
    digitalWrite(BLUE, HIGH);
  }
  else {
    setColor(distance);
  }
  delay(500);
}
« Last Edit: February 05, 2013, 10:23:31 am by Crump »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: RGB Shield used to "output" range
« Reply #4 on: February 05, 2013, 10:48:07 am »
Crump - thanks for sharing this! really cool and a great example of a rangefinder that anyone can understand.