Hello, I've currently got a small system within my lego railway where I reset some signals using LDRs in the track. The system consists of a PCA9685 Servo driver hooked up the the LEDs, an IR receiver to control a secondary signal and 3 LDRs (1 per signal). Independently the code seems to work but the LDR detection within the main program doesn't seem to be picking up on any values. It's not a hardware fault as the LDRs work on a different program that just detects the resistance based on the light level so I'm guessing it's a coding error I've missed. Anyone any ideas?
Rather annoyingly TinkerCad does not have the PCA board as a component, but that is operating fine.
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
#include "IRremote.h"
Adafruit_PWMServoDriver PCA = Adafruit_PWMServoDriver(0x40, Wire);
String remoteInput = "0";
String SignalList[3];
int IRpin = 9;
IRrecv IR(IRpin);
decode_results cmd;
int bayStatus = 0;
//For PCA: A4 --> SCL, A5 --> SDA
class Signal{
public:
int RedPin;
int Yellow1Pin;
int GreenPin;
int Yellow2Pin;
int readPin;
int Status = 0;
int stepCount = 0;
int a = 1;
int stepUpVal = 100;
bool setReset = true;
int ambientValue;
int currentValue;
Signal(int Red, int Yellow1, int Green, int Yellow2, int readPin) {
RedPin = Red;
Yellow1Pin = Yellow1;
GreenPin = Green;
Yellow2Pin = Yellow2;
readPin = readPin;
}
int lightSet(){
int i = 0;
ambientValue = 0;
for (i; i < 40; i = i + 1){
//currentValue = analogRead(readPin);
ambientValue = ambientValue + currentValue;
delay(10);
}
ambientValue = ambientValue / i;
if (ambientValue < 0){
ambientValue = 800;
}
//Serial.println(ambientValue);
Serial.println("");
}
void lightDetect(){
stepCount = stepCount + a;
currentValue = analogRead(readPin);
Serial.println(currentValue);
if (currentValue < ambientValue *0.55){
Status = 1;
stepCount = 0;
setReset = true;
}
if (stepCount == stepUpVal){
stepUp();
stepCount = 0;
}
}
void stepUp(){
Status = Status + 1;
if (Status == 5){
Status = 4;
stepCount = 0;
}
if (Status == 1){
PCA.setPWM(RedPin, 0, 4095);
PCA.setPWM(Yellow1Pin, 0, 0);
PCA.setPWM(GreenPin, 0, 0);
PCA.setPWM(Yellow2Pin, 0, 0);
}
if (Status == 2){
PCA.setPWM(RedPin, 0, 0);
PCA.setPWM(Yellow1Pin, 0, 4095);
PCA.setPWM(GreenPin, 0, 0);
PCA.setPWM(Yellow2Pin, 0, 0);
}
if (Status == 3){
PCA.setPWM(RedPin, 0, 0);
PCA.setPWM(Yellow1Pin, 0, 4095);
PCA.setPWM(GreenPin, 0, 0);
PCA.setPWM(Yellow2Pin, 0, 4095);
}
if (Status == 4){
PCA.setPWM(RedPin, 0, 0);
PCA.setPWM(Yellow1Pin, 0, 0);
PCA.setPWM(GreenPin, 0, 4095);
PCA.setPWM(Yellow2Pin, 0, 0);
}
}
};
Signal Ed1(0, 1, 2, 3, A3);
Signal Ed2(4, 5, 6, 7, A1);
Signal Ed4(8, 9, 10, 11, A2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(0);
PCA.begin();
PCA.setPWMFreq(1600);
IR.enableIRIn();
Ed1.lightSet();
Ed2.lightSet();
Ed4.lightSet();
Ed1.stepUp();
Ed2.stepUp();
Ed4.stepUp();
}
void loop() {
// put your main code here, to run repeatedly:
while (IR.decode(&cmd)==0) {
Ed1.lightDetect();
Ed2.lightDetect();
Ed4.lightDetect();
if (cmd.value == 0x6BC6597B) {
//Train into bay, remote 1
bayStatus = 1;
Ed1.setReset = false;
}
if (cmd.value == 0x735B797E) {
//Train out of bay, remote 2
bayStatus = 2;
}
if (bayStatus == 0){
Ed4.stepCount = 0;
Ed4.Status = 1;
}
if (bayStatus == 1){
Ed4.stepCount = 0;
Ed4.Status = 1;
if (Ed1.Status == 2){
Ed1.stepCount = 0;
Ed1.setReset = false;
bayStatus = 3;
}
}
if (bayStatus == 3){
Ed1.stepCount = 0;
if (Ed1.setReset == true){
bayStatus = 0;
}
}
if (bayStatus == 2){
if (Ed1.Status != 1 || Ed2.Status != 1){
Ed4.stepCount = 0;
Ed4.Status = 1;
}
if (Ed1.Status == 1){
Ed1.stepCount = 0;
}
if (Ed2.Status == 1){
Ed2.stepCount = 0;
}
if (Ed1.Status == 1 && Ed2.Status == 1){
delay(10000);
Ed4.stepUp();
Ed4.setReset = false;
bayStatus = 4;
}
}
if (bayStatus == 4){
if(Ed1.setReset == true){
bayStatus = 0;
}
}
delay(100);
Serial.println(Ed1.currentValue);
}
IR.resume();
}
