Continue to Site

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.

  • 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.

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

Status
Not open for further replies.
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.
 
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



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:
slighly easier perhaps? still won't work though
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:
Don't know if this made it better but I fixed three things
Code:
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
}
 
How you driving your motor hardware I have a hard drive but it would not spin just plugging it up to the power.
 
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

**broken link removed**
 
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!

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:
shot from now working display
 
at least i learned a lot from this project :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top