I have been trying to use an LDR to input data into Unity 3D so I can trigger a random selection from voice clips in Unity but I'm not having much luck. I was following this video here but I think I am missing something as I can't get it to work the way I would like.
Basically I am trying to make it so that when the LDR reaches a certain level of darkness that in Unity it will call a function to play an audio clip from an array of random clips.
But I think I am missing something between what the code in his demo video uses VS the code I currently have.
If anyone more familiar with the Arduino Code and Unity can help that would be great.
Here is my Arduino code that I am using.
int lightPin = 0; //Define Pin For Photoresistor
int lightInt = 0;
const byte rLed = 12; //Sets Pin number LED is conneted too
const byte yLed = 11;
const byte gLed = 10;
const byte bLed = 9;
const byte bLed2 = 8;
char myChar; //changed the name of this variable because they are not all colurs now
const byte pulsePins[] = {13, 7}; //pins for a pulse output
char pulseTriggers[] = {'p', 'q'};
const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
unsigned long pulseLength = 1000;
void setup()
{
//Serial.begin (9600);
Serial.begin (115200);
pinMode(rLed, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(bLed, OUTPUT);
pinMode(bLed2, OUTPUT);
digitalWrite(rLed, LOW);
digitalWrite(yLed, LOW);
digitalWrite(gLed, LOW);
digitalWrite(bLed, LOW);
digitalWrite(bLed2, LOW);
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
pinMode(pulsePins[p], OUTPUT);
digitalWrite(pulsePins[p], LOW);
}
}
void loop()
{
if (Serial.available()) //if serial data is available
{
//Light Sensor
if((lightInt - analogRead(lightPin)) > 10 || (lightInt - analogRead(lightPin)) < -10){
lightInt = analogRead(lightPin);
Serial.println(lightInt);
}
int lf = 10;
myChar = Serial.read(); //read one character from serial
if (myChar == 'r') //if it is an r
{
digitalWrite(rLed, !digitalRead(rLed)); //change the state of the r LED
}
if (myChar == 'b')
{
digitalWrite(bLed, !digitalRead(bLed));
}
if (myChar == 'y')
{
digitalWrite(yLed, !digitalRead(yLed));
}
if (myChar == 'g')
{
digitalWrite(gLed, !digitalRead(gLed));
}
if (myChar == '1')
{
digitalWrite(bLed2, !digitalRead(bLed2));
}
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
if (myChar == pulseTriggers[p])
{
pulseStarts[p] = millis(); //save the time of receipt
digitalWrite(pulsePins[p], HIGH);
}
}
}
//the following code runs each time through loop()
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
if (millis() - pulseStarts[p] >= pulseLength) //has the pin been HIGH long enough ?
{
digitalWrite(pulsePins[p], LOW); //take the pulse pin LOW
}
}
}