The results of the experiment were not what he hypothesized - that putting weight at the back would make it go faster (based on pinewood derby experience) - in fact that was the slowest. He is still mulling over his findings and conclusions. I provided "engineering" support, but I'm letting him do the "science" on his own. Regardless, he is thinking and learning, so I consider it a huge success.
Here's the code. Feedback welcome - it works, but there might be better ways to do it. I think it's pretty clean, but there might be superfluous fragments of trial and error code hanging around - don't let that confuse you if so.
// CONSTANTS
// state
const int stateStandby = 0;
const int stateError = -1;
const int stateReady = 1;
const int stateTiming = 2;
const int stateFinished = 3;
const int DARK = 0;
const int LIGHT = 1;
// SKETCH VARIABLES
//analog pins
int apinPr1 = 0;
int apinPr2 = 1;
int apinPot1a = 2;
int apinPot2a = 4;
//digital pins
int pinBtnInit = 2; // pin for the initialize button
int pinLedPr1 = 13;
int pinLedPr2 = 12;
//process control variables
int state;
unsigned long timeStart;
unsigned long timeStop;
unsigned long time;
void setup()
{
pinMode(pinBtnInit, INPUT);
pinMode(pinLedPr1, OUTPUT);
pinMode(pinLedPr2, OUTPUT);
state = stateStandby;
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Initialized");
}
void loop()
{
// read the photoresistors
int pr1 = readPhotoresistor(apinPr1, apinPot1a, pinLedPr1);
int pr2 = readPhotoresistor(apinPr2, apinPot2a, pinLedPr2);
// check the state of the pushbutton
int btnState = digitalRead(pinBtnInit);
// reset the state to Ready when the button is pushed
// (don't reset if it is already Ready just to avoid cluttering the output stream)
if (btnState == LOW) {
if (pr1==LIGHT && pr2==LIGHT) {
if (state!=stateReady) {
state = stateReady;
Serial.println("State = Ready");
}
}
else {
if (state!=stateError) {
state = stateError;
Serial.println("State = Error: Not Ready");
}
}
}
// if Ready, then check the photoresistors and record time
if (state==stateReady) {
if (pr2==DARK) {
state=stateError;
Serial.println("State = Error: PR2 out of sequence");
}
else {
if (pr1==DARK) {
state=stateTiming;
timeStart = millis();
Serial.println("State = Timing");
}
}
}
// control timer
if (state==stateTiming && pr2==DARK) {
state=stateFinished;
timeStop = millis();
time = timeStop - timeStart;
Serial.print("State = Finished, time = ");
Serial.print(time);
Serial.print(" milliseconds");
Serial.println();
}
}
int readPhotoresistor(int pinLight, int pinPot, int pinLED) {
int lightLevel = analogRead(pinLight); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 1023, 0, 255);
//adjust the value 0 to 1023 to span 0 to 255
lightLevel = 255 - lightLevel; // reverse the value to make it more intuitive
int potBoost = analogRead(pinPot);
potBoost = map(potBoost, 0, 1023, -100, 100); // arbitrary range - trial and error
lightLevel = lightLevel + potBoost;
// arbitrary light level - trial and error
if (lightLevel >= 200) {
digitalWrite(pinLED, HIGH);
return LIGHT;
}
else {
digitalWrite(pinLED, LOW);
return DARK;
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.