rotational motion detection using nano

Hello, I am working on my first arduino project, trying to detect rotational motion on a 3D Printer filament spool. I am using an arduino micro, and have basically set up a circuit with a pololu optical distance sensor that acts as a switch when triggered by a reflective material (white versus black). The code for that part is a the basic Digital Read Serial which I found here under the learning section, and have been able to test my distance sensor with.

My plan is to have a pie-like chart with "wedges" of alternating black and white which will be fastened to the filament spool which is supposed to be rotating. The white sections will reflect resulting in a "0", everything else is just a "1". When the filament spool is empty or a jam stops it from moving, I want to be able to notice that the signal doesn't switch from a high to a low (0 to 1 on the readout), and then activate a LED after a predetermined time, perhaps 5-10 seconds.

The part that I'm not sure about is setting up a timer to count when the change from high to low occurs, and consequently turn the LED on if the prescribed time elapses without it switching back.

If anybody has coded something like this or would have any ideas on how to do it I would really appreciate it.

Thanks, Jesse Kaiser

The part that I'm not sure about is setting up a timer to count when the change from high to low occurs, and consequently turn the LED on if the prescribed time elapses without it switching back.

Use the millis() timer. http://arduino.cc/en/Reference/Millis

When one transition occurs then make a note of the current time given by millis(). Then by subtracting the saved time from the stored time you get how long it has been in mS since the transition, use the unsigned long data type for these calculations.

To detect the change from white to black, you might have begun with an if statement like this one:

while ( PIND & (1<<6) == 0);  //waits until digital pin 6 changes from 0 to 1

As you have noticed, if the spool isn’t rotating, the Arduino gets stuck in the loop. So, add a check for elapsed time:

while ( PIND & (1<<6) == 0 && (millis() - start_time < time_out));

Of course, start_time and time_out must be suitably defined unsigned long integers before entering the loop.

Not sure if it will matter on this scale, but you may notice a speed change when your spool is almost empty vs full. The same feed rate will produce different rotational speeds depending on the current spool diameter. Your calibrations will need to reflect the slowest ie. full spool turning speed.

You should see a full-sized mig welding machine spool boogie when the roll is almost out!

Thanks for the input, still struggling with getting the code to do what I want. Where do I put the millis() function to make it count the elapsed time? Here is the code I am using to detect the motion with my sensor,

/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain. */

// digital pin 2 has a pushbutton attached to it. Give it a name: int pushButton = 2;

// the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // make the pushbutton's pin an input: pinMode(pushButton, INPUT); }

// the loop routine runs over and over again forever: void loop() { // read the input pin: int buttonState = digitalRead(pushButton); // print out the state of the button: Serial.println(buttonState); delay(1); // delay in between reads for stability }

This allows me to see the readout on the serial monitor if it is a one or a zero. I'm afraid I don't have enough coding knowledge to implement the while loop that would detect the time between transitions. If anybody has something similar already coded that I could try to run, could you post it so I can test it for my project?

thanks, Jesse Kaiser