0

I've created a very basic POV project.

It has a 12V DC motor with a piece of wood on it. The wood has 6 LEDs on it as well as a copper ring. The power is delivered to the LEDs from brushes connecting to the copper ring and from the shaft of the motor (the ground is attached to the base of the motor.) I'm using a TCRT5000L Sensor to count the RPM and have attached the LEDs to pin 13 and ground of the Arduino. (I've attached white tape to one side of the wood so that the sensor only counts one rev.)

I need help (please) with the program.

This is the code that I am using:

int pin = 13; 
volatile  int tcnt = 0; 
volatile  int lastcount = 0; 
volatile int state = LOW; 

void setup() { 
  Serial.begin(9600);
  pinMode(pin, OUTPUT); 
  attachInterrupt(1, blink, RISING); 
} 

void loop() { 
  Serial.println(tcnt); 
  if (tcnt != lastcount)
  {
     digitalWrite(pin, HIGH);
     delay(10);
     digitalWrite(pin, LOW);
     delay(10);
     lastcount = tcnt;
  }


   delay(10);

} 

void blink() { 
  tcnt = tcnt + 1; 
  state = !state; 
}

My first issue is that the when I use the code above, the lights don't seem to switch on / off on the exact same area. They are within 30 degrees but certainly not precise.

The second issue (and the biggest) is that I'm sure that this code is not the best way to do what I need. For example, if I wanted to use this as an analog clock, so that the lights would only show on the current hour / minute, how would I do this?

Any help is greatly appreciated.

Side View

Top View

2
  • If you rotate the arm by hand and observe the position sensor signal does it come up rapidly and cleanly almost independent of speed of arm motion? The sensor may be near its limit and take a finite time to transition when reflection is detected. If so,more LED current or lower separation may help. | Without trying to follow exactly what happens in your cide, are the loop delays (several 10 mS ones, affecting response time? If you change them does response time alter? Commented Aug 15, 2015 at 16:04
  • Only the first delay is needed. Try removing the other two.
    – Gerben
    Commented Aug 15, 2015 at 19:26

1 Answer 1

0

Your third delay is the issue. It means you have up to 10 ms before you notice the need to turn on the light. Try this:

const byte pin = 13; 
volatile bool detected; 

void setup() { 
  pinMode(pin, OUTPUT); 
  attachInterrupt(1, blink, RISING); 
} 

void loop() { 
  if (detected)
    {
    delay(10);
    digitalWrite(pin, LOW);
    detected = false;
    }

} 

void blink() { 
  digitalWrite(pin, HIGH);
  detected = true; 
}

Now that immediately turns on the LED, so it will always happen at the same time. Then in the main loop you notice it is on, wait 10 ms, and then turn it off.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.