I am missing something stupid here, I am using 3 cascaded CD4017 decade counters to feed ( up to ) 25 bcd switches.
(I am posting here as it is multiplexing )
The 4 outputs of the switches are fed via diodes to 4 data inputs ( SW0 - 3 ) of the micro, each input has a pull down resistor.
The following code is supposed to send the data from the switches via virtualwire , but with no connections to the data inputs - just the pull down resistors I get all 1000 when the ISR is triggered. i.e :-
TX setup
Sleep
data for switchbank 0 = 1000
data for switchbank 1 = 1000
blah blah
data for switchbank 18 = 1000
data for switchbank 19 = 1000
Sleep
actually I think its trying to tell me something, its 4 am and I should have some rest ![]()
can anyone see my blonde moment here?
Incidentally, with another chip for the scanning that has zero going outputs to the switches, I inverted the data with the commented out line
// address |= digitalRead(SW*) << i; and this gives 1111 as expected...............*
my code is
```
*// FIRST TRY SCANNING USING 4017
#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 = 3; // bits to read in unique address - LSB
int SW1 = 4; // bits to read in unique address
int SW2 = 5; // bits to read in unique address
int SW3 = 6; // bits to read in unique address - MSB
int address = 0;
int add0;
int add1;
int add2;
int add3;
int reset = 7;
int clock = 8;
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
// create an array to store data to be sent out
// ***********************************************************************
int switchbanks = 19; // CHANGE TO NUMBER OF SWITCHES max 25
char msg [20]; // includes msg 0 which is PIN number from dip switches
// ***********************************************************************
// * 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, LOW);
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
// Power up functions
PRR = 0x00;
/ First thing to do is disable sleep. */
sleep_disable();
}
// ***********************************************************************
void setup()
{
Serial.begin(9600);
pinMode(pin2, INPUT); // our sleep interrupt pin
digitalWrite(pin2, HIGH); // sets pull up res
pinMode ( reset, OUTPUT ); // resets the external cd4017 counters to 0
digitalWrite(reset, HIGH);
pinMode ( clock, OUTPUT ); // starts the clock to the CD4017s to zero so first out = PIN number
digitalWrite(clock, LOW);
//***************************************************************************
// all data inputs SW0-3 have pull down 22k resistors
pinMode(SW0, INPUT); // LSB of remote Address
byte add0 = 0;
pinMode(SW1, INPUT); // LSB+1
byte add1= 0;
pinMode(SW2, INPUT); // LSB+2
byte add2 = 0;
pinMode(SW3, INPUT); // MSB of address
byte add3 = 0;
// *************************************************************************
Serial.begin(9600);
Serial.println("TX setup"); // for debug only
// ***********************************************************************t
// Initialise the IO and ISR for VirtualWire
vw_set_ptt_pin(10); // should be set as default ?
vw_setup(4000); // Bits per sec
} // end of void Setup()
// *******************************************************************************************
void loop()
{
digitalWrite(reset, HIGH);
Serial.println("Sleep"); // for debug only
enterSleep(); // call Sleep function to put us out
// THE PROGRAM CONTINUEs FROM HERE after waking up in enterSleep()
digitalWrite(reset, LOW);
scan ();
vw_send((uint8_t *)msg, switchbanks); // send the character out
vw_wait_tx(); // Wait until the whole message is gone
delay (10);
} // end of void loop
//********************************************************************************************
void scan ()
{
for ( int x=0; x<= switchbanks; x++ )
{
digitalWrite(clock, LOW );
delay ( 50 );
address=0;
for (uint8_t i = 0; i < 4; i++) { // read inputs from bcd switches sequenced by CD4017s
// address |= digitalRead(SW[i]) << i;
address = digitalRead(SW[i]) << i;
}
digitalWrite(clock, HIGH); // advances CD4017s ready for next read
Serial.print("data for switchbank ");
Serial.print(x);
Serial.print(" = ");
Serial.println(address, BIN);
msg [switchbanks] = address;
} // end of for ( int x=2; x<= switchbanks; x==)
} // end of scan function*
```