abril 15, 2022

JLASER- Programa conjunto de la diana

Written by

Tras probar por separado los distintos dispositivos que se conectan en la diana, los conectamos todos y unimos los programas, haciendo que funcionen de forma consecutiva. Se nos plantea un problema con el motor paso a paso pues es incompatible con las patillas escogidas inicialmente, así que buscamos información en Internet al respecto y cambiamos las patillas a otras. Finalmente conectamos el driver a las patillas D5, D6, D7 y D8.

Luego se nos presenta otro problema con el motor, pues el programa individual funciona solo en el bloque loop() sin pausas, lo que nos complica su inserción en el conjunto. Tras buscar de nuevo información y alternativas, cambiamos el programa de control del motor y lo metemos dentro de un bucle for, aunque también nos da problemas con las repeticiones pues se reinicia el ESP. Finalmente, y al menos de prueba, damos con el número de repeticiones necesario para que funcione y más adelante lo adecuaremos al programa definitivo.

El código es:

#include <Wire.h>
#include <Servo.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>
#include <AccelStepper.h>
#include "GUN_PITCHES.h"

#define HALFSTEP 8

// SDA=GPIO 4 / SCL=GPIO 5
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_1X);

//TUNES MARIO/DOOM
int tempo = 200; //MUSIC SPEED
int marioNotes = sizeof(MARIO_TONE) / sizeof(MARIO_TONE[0]) / 2;
int dooomnotes = sizeof(DOOM_TONE) / sizeof(DOOM_TONE[0]) / 2;
int whole_note = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;

// Patillas usadas
const int audioPin = 2;       // PIEZO PIN - patilla D4
const int ledPin = 0;         // NEOPIXEL PIN - patilla D3
const int servoPin = 16;       // SERVO PIN - patilla D0

// ULN2003 Motor Driver Pins
#define IN1 D8
#define IN2 D7
#define IN3 D6
#define IN4 D5

const int ledCount = 24;      // NEOPIXEL COUNT
int targetHit=1;
Adafruit_NeoPixel neoLights(ledCount, ledPin, NEO_GRB + NEO_KHZ800);

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution

// initialize the stepper library
AccelStepper stepper(HALFSTEP,  IN1, IN3, IN2, IN4);

//////////////////////////////////////////
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

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

  myservo.attach(servoPin);  // patilla D0

  pinMode(ledPin, OUTPUT);
  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);
  colorWipe_B(neoLights.Color(0,   0,   255), 10);
  colorWipe_B(neoLights.Color(0,   0,   0), 10); // CLEARING LEDS

  // set the speed and acceleration
  stepper.setMaxSpeed(500);
  stepper.setAcceleration(100);
  stepper.setSpeed(500);  
}

void loop() 
{
  uint16_t r, g, b, c, colorTemp, lux;

  Serial.println("Mario");
  START_TUNE(MARIO_TONE, marioNotes ); //PLAY MARIO TUNE

  Serial.println("Pi pi");
    tone(audioPin, PEWPEW_TUNE[0], 100);
    delay(2000);
    noTone(audioPin);

  Serial.println("Servo");
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }

  Serial.println("RGB");
  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.println(" ");

  Serial.println("Servo");
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }

  Serial.println("LED");
  colorWipe(neoLights.Color(255,   0,  0), 10, targetHit);
  delay(1000);
  colorWipe(neoLights.Color(0,   0,  255), 10, targetHit);
  delay(1000);
    for (int i = 0; i <= 2; 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);
    }
    targetHit++;
    if (targetHit>10) targetHit=1;

  Serial.println("motor");

  // check current stepper motor position to invert direction
  for(r = 0; r<=200; r++)
  {
   for (g = 0; g<10000; g++)
   {
    stepper.runSpeed();
   }
  }

}

//PLAY MUSIC
void START_TUNE(int tune[], int notesSize) {
  for (int thisNote = 0; thisNote < notesSize * 2; thisNote = thisNote + 2) {
    divider =  tune[thisNote + 1];
    if (divider > 0) {
      noteDuration = (whole_note) / divider;
    } else if (divider < 0) {
      noteDuration = (whole_note) / abs(divider);
      noteDuration *= 1.5;
    }
    tone(audioPin, tune[thisNote], noteDuration * 0.9);
    delay(noteDuration);
    noTone(audioPin);
  }
}

//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);
  }
}

Y a continuación un vídeo demostrando el funcionamiento. El sensor RGB no se puede comprobar aquí pero en el monitor serie funciona a cada paso.

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