1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

(solved)could use help with POV-clock code (trouble with even starting correct timing......)

Discussion in 'Arduino' started by fezder, Sep 13, 2015.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You have to understand some stuff about arduino if you use serial it's using interrupts too. You need to commit out the serial stuff in main and write some kind of handler if you want to send out something. There a millis library that may work better I used it on led displays it worked better and non blocking delays.
    But what you could do is keep a running count of how times pin 2 changes over time so you can send it out to figure timing and send out serially.
    Every time your loop runs it sends data out that stops the pin 2 from being read.
     
    • Informative Informative x 1
  2. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    well now, that's new! thanks for info
     
  3. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    Here's another version, motor control left out, but still won't work, i have no clue what causes it, but light still flickers annoyingly....
    by flicker i mean that light should stay on around 0-90 degrees, but fluctuates before and after 90 mark, more before. motor speed stays constant, only output flickers



    Code (c):
    int const outpin = 13;
    volatile int state = LOW;
    int reading;
    int previous = LOW;
    volatile long previousmillis = 0;
    volatile long starttime;
    volatile long elapsedtime;

    void setup() {
        pinMode(outpin, OUTPUT);
        attachInterrupt(digitalPinToInterrupt(2), blink, CHANGE);
    }

    void loop()
    {
      reading = state;                                       //read state
    if (reading == HIGH && previous == LOW)                 //if state is detected
    {
      detachInterrupt(2);                                   //first, disbale interrupt to enable calculations
    if (state == HIGH)
    starttime = millis();                                  //starttime is taken from millis, which counts all the time at background
    }
    previous = reading;                                    //take previous from current reading
    elapsedtime = millis () - starttime;                  //calculate time; millis is bigger than starttime
    if (elapsedtime ==0 && elapsedtime <= 4)           //turn led on between 0-4ms
    {
      digitalWrite (outpin, HIGH);
      }
      if (elapsedtime >=4)                               //turn led off at 5ms
      {
      digitalWrite (outpin, LOW);
      }
      attachInterrupt(digitalPinToInterrupt(2), blink, CHANGE);  //after calculations, enable interrupt
      }

      void blink() {
        state = !state;
    }
     
    Last edited: Feb 15, 2016
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland

    slighly easier perhaps? still won't work though
    Code (c):
    int const outpin = 13;                     //output for darligton array, turns on led-strip
    volatile int rotationmade=false;
    volatile int reading;
    volatile unsigned long starttime;
    unsigned long elapsedtime;
    unsigned long difference;

    void setup() {
      pinMode(outpin, OUTPUT);
      byte const rotation = 2;
      attachInterrupt(digitalPinToInterrupt(rotation), blink, CHANGE);
    }


    void loop()
    {
    noInterrupts();
    reading=rotationmade;
    interrupts();


    if (reading == true)
    {
    digitalWrite (outpin, HIGH);
    }

    elapsedtime = millis() - starttime;
    if (elapsedtime >=5 )
    {
      digitalWrite (outpin,LOW);
      rotationmade = false;
    }
    }



    void blink()
    {
        starttime = millis();                           //set starttime as current millis each rotation
      rotationmade= true;                               //rotation is made
    }
     
     
    Last edited: Feb 15, 2016
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Don't know if this made it better but I fixed three things
    Code (text):
    int const outpin = 13;                     //output for darligton array, turns on led-strip
    volatile int rotationmade=false;
    volatile int reading;
    volatile unsigned long starttime;
    volatile unsigned long elapsedtime; // there are interrupts so we need to make sure there saved
    volatile unsigned long difference;   // these too

    void setup() {
      pinMode(outpin, OUTPUT);
      const byte rotation = 2;  //was wrong byte after const
      attachInterrupt(digitalPinToInterrupt(rotation), blink, CHANGE);
    }


    void loop()
    {
    noInterrupts();
    reading=rotationmade;
    interrupts();


    if (reading == true)
    {
    digitalWrite (outpin, HIGH);
    }

    elapsedtime = millis() - starttime;
    if (elapsedtime >=5 )
    {
      digitalWrite (outpin,LOW);
      rotationmade = false;
    }
    }



    void blink()
    {
        starttime = millis();                           //set starttime as current millis each rotation
      rotationmade= true;                               //rotation is made
    }
     
    • Like Like x 1
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    How you driving your motor hardware I have a hard drive but it would not spin just plugging it up to the power.
     
  8. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    it needs special controller to drive it, it's BLDC so it has 3-wires/phases that need to be driven in specific order, like in stepper motors.
    I used RC-esc for the job, just control the esc like you'd control an servo motor, only i drive esc with ne555 circuit

    [​IMG]
     
  9. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    I asked from arduino forums too of this forums, sorry, but i figured there would be too good knowledge about this, we had some ''trouble'' getting it work, but here's code we managed to put together and now screen is steady. Nevertheless, thanks a lot for help & support from you guys!

    Code (c):
    byte const LEDoutPin = 13;      //output for darligton array, turns on led-strip
    byte const rotationSensor = 2;  // sensor detects one reference mark on the disk

    volatile boolean rotationFlag = false;
    volatile unsigned long starttime;
    unsigned long sensorDetectTime;

    int LEDstate;

    void setup() {
      pinMode(LEDoutPin, OUTPUT);
      attachInterrupt(digitalPinToInterrupt(rotationSensor), rotationSensorISR, FALLING); // or RISING depending on hardware.
    }

    void loop()
    {
      noInterrupts();
      boolean rotationStarted = rotationFlag;
      interrupts();

      if (rotationStarted == true)
      {
        digitalWrite (LEDoutPin, HIGH);  // turn on led when cycle starts
        LEDstate = HIGH;
     
        noInterrupts();
        sensorDetectTime = starttime;  // record the sensor time stamp
        interrupts();
     
        rotationFlag = false;  //acknowledge interrupt by resetting the flag
      }

      if ( micros() - sensorDetectTime >= 5000 and LEDstate == HIGH )
      {
        digitalWrite (LEDoutPin, LOW);  // interval is over, turn off LED
        LEDstate = LOW;  // remember LED state so we only turn it off once.
      }
    }

    void rotationSensorISR()
    {
      starttime = micros();                           //set starttime as current millis each rotation
      rotationFlag = true;                              //rotation is made
    }
     
    Last edited: Feb 15, 2016
  10. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    shot from now working display
     
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You said you wanted to write the code yourself ;) I just pointed you .
     
  12. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    at least i learned a lot from this project :)
     

Share This Page