PDA

View Full Version : Bounce still not behaving



Magnethead494
12-28-2012, 03:36 AM
Paul,

as subject states, I made progress. dimming works like it should, the tens bar works as it should.

But small issue- if it's dimmed AND the tens is turned on, then it ignores other commands to the bounce library- IE it will only capture random hit-and-miss movements of the other switches. If I undimm it or turn the tens off, then it works fine.

is it because of the 1.5 second debounce timer of onetwo (line 148)?

Since I have the update run after lowtime has elapsed (line 284) can I simply use a regular debounce period here (which would make onetwo and one duplicate, and I could simply use the latter)

- Steve

On Dec 9, 2012, at 7:36 PM, Paul Stoffregen wrote:

I looked only briefly, but it seems like you're on a good path. Sadly, I simply don't have time to review code much.

If it doesn't work perfectly, the best troubleshooting is to temporarily place Serial.println("message") inside each if / else and watch which ones get printed in the Arduino Serial Monitor while you press and release the switch in different ways. Usually that makes any error in the logic very obvious.

On 12/13/2012 12:04 PM, Steve wrote:
Paul,

I made the changes you described. Haven't tested it yet [Have to run and get parts, do some assembly, and build a computer after waiting for UPS], does the code look right to you, now? I cleaned some other stuff and added more comments to make it easier to understand.

Yes it will trigger the wrong output if the user holds the switch too long, but I am hoping that the instruction "Simply pull down and let go" should not cause a user to hold it down for an extended period of time.

switch position detection occurs at line 216

engaging into ON passes to line 394

disengaging out of ON passes to line 424

the trigger of (ON) passes to line 308


On Dec 9, 2012, at 7:36 PM, Paul Stoffregen wrote:

I'm not sure I completely understand the description below. It sounds like you have a switch that has 3 mechanical positions, but you've connected it to only a single digital input pin.

I believe the simplest solution is probably to use 2 pins, so you can easily determine the actial position.

It sounds like you're attempting to infer the position based on timing? If the input stays low for longer than a certain length of time, then it's in the "ON" position, but if the input goes high before that length of time, then you conclude it was in the "(ON)" position. If a user holds the switch with their finger too long, it'll make the wrong conclusion? When the user puts the switch into the "ON" position, you can't conclude it's in that position until the maximum "momentary press" time as elapsed.

If that's what you're trying to do, I'd use an elapsedMillis type variable. It's also possible to do with reading millis(), as your code does for the 2.5 second timeout, but elapsedMillis does that for you and it's much simpler to use. It appears to be simple an integer that automatically increases every millisecond.

Perhaps something like:

elapsedMillis sw1lowtime = 0;
int sw1waiting = 0;

void loop() {
if (one.fallingEdge()) {
sw1waiting = 1;
sw1lowtime = 0; // begin waiting....
}
if (sw1waiting) {
if (sw1lowtime > 500) {
sw1waiting= 0;
// conclude "ON" position... do whatever happens when "ON"
} else {
if (one.risingEdge()) {
sw1waiting = 0;
// rising edge happened within 0.5 second, conclude "(ON)" just ended... do something....
}
}
}

Ultimately it's not possible to truly detect more than 2 positions with a single digital signal.

Everyone has their own coding style.... but I would try to put all the code that makes these timing decisions in a single place, and I'd use the special elapsedMillis variable type which make the code simpler and lets you focus on the timing relationships you want without needing to clutter your code with a bunch of storing and subtracting millis() values.


On 12/04/2012 07:11 PM, Steve wrote:
Paul,

I've ran into an issue. I have one switch performing two functions, and I use bounce to help tell them apart.

The switch is an ON - OFF - (ON) piece, with both on's tied together [written high] and center grounded. If the switch is up, it turns one output on, if it is in the middle, it turns that output off [like a light switch]. If it is bumped down, it toggles another output- off if it was on, or on if it was off.

I start the process at line 217.

If the switch has changed from OFF to ON or (ON), this would trigger fallingEdge, and sends the thread to updateDisplays(0) [line 292], which then goes to the switch statement's 0 case.

If the switch has changed from (ON) or ON to off, it triggers risingEdge.

If the switch has changed from (ON) to OFF, I want to ignore it. Not sure how to do it?

I connected one oscilloscope probe to pin 40, and another to the physical switch terminal, and started moving the command to turn 40 on/off around in the code.

What I discovered is that somehow, the thread is getting sent to updateDisplays(8) [line 388], even though I have two ways for it NOT to get there. It is completely skipping over fallingEdge, and catching on ridingEdge, but only randomly. Sometimes it works right, sometimes it doesn't.

Is there a "good" way to work around this?

The way I'm doing it now, when the switch state is first changed, it gets to the switch(0) statement, and waits for a bit. If the pin is still low, then it assumes the switch is in the ON position, and performs that action. If the pin is high, it assumes the switch was in the (ON) position, and has been released, and performs the action for (ON). When the switch disengages to trigger risingEdge, I have it double check EEPROM(3) if it's even a legal operation. If it's an illegial operation, I have it assume a false read, and do what it was meant to do. If it is a legal operation, it turns the output for ON low. (The EERPOM(3) is set at line 379 within case(7), which is called at line 268)

It's a complex situation, maybe you can figure it out?

Thanks,

Steve



http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92945PM.png

http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92926PM.png

http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92840PM.png

http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92831PM.png

Magnethead494
12-28-2012, 03:38 AM
#include <Bounce.h>
#include <EEPROM.h>

Bounce one = Bounce(1, 20); // 10 ms debounce
Bounce onetwo = Bounce(1, 1500); // 10 ms debounce
Bounce twoup = Bounce(0, 40);
Bounce twodown = Bounce(25, 40);
Bounce threeup = Bounce(24, 40);
Bounce threedown = Bounce(23, 40);
Bounce fourup = Bounce(13, 40);
Bounce fourdown = Bounce(27, 40);

HardwareSerial Uart = HardwareSerial();

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
elapsedMillis sw1lowtime = 0;
int sw1waiting = 0;

void setup(){

Serial.begin(9600);

int i;

Uart.begin(9600);

pinMode(4,OUTPUT);
digitalWrite(4, LOW);
pinMode(26,OUTPUT);
digitalWrite(26, HIGH);
pinMode(5,OUTPUT);
digitalWrite(5, HIGH);
pinMode(38,OUTPUT);
digitalWrite(38, HIGH);
pinMode(39,OUTPUT);
digitalWrite(39, HIGH);

for(i = 7; i<13; i++){
pinMode(i, OUTPUT);
digitalWrite(i,HIGH);
}
for(i = 14; i<23; i++){
pinMode(i, OUTPUT);
digitalWrite(i,HIGH);
}
for(i = 43; i<46; i++){
pinMode(i, OUTPUT);
digitalWrite(i,HIGH);
}

pinMode(27,INPUT);
digitalWrite(27, HIGH);

pinMode(13,INPUT);
digitalWrite(13, HIGH);

for(i = 0; i<2; i++){
pinMode(i, INPUT);
digitalWrite(i,HIGH);
}
for(i = 23; i<26; i++){
pinMode(i, INPUT);
digitalWrite(i,HIGH);
}

updateDisplays(9);

}

void loop(){

int runonce = 0;
int tens;
int dim;

if (!runonce){
tens = EEPROM.read(3);
dim = EEPROM.read(4);
runonce = 1;
}

if (one.update()){

sw1waiting = 1;

if (one.fallingEdge()) { //the switch has been actuated off-center
sw1lowtime = 0; // begin waiting....
}
if (sw1waiting) {
if (sw1lowtime < 1000) {
if (one.risingEdge()) {
/* rising edge happened within 1 second, conclude "(ON)" just
ended and dimmer needs attention */
sw1waiting = 0;
updateDisplays(0);
delay(200);
}
} else {
sw1waiting = 0;

}

}

}

if (twoup.update()){
if (twoup.fallingEdge()) {
updateDisplays(2);
}
}
if (twodown.update()){
if (twodown.fallingEdge()) {
updateDisplays(1);
}
}
if (threeup.update()){
if (threeup.fallingEdge()) {
updateDisplays(4);
}
}
if (threedown.update()){
if (threedown.fallingEdge()) {
updateDisplays(3);
}
}
if (fourup.update()){
if (fourup.fallingEdge()) {
updateDisplays(6);
}
}
if (fourdown.update()){
if (fourdown.fallingEdge()) {
updateDisplays(5);
}
}

currentMillis = millis();

if ((currentMillis - previousMillis > 1000) && (sw1lowtime > 1500)){

previousMillis = currentMillis;

updateDisplays(7);

/*if (digitalRead(1) == 0){
updateDisplays(8);
} else {
updateDisplays(10);
}*/

tensOnOff();

}

}

Magnethead494
12-28-2012, 03:38 AM
void tensOnOff(void){

if (onetwo.update()){

if (onetwo.fallingEdge()){
updateDisplays(8);
} else
if (onetwo.risingEdge()){
updateDisplays(10);
}

}

}

void updateDisplays(int value){

Serial.println("Updating Display");

static int currOnes;
static int currTenths;
static int currHunds;
static int dim;
static int tens;

int eeTens;
int eeOnes;
int eeTenths;
int eeHunds;
int eeDim;

switch(value){

case 0:
// Controls the state of dimming


if (dim == 0){
digitalWrite(4, HIGH); // dimm display
dim = 1;
} else {
digitalWrite(4, LOW); // full brightness
dim = 0;
}


break;
case 1:
// controls the value of the ones place, incrementing
if (currOnes == 9){
currOnes = 0;
} else {
currOnes++;
}
break;
case 2:
// controls the value of the ones place, decrementing
if (currOnes == 0){
currOnes = 9;
} else {
currOnes--;
}
break;
case 3:
// controls the value of the tens place, incrementing
if (currTenths == 9){
currTenths = 0;
} else {
currTenths++;
}
break;
case 4:
// controls the value of the tens place, decrementing
if (currTenths == 0){
currTenths = 9;
} else {
currTenths--;
}
break;
case 5:
// controls the value of the hundredths place, incrementing
if (currHunds == 9){
currHunds = 0;
} else {
currHunds++;
}
break;
case 6:
// controls the value of the hundredths place, decrementing
if (currHunds == 0){
currHunds = 9;
} else {
currHunds--;
}
break;
case 7:
/* checks and updates/synchronizes the EEPROM data,
runs every 2.5 seconds at the end of loop() */
eeOnes = EEPROM.read(0);
if (eeOnes != currOnes){
EEPROM.write(0, currOnes);
}
eeTenths = EEPROM.read(1);
if (eeTenths != currTenths){
EEPROM.write(1, currTenths);
}
eeHunds = EEPROM.read(2);
if (eeHunds != currHunds){
EEPROM.write(2, currHunds);
}
eeTens = EEPROM.read(3);
if (eeTens != tens){
EEPROM.write(3, tens);
}
eeDim = EEPROM.read(4);
if (eeDim != dim){
EEPROM.write(4, dim);
}
break;
case 8:
// enable the tens

if (tens == 0){
digitalWrite(26, HIGH); // tens on
tens = 1;
} /*else {
digitalWrite(26, LOW); // tens off
tens = 0;
}*/

break;
case 9:
/* runs at the start in setup(), decides to start the board in the dimmed
and/or tens state based on how the box was powered off */

currOnes = EEPROM.read(0);
currTenths = EEPROM.read(1);
currHunds = EEPROM.read(2);
tens = EEPROM.read(3);
dim = EEPROM.read(4);

if (dim == 1){
digitalWrite(4, HIGH); // dimm display
} else {
digitalWrite(4, LOW); // full brightness
}

if ((tens == 1) && (digitalRead(1) == 0)){
/* make sure the switch is in the tens position
after the box was powered down with it there */
digitalWrite(26,HIGH);
} else {
digitalWrite(26,LOW);
}

Uart.print('w');
Uart.write((int) 2);

Uart.print('z');
Uart.write((int) 255);

break;
case 10:

if (tens == 1){
digitalWrite(26, LOW); // tens off
tens = 0;
}

break;

}

/*control for serial indicator display starts here */

if ((dim == 0) && (tens == 0)){
/* full brightness, no tens
displays "[blank] [ones] [tens] [hunds]" */

Uart.print('v');
Uart.print('x');
Uart.print(currOnes);
Uart.print(currTenths);
Uart.print(currHunds);
} else if ((dim == 0) && (tens == 1)){
/* full brightness, tens
displays " 1 [ones] [tens] [hunds]" */
Uart.print('v');
Uart.print('1');
Uart.print(currOnes);
Uart.print(currTenths);
Uart.print(currHunds);

} else if ((dim == 1) && (tens == 0)){
/* dimmed, no tens
displays " d [ones] [tens] [hunds]" */

Uart.print('v');
Uart.print('d');
Uart.print(currOnes);
Uart.print(currTenths);
Uart.print(currHunds);

} else if ((dim == 1) && (tens == 1)){
/* dimmed, tens
alternates two outputs:

" d [ones] [tens] [hunds]"

" 1 [ones] [tens] [hunds]"

*/

Uart.print('v');
Uart.print('d');
Uart.print(currOnes);
Uart.print(currTenths);
Uart.print(currHunds);

delay (750);

Uart.print('v');
Uart.print('1');
Uart.print(currOnes);
Uart.print(currTenths);
Uart.print(currHunds);

}

setDisplay(currOnes, currTenths, currHunds);

}

Magnethead494
12-28-2012, 03:39 AM
void setDisplay(int ones, int tenths, int hunds){

Serial.println("Setting Display");

int digR[7] = {18, 38, 21, 19, 20, 39, 22};
int digC[7] = {16, 15, 44, 17, 45, 14, 43};
int digL[7] = {8, 7, 11, 9, 10, 5, 12};

switch(ones){
case 0:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], HIGH);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], LOW);
break;
case 1:
digitalWrite(digR[0], LOW);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], LOW);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], LOW);
digitalWrite(digR[6], LOW);
break;
case 2:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], LOW);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], HIGH);
digitalWrite(digR[5], LOW);
digitalWrite(digR[6], HIGH);
break;
case 3:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], LOW);
digitalWrite(digR[6], HIGH);
break;
case 4:
digitalWrite(digR[0], LOW);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], LOW);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], HIGH);
break;
case 5:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], LOW);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], HIGH);
break;
case 6:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], LOW);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], HIGH);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], HIGH);
break;
case 7:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], LOW);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], LOW);
digitalWrite(digR[6], LOW);
break;
case 8:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], HIGH);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], HIGH);
break;
case 9:
digitalWrite(digR[0], HIGH);
digitalWrite(digR[1], HIGH);
digitalWrite(digR[2], HIGH);
digitalWrite(digR[3], HIGH);
digitalWrite(digR[4], LOW);
digitalWrite(digR[5], HIGH);
digitalWrite(digR[6], HIGH);
break;

}

switch(tenths){
case 0:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], HIGH);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], LOW);
break;
case 1:
digitalWrite(digL[0], LOW);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], LOW);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], LOW);
digitalWrite(digL[6], LOW);
break;
case 2:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], LOW);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], HIGH);
digitalWrite(digL[5], LOW);
digitalWrite(digL[6], HIGH);
break;
case 3:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], LOW);
digitalWrite(digL[6], HIGH);
break;
case 4:
digitalWrite(digL[0], LOW);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], LOW);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], HIGH);
break;
case 5:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], LOW);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], HIGH);
break;
case 6:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], LOW);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], HIGH);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], HIGH);
break;
case 7:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], LOW);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], LOW);
digitalWrite(digL[6], LOW);
break;
case 8:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], HIGH);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], HIGH);
break;
case 9:
digitalWrite(digL[0], HIGH);
digitalWrite(digL[1], HIGH);
digitalWrite(digL[2], HIGH);
digitalWrite(digL[3], HIGH);
digitalWrite(digL[4], LOW);
digitalWrite(digL[5], HIGH);
digitalWrite(digL[6], HIGH);
break;
}

switch(hunds){
case 0:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], HIGH);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], LOW);
break;
case 1:
digitalWrite(digC[0], LOW);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], LOW);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], LOW);
digitalWrite(digC[6], LOW);
break;
case 2:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], LOW);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], HIGH);
digitalWrite(digC[5], LOW);
digitalWrite(digC[6], HIGH);
break;
case 3:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], LOW);
digitalWrite(digC[6], HIGH);
break;
case 4:
digitalWrite(digC[0], LOW);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], LOW);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], HIGH);
break;
case 5:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], LOW);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], HIGH);
break;
case 6:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], LOW);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], HIGH);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], HIGH);
break;
case 7:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], LOW);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], LOW);
digitalWrite(digC[6], LOW);
break;
case 8:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], HIGH);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], HIGH);
break;
case 9:
digitalWrite(digC[0], HIGH);
digitalWrite(digC[1], HIGH);
digitalWrite(digC[2], HIGH);
digitalWrite(digC[3], HIGH);
digitalWrite(digC[4], LOW);
digitalWrite(digC[5], HIGH);
digitalWrite(digC[6], HIGH);
break;
}

}

PaulStoffregen
12-28-2012, 01:41 PM
I would not design the code this way. It's confusing to create 2 Bounce objects on the same pin, and then on top of that build code which does different things based on timing.

There's also a delay(200) lurking inside an application that really should never delay, since it's checking for updates (or should be) all the time. That may be of no consequence, but any use of delay() in a program like this is suspicious.

In private email, you mentioned using a single 3-position switch with a single I/O pin, to perform multiple functions. I recall the intended function was unclear (at least to me). Maybe you could write a specification-style description of what that switch does? Often a spec-style written description is a good step towards building clear and reliable code to implement the desired function. Even if not, a clear written description would be useful for anyone looking at the code.

It's also entirely possible you've got some sort of hardware problem? If unrelated inputs are failing with power is applied to certain LEDs, that very well may be hardware issue? I just can't tell.

But one thing is certain, I just don't understand your code using 2 Bounce objects on the same pin. If you want help from me, I would highly recommend restructuring this program, so there's 1 Bounce object per pin, and all are updated at the beginning of loop(), and all are tested for risingEdge() and/or fallingEdge() as the first thing loop() does after updating them. Embedding the Bounce updates and checks inside another function, which is only called under certain circumstances, makes your code simply too difficult to read. If it works and you understand it, then great. But when you ask me or anyone else for help, if your code is difficult to read, it's impossible to help you.

Adding comments, at the top which explain what the project as a whole actually does, and comments near each risingEdge() and fallingEdge() that explain the intended action would also make this program more readable, increasing the chances I could help in a meaningful way.

I'm sorry if this sounds a bit harsh, but as this program is designed (and documented), I just can't reasonably follow the logic.

Magnethead494
12-29-2012, 07:35 AM
This is my loop().

Switch one is a ON-OFF-(ON). I'm limited to the 10 conductors of an RJ50 (+5V, GND, 1-way UART, one, two-up, two-down, three-up, three-down, four-up, four-down] as shown in the schematic.

http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92840PM.png

When one is up ["ON"], then the pin for Tens should be HIGH.

When one is down momentarily ["(ON)"], then the pin for the dimming relay should be held high, until one is actuated momentarily again. The two should also be simultaneous- so that the board is dimmed and the tens is on, together. Such that one is pulled down ["(ON)"], released, the pushed up ["ON"].

This is the display module, followed by the control module. The dimming relay is at the top center. NC (transistor off) is a direct power feed, NO (transistor on) feeds the power through a 16 ohm resistor to dim the LED's.

In an ideal world, I could simply say that while one is low, turn on the tens. If one is only low for 1/2 a second then released [using Bounce], dim/undim the board. However, that would turn the tens on while actuating the dim command, which is what I don't want, even though it would turn the tens back off after the dimming actuation.

The physical display is updated separately from the bounces because the updateDisplays() function also manages the serial output. Yes I realize that having a delay here is strange, but it's the only place that I can reference if dim and tens are both true, to cycle what the serial output sends.

https://sphotos-b.xx.fbcdn.net/hphotos-ash3/133374_10151548573069606_189481074_o.jpg

http://i1105.photobucket.com/albums/h355/magnethead494/Screenshot2012-12-27at92926PM.png



void loop(){

int runonce = 0;
int tens;
int dim;

if (!runonce){
tens = EEPROM.read(3);
dim = EEPROM.read(4);
runonce = 1;
}

if (one.update()){

sw1waiting = 1;

if (one.fallingEdge()) { //the switch has been actuated off-center
sw1lowtime = 0; // begin waiting....
}
if (sw1waiting) {
if (sw1lowtime < 1000) {
if (one.risingEdge()) {
/* rising edge happened within 1 second, conclude "(ON)" just
ended and dimmer needs attention */
sw1waiting = 0;
updateDisplays(0);
delay(200);
}
} else {
sw1waiting = 0;

}

}

}

if (twoup.update()){
if (twoup.fallingEdge()) {
updateDisplays(2);
}
}
if (twodown.update()){
if (twodown.fallingEdge()) {
updateDisplays(1);
}
}
if (threeup.update()){
if (threeup.fallingEdge()) {
updateDisplays(4);
}
}
if (threedown.update()){
if (threedown.fallingEdge()) {
updateDisplays(3);
}
}
if (fourup.update()){
if (fourup.fallingEdge()) {
updateDisplays(6);
}
}
if (fourdown.update()){
if (fourdown.fallingEdge()) {
updateDisplays(5);
}
}

currentMillis = millis();

if ((currentMillis - previousMillis > 1000) && (sw1lowtime > 1500)){

previousMillis = currentMillis;

updateDisplays(7);

/*if (digitalRead(1) == 0){
updateDisplays(8);
} else {
updateDisplays(10);
}*/

tensOnOff();

}

}

Magnethead494
01-01-2013, 11:44 AM
bump to the top.