ANDADOR – CODE v2.0

// Adafruit NeoPixel - Version: Latest 

#include <Adafruit_NeoPixel.h>

#define trig1Pin 2
#define trig2Pin 4
#define echo1Pin 3
#define echo2Pin 5
#define PIN 14
#define LED_COUNT 3

Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin (9600);
  pinMode(trig1Pin, OUTPUT);
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo1Pin, INPUT);
  pinMode(echo2Pin, INPUT);
  leds.begin();
}

void loop() {
  long distance1, distance2, color1, color2;
  distance1=callSensor(trig1Pin, echo1Pin);
  distance2=callSensor(trig2Pin, echo2Pin);

  color1 = checkDistance(distance1);
  color2 = checkDistance(distance2);

  if(color1 > color2) {
    pixelColor(color1);
  }
  else {
    pixelColor(color2);
  }

  Serial.print(distance1);
  Serial.println(" cm");
  Serial.println(" - ");
  Serial.print(distance2);
  Serial.println(" cm");

  delay(500);
}

int callSensor(int writePin, int readPin){
  long duration, distance;
  digitalWrite(writePin, LOW);// Added this line
  delayMicroseconds(10); // Added this line
  digitalWrite(writePin, HIGH);
  delayMicroseconds(10);
  digitalWrite(writePin, LOW);
  duration = pulseIn(readPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

long checkDistance(long distance){
  long color;
  switch (distance) {
  case 1 ... 40:
    color = 0xFF0000;
    break;
  case 41 ... 70:
    color = 0xFFFB00;
    break;
  default:
    color = 00000000;
    break;
  }
  return color;
}

void pixelColor(long color){
  int i;
  for(int i=0;i<LED_COUNT;i++){
    leds.setPixelColor(i, color); 
    leds.show(); 
    delay(50); 
  }
}

 

ANDADOR – Code v1.0

// Versión 1.0 - 20170828

#include <Adafruit_NeoPixel.h>
#define trig1Pin 2
#define trig2Pin 4
#define echo1Pin 3
#define echo2Pin 5

#define PIN 14
#define LED_COUNT 3

Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  //Serial.begin (9600);
  pinMode(trig1Pin, OUTPUT);
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo1Pin, INPUT);
  pinMode(echo2Pin, INPUT);
  leds.begin();
}

void loop() {
  long distance1, distance2, color1, color2;
  distance1=callSensor(trig1Pin, echo1Pin);
  distance2=callSensor(trig2Pin, echo2Pin);

  color1 = checkDistance(distance1);
  color2 = checkDistance(distance2);

  if(color1 > color2) {
    pixelColor(color1);
  }
  else {
    pixelColor(color2);
  }
  /*
  Serial.print(distance1);
  Serial.println(" cm");
  Serial.println(" - ");
  Serial.print(distance2);
  Serial.println(" cm");
  */
  delay(600);
}

int callSensor(int writePin, int readPin){
  long duration, distance;
  digitalWrite(writePin, LOW);// Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(writePin, HIGH);
  delayMicroseconds(20);
  digitalWrite(writePin, LOW);
  duration = pulseIn(readPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

long checkDistance(long distance){
  long color;
  switch (distance) {
  case 1 ... 25:
    color = 0xFF0000;
    break;
  case 26 ... 50:
    color = 0xFFFB00;
    break;
  default:
    color = 00000000;
    break;
  }
  return color;
}

void pixelColor(long color){
  int i;
  for(int i=0;i<LED_COUNT;i++){
    leds.setPixelColor(i, color); 
    leds.show(); 
    delay(50); 
  }
}