I had a problem with the keypad library last week, on a remote control I am making,so I took the 4 pushbuttons straight to 4 pins on the chip, which works fine.
I have 7 BCD switches, which I scan in sequence, read the 1,2,4,8 switch outputs ( through diodes ) and convert to byte to send.
All is well except one of the switches shows all numbers fine, except 7, which shows as a 0.
I couldnt imagine any software problem doing this, so I just changed the switch, and its still doing it . ( the message [7] is the one that is mixed up )
I swapped the pins E and F over in the code, and its another switch that has the problem, but message 7 will not show a 7.
I have monitired the messages as they are generated, and message [7] will show 0 when it should be 7.
I have measured with a meter, and I get continuity between the 1,2,4 outputs of the BCD switch to the chip.
The code is below, can anybody spot my error ?
/*
uses 100 k pull down resistors on BCD inputs to chip, diodes anodes to switch 1248 outputs.
ptt RUNS LED AND RF,
Uses 3 x AA 3v battery
*/
#include <VirtualWire.h> // Wireless transmitter/receiver library
#include <avr/sleep.h> // powerdown library
#include <avr/interrupt.h> // interrupts library
// ***********************************************************************
uint8_t SW[4]; // assign four data pins from bcd switches
int SW0 = 5; // bits to read from bcd switches LSB
int SW1 = 6;
int SW2 = 7;
int SW3 = 8; // - MSB
int address = 0;
int add0; // bits generated from cd switches
int add1;
int add2;
int add3;
int key;
int comA = 14; // switch scans to swithch sliders
int comB = 4;
int comC = 10;
int comD = 15;
int comE = 3;
int comF = 11;
int comG = 12;
int PIN = 77; // preset pin number to send
int dataA; // DATA REBUILT FROM BCD INPUTS FROM SWITCHES
int dataB;
int dataC;
int dataD;
int dataE;
int dataF;
int dataG;
int pause=HIGH;
int dpin0 = 0; // apparently redefined by Serial as Serial Monitor works
int dpin1 = 1; // apparently redefined by Serial as Serial Monitor works
int pin2 = 2; // Int0 interrupt pin
// ***********************************************************************
int sleep_count = 0; // flag/counter to tell us to go sleep
// ***********************************************************************
// create an array to store data to be sent out
char msg [9]; // pin, key, A,B,C,D,E,F,G
int runbut= 16;
int updatebut = 17;
int setbut = 18;
int pausebut = 19;
//***************************************************
// * Name: pin2Interrupt, "ISR" to run when interrupted in Sleep Mode
void pin2Interrupt()
{
/* This brings us back from sleep. */
}
//***************************************************
// * Name: enterSleep
void enterSleep()
{
/* Setup pin2 as an interrupt and attach handler. */
attachInterrupt(0, pin2Interrupt, FALLING);
delay(50); // need this?
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // setting up for sleep ...
sleep_enable(); // setting up for sleep ...
ADCSRA &= ~(1 << ADEN);
PRR = 0xFF;
sleep_mode(); // now goes to Sleep and waits for the interrupt
/* The program will continue from here after the interrupt. */
detachInterrupt(0); //disable interrupts while we get ready to read the keypad
PRR = 0x00;
/* First thing to do is disable sleep. */
sleep_disable();
}
//************************************************************************** LOOP
void setup()
{
Serial.begin(9600);
/* Setup the pin directions, write inputs High to turn on internal pullups */
pinMode(pin2, INPUT); // our sleep interrupt pin
digitalWrite(pin2, HIGH); // set pull up res
// define all the unused pins as inputs with internal pullups for lower power state
pinMode(dpin0, INPUT); // apparently redefined by Serial as Serial Monitor works (receiving anyway)
digitalWrite(dpin0, HIGH); // apparently redefined by Serial as Serial Monitor works
pinMode(dpin1, INPUT); // apparently redefined by Serial as Serial Monitor works
digitalWrite(dpin1, HIGH); // apparently redefined by Serial as Serial Monitor works
pinMode (comA , OUTPUT ); // common slders of the BCD switches
pinMode (comB , OUTPUT );
pinMode (comC , OUTPUT );
pinMode (comD , OUTPUT );
pinMode (comE , OUTPUT );
pinMode (comF , OUTPUT );
pinMode (comG , OUTPUT );
pinMode(SW0, INPUT); // LSB of bcd
byte add0 = 0;
pinMode(SW1, INPUT); // LSB+1
byte add1= 0;
pinMode(SW2, INPUT); // LSB+2
byte add2 = 0;
pinMode(SW3, INPUT); // MSB of bcd
byte add3 = 0;
// PIN = 18; // ( preset for WPCC to 1101 ( 13 ) Pin number SACS = 11 Glenwoodrugby 18
Serial.println("my address is: ");
Serial.println(PIN);
digitalWrite(SW0, LOW); // disable pullups on PIN inputs to save current, finished with these pins PIN read.msg[0]
digitalWrite(SW1, LOW);
digitalWrite(SW2, LOW);
digitalWrite(SW3, LOW);
Serial.println("TX setup"); // for debug only
// vw_set_rx_pin(0);
vw_set_tx_pin(9); // Tx module connections and speed
vw_set_ptt_pin(13); // powers Tx and LED
vw_setup(2000);
; // Bits per sec - had to double from 2000 with 8MHz 3.3V Pro-Mini
// VW reset it to input as the Rx default pin.
// pinMode (comA , OUTPUT ); // common slders of the BCD switches
pinMode ( runbut, INPUT );
pinMode ( setbut, INPUT );
pinMode ( updatebut, INPUT );
pinMode ( pausebut, INPUT );
digitalWrite(runbut, HIGH);
digitalWrite(setbut, HIGH);
digitalWrite(updatebut, HIGH);
digitalWrite(pausebut, HIGH);
}
// *************************************************************************end of void Setup()
[code]
[/code]