const int LightPins[] = {4,5,6,7,8,9,10,11,12,13}; //These are the output pins for the CoolNeon shield const int Thresholds[] = {36,30,24,18}; // Distance thresholds in inches. A new light comes on when each threshold is reached const int LightPinCount = 10; //Number of outputs on the shield const int ThresholdCount = 4; //Number of lights we're actually using const int TriggerPin = 2; //set both of these to 2 if using a 3 wire proximity sensor const int EchoPin = 3; void setup() { Serial.begin(9600); delay(200); //Set all the light pins to output for(int i = 0 ; i < LightPinCount; i++ ) { pinMode(LightPins[i], OUTPUT); } //Start with the lights off LightsOff(); } void loop() { long Duration; long Inches; //Send out a ping pinMode(TriggerPin, OUTPUT); digitalWrite(TriggerPin, LOW); //According to the Ping example we should give a 2uS low for a clean high pulse delayMicroseconds(2); digitalWrite(TriggerPin, HIGH); delayMicroseconds(5); digitalWrite(TriggerPin, LOW); //Read the echo pinMode(EchoPin, INPUT); Duration = pulseIn(EchoPin, HIGH); Inches = msToInches(Duration); //Turn the appropriate light on SetLight(Inches); Serial.println(Inches); delay(500); } void SetLight(long Inches) { //This method turns on a single light based on the threshold. int MaxLight = -1; LightsOff(); for(int i = 0; i < ThresholdCount; i++) { if(Inches < Thresholds[i]) { MaxLight = i; } } if(MaxLight >= 0) digitalWrite(LightPins[MaxLight], LOW); } void SetLightCumulative(long Inches) { //This method allows multiple lights to be on at the same time. LightsOff(); for(int i = 0; i < ThresholdCount; i++) { if (Inches < Thresholds[i]) { Serial.print("Threshold: "); Serial.println(i); digitalWrite(LightPins[i], LOW); //Turn on the light if inches is within its threshold } } } void LightsOff() { for(int i = 0; i < LightPinCount; i++) { digitalWrite(LightPins[i], HIGH); } } long msToInches(long microseconds) { return microseconds / 148; }