Go Down

Topic: rotory encoder entering 10th and 100s how to do this? (Read 617 times) previous topic - next topic

rondog

Aug 07, 2012, 08:08 pm Last Edit: Aug 07, 2012, 08:25 pm by rondog Reason: 1
I need to add the function to my PGM that I can set a number such as "118.50" I want to use the rotory enc. to enter first the 118 then push the rotory shaft button then enter the 50 This will display on 7 seg. leds and also be sent to my Visual Basic pgm. Here is the Arduino code thus far. All works as is.

Code: [Select]
// Arduino digital pins used to light up
// corresponding segments on the LED display
// Ron Buchwald** BUCHTRONICS LLC
// REV 1- 1/23/2012  10:AM
//NEW ADDED 8/8/2012



#define ID0 4 //DATA 0 - 3
#define ID1 5
#define ID2 6
#define ID3 7
#define AD0 9  // ADDRESS 0- 2
#define AD1 10
#define AD2 11
#define WRITE 8
#define encoder0PinA  2
#define encoder0PinB  3
volatile unsigned int encoder0Pos = 0;
// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW
void icm7218c(int digit,int datum)
{  digitalWrite(AD0,1 & digit);
  digitalWrite(AD1,1 & (digit >> 1));
  digitalWrite(AD2,1 & (digit >> 2));
  digitalWrite(ID0,1 & datum);
  digitalWrite(ID1,1 & (datum >> 1));
  digitalWrite(ID2,1 & (datum >> 2));
  digitalWrite(ID3,1 & (datum >> 3));
  digitalWrite(WRITE,HIGH);
  delay(10);
  digitalWrite(WRITE,LOW);
}
void numout(int n)
{  int pos = 4;
  do
  {  icm7218c(pos,n % 10);
     n /= 10;
  }
  while (pos--);
}        
void setup() {
 
 pinMode(ID0, OUTPUT);
 pinMode(ID1, OUTPUT);
 pinMode(ID2, OUTPUT);
 pinMode(ID3, OUTPUT);
 pinMode(AD0, OUTPUT);
 pinMode(AD1, OUTPUT);
 pinMode(AD2, OUTPUT);
 pinMode(WRITE,OUTPUT);
 
 pinMode(encoder0PinA, INPUT);
 digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
 pinMode(encoder0PinB, INPUT);
 digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor

 attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
 Serial.begin (9600);
 Serial.println("start");                // a personal quirk
}
void loop() {
 
 
 encoder0Pos = constrain(encoder0Pos, 11800, 13699);
       numout(encoder0Pos);
   }
 void doEncoder() {
 /* If pinA and pinB are both high or both low, it is spinning
  * forward. If they're different, it's going backward.
  * For more information on speeding up this process, see
  * [Reference/PortManipulation], specifically the PIND register.
  */
 if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
   encoder0Pos++;
 } else {
   encoder0Pos--;
 }
     Serial.println (encoder0Pos, DEC);
 }      


As you may see the number prints as 11850 I have hard wired the dec. point on the leds and the VB pgm does not need to see the "."
Also it would be rear cool if the 100s flash when being set and then the 10th. I have made a Radio comm panel for my flight simm. ;)
Any suggestions welcome. Ron.

PeterH


Any suggestions welcome. Ron.


Write a sketch which:

Has a state variable indicating whether it is inputting units or decimals or nothing.
While inputting a value, periodically updates the display to make that value flash.
Detects when the encoder has moved forward or backwards.
Increments or decrements the current value of the number being adjusted, when the encoder moves.
Outputs the changed value to the display, when the value changes.
Monitors the pushbutton and detects changes between up and down states.
Updates the state variable when the pushbutton is pushed (either up or down - you decide).
I only provide help via the forum - please do not contact me for private consultancy.

rondog

Quote
Write a sketch which:

Has a state variable indicating whether it is inputting units or decimals or nothing.


Thank you for the replay however this does not help me to understand HOW I would write this in code. As the encoder is the input source, I would need to say if the button is not pushed then the rotory pos is inputting units else it is inputting decimals. would this go somewhere here?

if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos++;
  } else {
    encoder0Pos--;
  }

I realy don't understand.

PaulS

Quote
As the encoder is the input source, I would need to say if the button is not pushed then the rotory pos is inputting units else it is inputting decimals. would this go somewhere here?

No. The encoder is not inputting "units" or "decimals". It is inputting pulses. You are counting those pulses. It is up to you to interpret those pulses as the integer portion of the value or the fractional portion (terminology IS important).

Whether you make that decision as a result of a switch press, or some other means, is up to you. You do NOT make that decision in that block of code, though. You make the decision whether the number is the fractional part or the whole part when the "I'm done changing the number" input is received, whatever that input consists of.

PeterH


Thank you for the replay however this does not help me to understand HOW I would write this in code.


Is it true to say that you want the movement of the encoder to mean different things at different times? So, for example, initially the encoder is ignored. Push the button, and the Arduino goes into 'entering units' mode where the units go up and down as you move the encoder; push the button again and the Arduino goes into 'entering decimals' mode where the decimals go up and down as you move the encoder; push the button again and the Arduino goes back to idle, where the encoder is ignored. Is that the sort of thing you have in mind?

In that case, I suggest you start by writing a sketch that reads the state of the pushbutton, reads whether it is up or down, detects whether this is a transition i.e. from up to down or vice versa, and changes between modes each time the button is pressed. Changing modes is as simple as defining an enumerated type that defines the possible modes, and having a variable which holds the current mode.

While reading the button you will want to debounce it, and I believe there's a library which will do that for you.
I only provide help via the forum - please do not contact me for private consultancy.

rondog

#5
Aug 09, 2012, 02:39 pm Last Edit: Aug 09, 2012, 02:52 pm by rondog Reason: 1
Code: [Select]
Push the button, and the Arduino goes into 'entering units' mode where the units go up and down as you move the encoder; push the button again and the Arduino goes into 'entering decimals' mode

Yes this is what I want to do. Having said this, The button is NOT what I don't understand!. The question is How do I write the code to say "encoder is only counting 100s then only counting 10ts."
look at the sketch again. Where and how do I phrase this. A small snippet would be a big help, IE. INT X = 100; INT Y = 10; I need to write the code, not how to go about writing the code, I need the code. I hope I have cleared this up. Both answers are not addressing the question. Thanks. Ron.


Riva

I have attached a test sketch I wrote to use a rotary encoder to set the date and time of a DS3231 RTC. It should give you the basic idea of what you need to do.
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

PeterH



Yes this is what I want to do. Having said this, The button is NOT what I don't understand!. The question is How do I write the code to say "encoder is only counting 100s then only counting 10ts."

I need to write the code, not how to go about writing the code


I'm starting to get the impression that you don't want advice about how to design or write the code - you want somebody to give you the working code. Is that right?
I only provide help via the forum - please do not contact me for private consultancy.

Go Up