Pages: [1]   Go Down
 Author Topic: using function repeatedly  (Read 635 times) 0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline
Edison Member
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
 « on: April 25, 2011, 10:11:06 am » Bigger Smaller Reset

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 :-)
Code:

#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 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
// shift it left 2 places
// shift it left 1 place
// now OR it together
Serial.print("data for switchbank ");
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 ...
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);

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:
checkdata ();
// msg [0]  = address;
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

 Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 525
Posts: 26538
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
 « Reply #1 on: April 25, 2011, 12:20:56 pm » Bigger Smaller Reset

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()'
 Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Cape Town South Africa
Offline
Edison Member
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
 « Reply #2 on: April 25, 2011, 12:23:37 pm » Bigger Smaller Reset

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

I will try that now
 Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Cape Town South Africa
Offline
Edison Member
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
 « Reply #3 on: April 25, 2011, 12:34:50 pm » Bigger Smaller Reset

Thanks Robert, I wish I had bugged you earlier now ;-)   seems to be fine .

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

With my mobile phone I can call people and talk to them -  how smart can you get ?

Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 525
Posts: 26538
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
 « Reply #4 on: April 25, 2011, 12:36:59 pm » Bigger Smaller Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Cape Town South Africa
Offline
Edison Member
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
 « Reply #5 on: April 25, 2011, 12:45:15 pm » Bigger Smaller Reset

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.
 Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

 Pages: [1]   Go Up