using function repeatedly

I have wasted a day on this, I have before used a function to calculate a result using the same integers and just reloading the information and generating an answer.

I am obviously doing something stupid. I have posted the Mk 999 version below, it has changed many times today..

This time I am trying to scan the commons of 5 BCD switches, by sequentially taking pins 7,8,9 13, and 14 low, and feeding the 4 binary contacts ( through diodes ) to the pins 3 ,4 , 5 and 6 and saving the number as msg [n] = address. for sending by Virtual Wire.

I tried it with linking a wire between 6 and 14 which gives me the predicted 7 on the first loop, and then 15 for the next loops.

When I print the pins it is right , but the address always want to give 15.

I could do it separately, but its bugging me because I have been having some success with functions and arrays lately.. Time to phone a friend :slight_smile:

#include <VirtualWire.h>    // Wireless transmitter/receiver library
#include <avr/sleep.h>      // powerdown library
#include <avr/interrupt.h>  // interrupts library
// ***********************************************************************
int SW0 = 3;               // bits to read from BCD switches - LSB
int SW1= 4;               // bits to read from BCD switches
int SW2 = 5;               // bits to read from BCD switches
int SW3 = 6;               // bits to read from BCD switches - MSB

int add0;                  // address bits read from switches
int add1;
int add2;
int add3;

int comUnit = 7;    //  outputs to common contacts of the BCD switches
int comTens = 8;
int comHund = 9;
int comThou = 13;
int comAddr = 14;

int comcount;
//int switchbanks = 5;  // change here for number of switches in unit 

int pin2 = 2;               // Int0 interrupt pin,  refresh button to ground
int address = 0;            // bits put together afteer reading switches

char msg [5]; // extra char added for address
//****************************************************************************
//                           FUNCTION FOR READING DATA FROM BCD SWITCHES
void checkdata () // read and put together bytes function
{
  add3 = digitalRead(SW3); // shift it left 3 places
  add3 = add3 << 3;
  add2 = digitalRead(SW2);
  // shift it left 2 places
  add2 = add2 << 2;
  add1 = digitalRead(SW1);
  // shift it left 1 place
  add1 = add1 << 1;
  add0 = digitalRead(SW0);
  // now OR it together
  address = address|add3;
  address = address|add2;
  address = address|add1;
  address = address|add0;
  Serial.print("data for switchbank ");
  Serial.println(address);
  int n = comcount;
  msg [n]  = address;
}
//**************************************************************************************
// *  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 
  PRR = 0x00;
  /* First thing to do is disable sleep. */
  digitalWrite(comUnit, HIGH);
  digitalWrite(comTens, HIGH); 
  digitalWrite(comHund, HIGH);
  digitalWrite(comThou, HIGH);
  digitalWrite(comAddr, HIGH);
 
  sleep_disable(); 
}

// ***********************************************************************
// set up the pins as Inputs, Outputs, etc.
void setup()
{
  Serial.begin(9600);

  pinMode  (comUnit, OUTPUT );
  pinMode ( comTens, OUTPUT) ;
  pinMode ( comHund , OUTPUT) ;
  pinMode ( comThou , OUTPUT) ;
  pinMode ( comAddr , OUTPUT) ;

  /* Setup the pin directions, write inputs High to turn on internal pullups */

  //**************************************************************************
  pinMode(pin2, INPUT);                 // our sleep interrupt pin
  digitalWrite(pin2, HIGH);
    // puts all BCD inputs to input with pullups
  pinMode(SW0, INPUT); // LSB of remote Address
  digitalWrite(SW0, HIGH);
  // byte add0 = 0; // read the value of SW0
  pinMode(SW1, INPUT); // LSB+1
  digitalWrite(SW1, HIGH);
  // byte add1= 0;
  pinMode(SW2, INPUT);  // LSB+2
  digitalWrite(SW2, HIGH);
  //  byte add2 = 0;
  pinMode(SW3, INPUT);  // MSB of address
  digitalWrite(SW3, HIGH);
  //  byte add3 = 0;

  // Initialise the IO and ISR for VirtualWire
  vw_setup(4000);	                      // Bits per sec - had to double from 2000 with 8MHz 3.3V Pro-Mini
}                                            // end of void Setup()
void loop()
{
  //               wakes up here
  for (int comcount=0; comcount<=5; comcount++ ) {
    switch (comcount)
    {
    case 0:
      digitalWrite (comAddr, LOW);    
      checkdata ();    
     // msg [0]  = address;
      digitalWrite (comAddr, HIGH);
      break;
    case 1:
      digitalWrite (comThou, LOW);    
      checkdata ();    
     // msg [1]  = address;
      digitalWrite (comThou, HIGH);
      break;
    case 2:
      digitalWrite (comHund, LOW);    
      checkdata ();    
     // msg [2]  = address;
      digitalWrite (comHund, HIGH);
      break; 
    case 3:
      digitalWrite (comTens, LOW);    
      checkdata ();    
     // msg [3]  = address;
      digitalWrite (comTens, HIGH);
      break;
    case 4:
      digitalWrite (comUnit, LOW);    
      checkdata ();    
    //  msg [4]  = address;
      digitalWrite (comUnit, HIGH);
      break;
    case 5:
      Serial.println("Sleep");
      enterSleep ();
    //default:
    }  // end of switch function
  }  //   end of comcount 1-4

  vw_send((uint8_t *)msg, 5);     // send the character out
  //Serial.println(key);                // for debugging only
  vw_wait_tx();                             // Wait until the whole message is gone
  delay (50); 
}
// end of void loop

John,
The problem might be that I OR'd the bits together to make the 4-bit address. Once they are set, there is nothing to clear them. I was writing for a static address that came off a DIP switch that didn't change after power on.
Try setting "address" to 0 before calling 'checkdata()'

Thanks Robert, I didn't want to bug you yet again with a pm :slight_smile:

I will try that now

Thanks Robert, I wish I had bugged you earlier now :wink: seems to be fine .

I should have spotted it I guess, you wont believe the things I tried !

Yep, you can yourself into all kinds of situations cuttin' & pastin'!

Thats very true, at one point I actually started from scratch but when I got some hassles with that I wondered if I was heading for a dead end, so went back to what I had working in a different form.

When I get caught up and have some time, I will go back without pressure and see if it works.