Connect with us

Arduino IR break beam sketch

Discussion in 'Microcontrollers, Programming and IoT' started by Liamlambchop, Oct 20, 2011.

Scroll to continue with content
  1. Liamlambchop

    Liamlambchop

    14
    0
    Sep 19, 2011
    Hey everyone,

    I am trying to use an Arduino library called 'Timer1' http://arduino.cc/playground/Code/Timer1

    I am trying to use it to switch on a pulse width modulation function every x microseconds. I am doing this, instead of simply running the PWM function in the loop, because I need to run other things in the loop :)

    Here is my sketch in English, could I have some help with the C language?

    if Timer1 period is up {
    begin PWM function;
    stop Timer1;
    start a new timer which will be used to stop the PWM function;
    {
    if the new timer's period is up {
    stop PWM;
    restart old Timer1 (and hence restart the cycle);}
    }
    }

    This would have to be in my void loop() I'd imagine. And I would also likely have to be using Timer1.read().... but I am not sure how to implement that.

    Any help would be greatly appreciated!
     
  2. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    doesn't the arduino have multiple PWM pinout's?

    If I understand you correctly, I would use a while loop setting the PWM pin to operate and then pause for whatever set time you need.

    If you are trying to generate PWM on a non-PWM pin then I am not so sure I understand your problem.


    edit: after looking at the library it seems that Timer1 is used to set the PWM freq. so now all you need is to make it go on and off every few seconds?
     
    Last edited: Oct 20, 2011
  3. Liamlambchop

    Liamlambchop

    14
    0
    Sep 19, 2011
    I need my PWM to be at 38Khz. This is much faster than the arduino's PWM pinout's unfortunately.

    Here is what I have so far:

    #include <TimerOne.h>

    unsigned long now;
    unsigned long then = 0;
    unsigned long interval = 2500000;
    unsigned long pwminterval = 5000000;

    void setup(){
    pinMode(9, OUTPUT);
    Timer1.initialize();
    digitalWrite(13,HIGH);

    }

    void loop(){
    now = micros(); //variable to store the read value
    if (now - then > interval){
    Timer1.pwm(9,512,100000);}
    if (now - then > pwminterval){
    digitalWrite(9,LOW);
    then = now;
    }

    }

    It doesn't work It should: turn pin13 on HIGH, wait 2.5seconds, start pwm, stop pwm after 2.5s, wait 2.5 seconds, start pwm, stop pwm after 2.5s...... etc. But instead it: turns on pin13 HIGH, waits 2.5 seconds, starts pwm, stops pwm for a little bit (less than 2.5s) and then erratically blinks on and off randomly.

    What is the problem with the sketch?
    I have a feeling that Timer1. is screwing with the timing of it all. Because when I take out the 'now = then' (on the last line of my sketch) pwm still continues even though the Timer1.'s pin is meant to be LOW.

    I also tried using while, here is what I did:

    #include <TimerOne.h>

    unsigned long now;
    unsigned long then = 0;
    unsigned long interval = 2500000;
    unsigned long pwminterval = 5000000;

    void setup(){
    pinMode(9, OUTPUT);
    digitalWrite(13,HIGH);

    }

    void loop(){
    now = micros(); //variable to store the read value
    if (now - then > pwminterval){
    now = then;}
    while(2500000 < now < 5000000){
    digitalWrite(9,HIGH);
    delayMicroseconds(100000);
    digitalWrite(9,LOW);
    delayMicroseconds(100000);


    } }

    This doesn't work either. It just turns the LED on pin 9 on at the same time as the LED on pin 13. What's going on?
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,291
    2,731
    Jan 21, 2010
    Try asking this on a dedicated arduino forum. I am aware that you can change prescaler values to do such stuff as altering the time required to do an A-D, there may be a similar way to alter pwm frequency at the risk of a reduction in resolution.
     
  5. Liamlambchop

    Liamlambchop

    14
    0
    Sep 19, 2011
    I've made this one:)

    It seems to work. If anyone sees a problem with it, please let me know. If not, feel free to steal it!

    Code:
    /* People counter sketch baby!
     */
    
    // constants won't change. Used here to 
    // set pin numbers:
    const int ledPin =  9;      // the number of the LED pin
    
    // Variables will change:
    int ledState = LOW;             // ledState used to set the LED
    long previousMicros = 0;        // will store last time LED was updated
    long then = 0; //stores value of now when it was last updated
    
    // the follow variables is a long because the time, measured in miliseconds,
    // will quickly become a bigger number than can be stored in an int.
    long interval = 13 ;           // interval at which to blink (microseconds)
    long pause = 520; // pause between pulsing
    long pausepulse = 2080; //time from beginning of pause till end of pulsing
    
    void setup() {
      // set the digital pin as output:
      pinMode(ledPin, OUTPUT);      
    }
    
    void loop()
    {
      // here is where you'd put code that needs to be running all the time.
    
      // check to see if it's time to blink the LED; that is, if the 
      // difference between the current time and last time you blinked 
      // the LED is bigger than the interval at which you want to 
      // blink the LED.
      long now = micros();
      
      if (now - then >= pause && now - then <= pausepulse){
        unsigned long currentMicros = micros();
     
        if(currentMicros - previousMicros > interval) {
        // save the last time you blinked the LED 
        previousMicros = currentMicros;   
    
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;
    
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
        }}
      if (now - then >= pausepulse){
        then = now;}
      
      }
        
      
          
    
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-