well hears the basic idea i have an arduino mega 2560 and five 1 watt 8-30 volt regulated high output led modules in parallel i want to use the mega 2560 to drive the gate on a mosfet using pwm in the following modes each mode is selected by the push of a single no momentary button or if i can the use of a rotary encoder knob pinched from a washing machines control panel
the modes are as follows
mode one off this is the state i want with either the encoder @ 12 o clk position or after i hold the button down 20 sec
mode two high aka leds at max brightness encoder @ 3 o clk or botton pushed twice in 15 sec
mode three med aka half brightness encoder @ 6 o clk or 4 pushes of button in 15 sec
mode four low aka min usable brightness encoder @ 9 o clk or 6 pushes of button in 15 sec
but i knned help sketching this code i have the pwm codes
Can you post again, using the shift key and some punctuation, please?
my apologies im posting this from an iPod touch so its not easy to properly structure these posts.
here is more information on the encoders im using they are 6 pin 32 detent binary state and im using them with pin 1 as gnd and pins 2-6 as the pull down pins for the input of the mega2560 i have the following states chosen for the project
going counterclockwise from detent 32 i am using the following process
on the circuit the following conditions are true encoder pin 1 tied to gnd
digital ins 1,2,3,4,5 of the mega2560 are tied to +5v through a 4.7K sip resistor network
encoder pins 2,3,4,5,6 are tied to the following
digital pin 1 encoder pin 2
digital pin 2 encoder pin 3
digital pin 3 encoder pin 4
digital pin 4 encoder pin 5
digital pin 5 encoder pin 6
32-1 detent ccw = pin 1 tied to pins 3,5,6
32-2 detent ccw = pin 1 tied to pins 3,4,5,6
32-3 detent ccw = pin 1 tied to pins 2,3,4,5,6
32-4 detent ccw = pin 1 tied to pins 2,3,5,6
i need to be able to make a sketch that reads the states of digital pins 1-5 and using a set of defined truth tables for the states of those pins compared to the state of the encoders position preforms the actions i posted above in my initial post
I'm not too interested in the details of wiring your rotary switch up to the input pins, but assuming you get that right it will be possible to infer the switch position from the inputs. Given that you will know how the input values relate to the switch position, it will not be hard to implement the code to read the inputs and determine the switch position. The other behaviour you describe also sounds straight forward. Which part are you having trouble with?
i am having a hard time deciding what conditional loop to use should i use the digital read pin mode in a if then function or use the do while and go sub call to run a subroutine that is selected by the state of the returned values of the input pins?
i at one time used a lot of q basic in this form but that was a lot of years ago when i was still in high school and i have not kept up on any form of code writing my area of expertise is in hardware interfaces and logic integration i used to be the lead hardware developer for virtual diva data systems inc which is at this time in asset foreclosure due to lack of income
There seems to be a significant amount of control logic where handling of inputs depends on past inputs. If I were you I'd design the control logic as a finite state machine.
Hi, silly question first, did you test the pins of the switch you are using with a voltmeter to be sure is working correctly?
Then, if what you're using is a rotary encoder I suggest you to read this:
http://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino
From your description however it seems more a 5 state switch, and in that case I would do something like this:
//initialize a variable "state"
int state;
//create a function that reads continously the pins
//and returns the pin number
int rotaryEncoderCheck() {
//from pin2 to pin6
for (int i=2; i<=6; i++) {
//if the pin is HIGH
if(digitalRead(i) == HIGH) {
//return its value
return i;
}
}
}
//put in your loop this call to the function
//and with state value control the logic
state = rotaryEncoderCheck();
Bye! ![]()
Hi VirtualDDS,
I'm no C++ programmer so am little help on the code but may have a suggestion.
If the encoder your planning to use is a position encoder then could you ensure it's inputs are all on the same mega port and read the entire port instead of one pin at a time. Then you would just read the port, mask off unwanted pins and use a switch/case to select what to do depending on result.
If you cannot put entire encoder on one port then maybe creating an array of pins to read and in a loop read each pins state and rotating it's result into a byte or int variable. At the end of the loop you would use switch/case to act on result.
// List of input pins to read
const int Input_Pins [7] = {
1,5,3,7,9,11,2};
void setup(){
// set list of pins to input
for (int a=0;a<7;a++){
pinMode(Input_Pins[a],INPUT);
}
}
void loop(){
// Read the pins
int x = readPins();
// Act on returned value
switch (x) {
case 0: // 12 O'Clock
// statements
break;
case 90: // 3 O'Clock
// statements
break;
case 180: // 6 O'Clock
// statements
break;
case 270: // 9 O'Clock
// statements
break;
default: // Any other value, do nothing
// statements
break;
}
}
int readPins(){
int result = 0; // Setup result register
for (int a=0;a<7;a++){ // Index through array
int x=digitalRead(Input_Pins[a]); // Read pin state
result=result << 1; // Rotate result register
result = result | x; // OR pinstate into bit 0
}
return result; // Return
}
the encoder im using is basically a binary switch that for each of its 32 detent positions generates a bit sequence on its 6 pins one of those pins however im using as the common pin since im intent on using this switch to pull down high state pins from the arduino to in theory avoid the false trigger issue associated with floating input states here is the full grey code table for the encoder
shaft
position
1 0 0 0 1 1 off
2 0 0 1 1 1
3 0 1 1 1 1
4 0 1 0 1 1
5 0 1 0 0 1
6 0 1 1 0 1
7 0 0 1 0 1
8 0 0 0 0 1 hi
9 0 0 0 0 0
10 0 0 1 0 0
11 0 1 1 0 0
12 0 1 0 0 0
13 0 1 0 1 0
14 0 1 1 1 0
15 0 0 1 1 0
16 0 0 0 1 0 med
17 1 0 0 1 0
18 1 0 1 1 0
19 1 1 1 1 0
20 1 1 0 1 0
21 1 1 0 0 0
22 1 1 1 0 0
23 1 0 1 0 0
24 1 0 0 0 0 low
25 1 0 0 0 1
26 1 0 1 0 1
27 1 1 1 0 1
28 1 1 0 0 1
29 1 1 0 1 1
30 1 1 1 1 1
31 1 0 1 1 1
32 1 0 0 1 1 strobe
arduino pin 3 4 5 6 7
encoder pin 4 2 1 3 5
it does not like the layout of the int command warning only initialized variables can be placed into program memory area
// List of input pins to read
const int Input_Pins [7] = {
3,4,5,6,7};
void setup(){
// set list of pins to input
for (int a=0;a<7;a++){
pinMode(Input_Pins[a],INPUT);
}
}
void loop(){
// Read the pins
int x = readPins();
// Act on returned value
switch (x) {
case 0: // 12 O'Clock
// statements
break;
case 90: // 3 O'Clock
// statements
break;
case 180: // 6 O'Clock
// statements
break;
case 270: // 9 O'Clock
// statements
break;
default: // Any other value, do nothing
// statements
break;
}
}
int readPins(){
int result = 0; // Setup result register
for (int a=0;a<7;a++){ // Index through array
int x=digitalRead(Input_Pins[a]); // Read pin state
result=result << 1; // Rotate result register
result = result | x; // OR pinstate into bit 0
}
return result; // Return
}
Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
hang on it says (int a=0;a<7:a++) is this trying to use the annologue pins i would like to use the digital inputs
is this trying to use the annologue pins i
const int Input_Pins [7] = { 3,4,5,6,7};
No, but it does use pin zero several times.
AWOL:
is this trying to use the annologue pins i
const int Input_Pins [7] = { 3,4,5,6,7};No, but it does use pin zero several times.
im not using pins 0 or 1 thats my I2C bus port at this time
If you're using that array with that for loop, you're using pin zero twice.
VirtualDDS:
it does not like the layout of the int command warning only initialized variables can be placed into program memory area
With verbose compilation off, I get:
Binary sketch size: 902 bytes (of a 32256 byte maximum)
The initial example I wrote used seven pins but you only need 5 (my bad), just changed the 7 to a 5 in the initial pin table and any for/next loops that use it. Hopefully this will work and compile fine.
// List of input pins to read
const int Input_Pins [5] = {
3,4,5,6,7};
void setup(){
// set list of pins to input
for (int a=0;a<5;a++){
pinMode(Input_Pins[a],INPUT);
}
}
void loop(){
// Read the pins
int x = readPins();
// Act on returned value
switch (x) {
case 3: // Off
// statements
break;
case 1: // Hi
// statements
break;
case 2: // Med
// statements
break;
case 16: // Low
// statements
break;
case 19: // Strobe
// statements
break;
default: // Any other value, do nothing
// statements
break;
}
}
int readPins(){
int result = 0; // Setup result register
for (int a=0;a<5;a++){ // Index through array
int x=digitalRead(Input_Pins[a]); // Read pin state
result=result << 1; // Rotate result register
result = result | x; // OR pinstate into bit 0
}
return result; // Return
}