The code is finally working logically as far as I can tell. This was much more difficult than my underestimate of how-hard-can-it-be. It took a good while just planning out the action logic and figuring out coefficients to calibrate the photosensor inputs was another unexpected roadblock that backed up traffic for miles. I got it to the point where it will theoretically chase light. I haven’t added other parts of brain to it yet, since sadly, I can not physically test it beyond this computer screen due to shortcomings on the component purchasing side of things… In my defense, every subsystem has one key component missing, all of which were bafflingly elusive.
And without further adieu, my attempt at futility.
int LightL = 4; // input analog pin - photoresistor [left]
int LightR = 5; // input analog pin - photoresistor [right]
int LightB = 2; // input analog pin - photoresistor [back]
int Prox = 3; // input analog pin - ultrasonic transducer (proximity)
int MotorL1 = 6; // output digital pin - motor [left] - direction 1
int MotorL2 = 7; // output digital pin - motor [left] - direction 2
int MotorL = 10; // output analog pin - motor [left] - speed
int MotorR1 = 8; // output digital pin - motor [right] - direction 1
int MotorR2 = 9; // output digital pin - motor [right] - direction 2
int MotorR = 11; // output analog pin - motor [right] - speed
int Lin; // input - photoresistor [left]
int Rin; // input - photoresistor [right] * constant
int Bin; // input - photoresistor [back] * constant
long Lout; // output - motor [left]
long Rout; // output - motor [right]
int LightMin = 400; // light input MIN for motion
int LightMax = 900; // light input MAX for motion
int LightShoot = 700; // light input threshold to start shooting
int SpeedLow = 20; // motor output LOW
int SpeedHigh = 200; // motor output HIGH
int msecond = 0;
void setup()
{
Serial.begin(9600);
pinMode(LightL, INPUT);
pinMode(LightR, INPUT);
pinMode(LightB, INPUT);
pinMode(MotorL, OUTPUT);
pinMode(MotorL1, OUTPUT);
pinMode(MotorL2, OUTPUT);
pinMode(MotorR, OUTPUT);
pinMode(MotorR1, OUTPUT);
pinMode(MotorR2, OUTPUT);
analogWrite(MotorL, 0);
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, HIGH);
analogWrite(MotorR, 0);
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, HIGH);
}
void loop()
{
Lin = analogRead(LightL);
Rin = analogRead(LightR) *1.1; // 1.26
Bin = analogRead(LightB) *1.8;
if(Bin>Lin+100 && Bin>Rin+100) { /*turn around*/ }
if(LinLightMax) {Lout=255;}
else if(Rin>LightMin)
{ // Lout = (SpeedHigh-SpeedLow)*(Lin-LightMin)/(LightMax - LightMin)+SpeedLow
Lout = SpeedHigh-SpeedLow;
Lout *= Lin-LightMin;
Lout /= LightMax - LightMin;
Lout += SpeedLow;
}
}
if(Lin>LightMax)
{
Lout=0;
if(Rin>LightMin && Rin<LightMax)
{ // Rout = (SpeedHigh-SpeedLow)*[1-(Rin-LightMin)/(LightMax - LightMin)]+SpeedLow
Rout = SpeedHigh-SpeedLow;
Rout *= LightMax - Rin;
Rout /= LightMax - LightMin;
Rout += SpeedLow;
}
}
if(RinLightMax) {Rout=255;}
else if(Lin>LightMin)
{
Rout = SpeedHigh-SpeedLow;
Rout *= Rin-LightMin;
Rout /= LightMax - LightMin;
Rout += SpeedLow;
}
}
if(Rin>LightMax)
{
Rout=0;
if(Lin>LightMin && LinLightMin && LinLightMin && RinLightShoot && Rin>LightShoot && abs(Lin-Rin)<30)
{
/* shoot! */
}
analogWrite(MotorL, Lout);
analogWrite(MotorR, Rout);
msecond = millis()/1000;
if(millis()-msecond*1000 == 0) // display every second
{
Serial.print("L:i");
Serial.print(Lin);
Serial.print("-o");
Serial.print(Lout);
Serial.print(" R:i");
Serial.print(Rin);
Serial.print("-o");
Serial.print(Rout);
Serial.print(" B:");
Serial.println(Bin);
// Serial.println(analogRead(Prox));
}
}