mayo 21, 2021

JLASER- Circuito de diana con sensor RGB

Written by

Simplemente sustituimos el sensor LDR por uno RGB del tipo TCS34725. Ya lo hemos probado aparte, y el montaje lo hacemos similar al anterior:

Circuito de la diana con sensor RGB

Ahora probamos el circuito montado con la pistola roja. El programa que hemos modificado es el siguiente:

/*
  EASY LASER GUN BY ARNAUD ATCHIMON @NEOFUTURISM.NET

  DO NOT SHOOT YOUR EYES OUT!! PLEASE :)
  Using Photocell_Example.ino from   (https://www.sparkfun.com/products/9088) Jim Lindblom @ SparkFun Electronics
  LIBRARY NEEDED:
  I'm using the fanstastic servo library from PaulStoffregen @ https://github.com/PaulStoffregen/PWMServo

   CONNECTION
   ----------
   NANO
   PIN 5 // BUTTON PIN
   PIN 6 // NEOPIXEL PIN
   PIN 9  // SERVO PIN
   PIN A0 // LDR PIN -- 1OKOHM RESISTOR TO GROUND
*/

#include <Adafruit_NeoPixel.h>
#include <PWMServo.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "TARGET_PITCHES.h"

#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

const int btnMode = 5;        //TRIGGER BTN
const int ledPin = 6;         //NEOPIXEL PIN
const int ledCount = 24;      //NEOPIXEL COUNT
const int piezoPin = 8;       // SOUND PIN
const int servoPin = 9;       // SERVO PIN
const int ONBOARD_PIN = 13;   // ONBOARD LED

Adafruit_NeoPixel neoLights(ledCount, ledPin, NEO_GRB + NEO_KHZ800);
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_1X);

PWMServo MYSERVO;

int MELODY[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

bool basePosition = false;
int servoPos = 0;           // SERVO POSITION
int minServoPos = 100;
int maxServoPos = 20;

int targetHit = 0;          // TARGET COUNT
int targetLight = 0;
float lightHit = 4;         // NEOPIXE RING (24 / 4) = 6 SHOTS TO FINISH GAME
int modeBtn;
int modeSelect = 0;


// LDR VALUES YOU DO NOT NEED TO MODIFY THIS, CONNECT A 10K RESISTOR BETWEEEN GROUND AND A0
const float VCC = 4.98;
const float r_div = 4660.0;
const float DARK_THRESHOLD = 10000.0;

void setup()
{
  Serial.begin(115200);

  pinMode(btnMode, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(piezoPin, OUTPUT);
  pinMode(ONBOARD_PIN, OUTPUT);

#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif

  neoLights.begin();
  neoLights.show();
  neoLights.setBrightness(100);
  
  //SERVO AND NEOPIXEL LOOP, IF THE SERVO GET STUCK HERE, PRESS RESET A FEW TIME, OR CHECK POWERING
  colorWipe_B(neoLights.Color(255,   0,   0), 10);
  MYSERVO.attach(servoPin);
  SERVO_INIT();
  //  SERVO_RST();
  colorWipe_B(neoLights.Color(0,   0,   255), 10);
  colorWipe_B(neoLights.Color(0,   0,   0), 10); // CLEARING LEDS

  // Activa el sensor RGB si está presente
  if (tcs.begin()) {
    Serial.println("TCS34725 activado");
  } 
  else 
  {
    Serial.println("Error: TCS34725 no encontrado");
    while (1);
  }
}


void loop()
{
  uint16_t r, g, b, c, colorTemp, lux;
  modeBtn = digitalRead(btnMode);

  tcs.getRawData(&r, &g, &b, &c);
  Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");

  // Comprueba botón y cambia modo de funcionamiento
  if (modeBtn == LOW) 
  {
    delay(250);
    modeSelect++;
    if (modeSelect == 1)
      SERVO_FLAT();
    else
      SERVO_INIT();
  }
  
  // MODE:: TABLE MODE VS WALL MODE
  if (modeSelect == 0)
  {
    //  POINT THE LASER TO THE LDR AND SET "lightHit" ACCORDINGLY
    if (r>200) 
    {
      HITMISS(minServoPos, maxServoPos);
    } 
    else noTone(piezoPin);
  }
  if (modeSelect == 1)
  {
    if (r>200) 
    {
      HITMISS(maxServoPos, minServoPos);
    } 
    else noTone(piezoPin);
  }
  if (modeSelect >= 2) 
    modeSelect = 0;
  if (targetHit < ledCount / 2)
    targetLight = 0;
  else if (targetHit >= 12 && targetHit < ledCount)
    targetLight = 1;
  switch (targetLight) 
  {
    case 0:
      colorWipe(neoLights.Color(255,   0,  0), 10, targetHit);
      break;
    case 1:
      colorWipe(neoLights.Color(0,   0,  255), 10, targetHit);
      break;
  }

  if (targetHit >= ledCount) 
  {
    for (int i = 0; i <= 2; i++) 
    {
      //      i++;
      colorWipe_B(neoLights.Color(255,   0,  0), 15);
      colorWipe_B(neoLights.Color(0,   255,  0), 15);
      colorWipe_B(neoLights.Color(0,   0,  255), 15);
      colorWipe_B(neoLights.Color(  0,   0, 0), 5);
    }
    MYSERVO.detach();  // STOP THE SERVO BUZZ
    delay(500);
    targetHit = 0;
  }

  // threshold setting, turn the LED on.
  //  if (lightR >= DARK_THRESHOLD) {
  //    digitalWrite(ledPin, HIGH);
  //  } else {
  //    digitalWrite(ledPin, LOW);
  //  }

  Serial.println();
  Serial.print("TARGET ");
  Serial.println(targetHit);
  Serial.print("MODES ");
  Serial.println(modeSelect);
  delay(1);
}

// NEOPIXELLED SETUP
void colorWipe(uint32_t color, int wait, int colorNum) {
  for (int i = 0; i < colorNum; i++) {
    neoLights.setPixelColor(i, color);
    neoLights.show();
    delay(wait);
  }
}

void colorWipe_B(uint32_t color, int wait) {
  for (int i = 0; i < neoLights.numPixels(); i++) {
    neoLights.setPixelColor(i, color);
    neoLights.show();
    delay(wait);
  }
}

// SERVO SETUP
void SERVO_INIT() {
  SERVO_MOVE(minServoPos);
  delay(1000);
  SERVO_MOVE(maxServoPos);
  delay(1500);
  MYSERVO.detach();
}

void SERVO_FLAT() 
{
  SERVO_MOVE(maxServoPos);
  delay(1000);
  SERVO_MOVE(minServoPos);
  delay(500);
  MYSERVO.detach();
}

void SERVO_MOVE(int servoMove) 
{
  MYSERVO.attach(servoPin);
  MYSERVO.write(servoMove);
  //  delay(10);
}

// SERVO IS HIT
void HITMISS(int maxServo, int minServo) 
{
  tone(piezoPin, 1000, 500);
  targetHit = targetHit + 4;
  SERVO_MOVE(maxServo);
  colorWipe_B(neoLights.Color(0,   0,  255), 10);
  SERVO_MOVE(minServo);
  colorWipe_B(neoLights.Color(0,   0,  0), 10);
}

void SERVO_RST() 
{
  for (servoPos = 0; servoPos < minServoPos; servoPos += 10) { // goes from 0 degrees to 180 degrees, 1 degree steps
    MYSERVO.write(servoPos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (servoPos = minServoPos; servoPos >= 1; servoPos -= 10) { // goes from 180 degrees to 0 degrees
    MYSERVO.write(servoPos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  MYSERVO.detach();
}

Y el resultado se ve en el vídeo.

Category : JUEGO LÁSER

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Proudly powered by WordPress and Sweet Tech Theme