Making a "PROGRAM" into a funtion I can call.
ok, here story......
this works.
the "original" 1 turn direction , numbers did front reversed code did.
now, nice piece of code. works - think - doesn't anything.
i wanting use code can put rotary encoders in other projects.
but mean need turn can "include" in other codes.
i how instead of "serial.print........" return value calling code.
but stuck on how can include in other sketches.
could explain me how this?
i shall try in mean time, appreciate help.
code: [select]
#include <arduino.h>
//
// code modified.
// turn clockwise increment value.
// turn counter clockwise decrement value.
//
// -----------------------------------------------------------------------------
// constants
const int pinclk = 2; // used generating interrupts using clk signal
const int pindt = 3; // used reading dt signal
const int pinsw = 4; // used push button switch
const int debounce = 10; // used debounce routine
// -----------------------------------------------------------------------------
// global vars
volatile int virtualposition = 0;
// -----------------------------------------------------------------------------
// forward decls
void isr();
void loop();
void setup();
// -----------------------------------------------------------------------------
// interrupt service routine executed when high low transition detected on clk
void isr()
{
static unsigned long lastinterrupttime = 0;
unsigned long interrupttime = millis();
if (interrupttime - lastinterrupttime > debounce)
{
if (digitalread(pindt))
virtualposition = (virtualposition + 1);
else
virtualposition = virtualposition - 1;
}
lastinterrupttime = interrupttime;
} // isr
// -----------------------------------------------------------------------------
void setup()
{
serial.begin(9600);
pinmode(pinclk,input);
pinmode(pindt, input);
pinmode(pinsw, input_pullup);
attachinterrupt(0, isr, falling); // interrupt 0 connected pin 2 on arduino uno
serial.println("start");
} // setup
// -----------------------------------------------------------------------------
void loop()
{
int lastcount = 0;
while (true)
{
if (!(digitalread(pinsw))) // check if pushbutton pressed
{
virtualposition = 0; // if yes, reset counter zero
while (!digitalread(pinsw)) {} // wait til switch released
delay(10); // debounce
serial.println("reset"); // using word reset instead of count here find out buggy encoder
}
if (virtualposition != lastcount)
{
lastcount = virtualposition;
serial.print("count = ");
serial.println(virtualposition);
}
} // while
} //loop
this works.
the "original" 1 turn direction , numbers did front reversed code did.
now, nice piece of code. works - think - doesn't anything.
i wanting use code can put rotary encoders in other projects.
but mean need turn can "include" in other codes.
i how instead of "serial.print........" return value calling code.
but stuck on how can include in other sketches.
could explain me how this?
i shall try in mean time, appreciate help.
code: [select]
while (true)
running infinite loop inside infinite loop() pointless. rid of infinite loop.
creating series of functions (initencoder(), encodermoved(), etc.) want do. initencoder() function need take function pointer argument, can have multiple instances of encoder, each own isr.
Arduino Forum > Using Arduino > Programming Questions > Making a "PROGRAM" into a funtion I can call.
arduino
Comments
Post a Comment