code backwards and soldered circuit no longer works

I built a circuit with an optical sensor on the solderless breadboard on my protoshield, the code I wrote was working perfectly, but then when I set it up again today it was backwards. It is supposed to measure the time duration of the optical sensor being blocked, now it measures the time the optical sensor is not blocked. I was not aware of altering any of the code. Code is as follows:

boolean gate_hit = false; //gate is not hit
unsigned long time_1;
unsigned long time_2;
boolean time_1_stored = false; // time_1 is not yet stored
boolean time_2_stored = false; // time_2 is not yet stored

void setup ()
{
int sensor_pin = 1; // yellow jumper connected to interrupt 0 on pin 3
Serial.begin(9600);
attachInterrupt(sensor_pin, gate_handler, CHANGE); //pin=sensor_pin, function=gate_handler, state=CHANGE
}

void gate_handler () //activates when interrupt pin 1 is changes state. aka, sensor is blocked or released.
{
unsigned long time;
time = millis(); // start to record time
if (!gate_hit) // if gate is hit...
{
time_1 = time; // record time
time_1_stored = true;
}
else // if gate is released...
{
time_2 = time; // record time
time_2_stored = true;
}
gate_hit =! gate_hit; // resets gate_hit
}
void loop ()
{
if (time_1_stored && time_2_stored)
{
long sensorDiff = 0;
sensorDiff = time_2 - time_1; // measures the time druing between hit and release
Serial.print(time_1);
Serial.print(" ");
Serial.print(time_2);
Serial.print(" ");
Serial.println(sensorDiff);
time_1_stored = false; // resets time_1 and _2 back to false
time_2_stored = false;
}
}

Also, I soldered the circuit onto a printed breadboard and it now spits out random numbers while the optical sensor is blocked... I thought there was a short so I desoldered and resoldered the board to make sure everything was correct. I'm new to all this, so I know I did something wrong I just can't figure it out...

Thanks for your help!

optical_speed_trap_CHANGE_good.pde (1.41 KB)

It looks to me like you're defining the startup state of the sensor gate in your code and not getting it from the sensor.

So the state of the sensor at startup will define whether it measures blocked or unblocked.

You should get the initial state from the sensor at startup and/or confirm it in the interrupt routine.

As for the random values on your soldered circuit. Does it need a pulldown on the sensor pin? Exactly what you need to do would probably depend on the sensor.

Have you tried the "volatile" keyword?

I built a circuit with an optical sensor on the solderless breadboard on my protoshield,

So how come the title of the post says "soldered circuit"?
Which do you have?
It sounds to me like you have swapped over the sensor and pull up resistor from now to when it worked before.

Grumpy_Mike:

I built a circuit with an optical sensor on the solderless breadboard on my protoshield,

So how come the title of the post says "soldered circuit"?
Which do you have?

If you read the post it says that the circuit was originally bread boarded and then later soldered. That's usually how people do things isn't it? :roll_eyes:

it says that the circuit was originally bread boarded and then later soldered. That's usually how people do things isn't it?

No, not necessarily; some of us just go straight to solder. 8)

some of us just go straight to solder

I do this often, and then wonder why I have amassed such a collection of Solderless Breadboards.

That's usually how people do things isn't it?

No it is not. They are the invention of the devil. Straight to solder. It seems only the solder-less system worked and when it was soldered up:-

I soldered the circuit onto a printed breadboard and it now spits out random numbers while the optical sensor is blocked... I thought there was a short so I desoldered and resoldered the board to make sure everything was correct.

So taking things apart and then putting them back together is the sort of stupid fault finding technique that using solderless bread board makes you do. In this case doing it with a soldered circuit is plain silly.

The soldered circuit didn't work because you wired it up wrong, simple as that. The random numbers bit implies that the input was floating.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
Even though that was not the intention that is what was wired up. That is what is happening here.

However, the alternative explanation is that the laws of physics suddenly got reversed one time due to negative energy flow caused by bad kama. I'm off to hug a tree. :cold_sweat:

Thanks for the help, I discovered that when I was soldering the sensors in, I was bending the wires inside the sensor; I think what happened was they were coming in contact -- causing a short. I slid some small-diameter heat shrink up around the sensor wires to protect them from my clumsy damage and this seems to be working better.