Go Down

Topic: Arduino 0007 Serial printing and switches (Read 608 times) previous topic - next topic

braz

I've just recently started playing with a standard Arduino NG board (AtMega8) and am working to connect several switches to the board and tell which of the switches are active and which was the first switch activated. The program checks pins 5 and 7 to which the switches are connected and then uses the Serial.print function to print a(pin 5) or b(pin 7) out along with a 1(on) or 0(off) depending on the switch state. The problem is that after a short number of lines ~64 the program crashes or stops transmitting data back out on the serial. The TX light on the board stops flashes as it does for the earlier readings (before 64ish). I'm not sure if it is the way I've written the code or if I need to do something else but any suggestions, comments or advice would be welcome. I'm using a Mac OS X 10.4.8 and the Arduino 0007 IDE.

The code is
Code: [Select]
int switchAPin = 5;
int switchAState = LOW;
int switchBPin = 7;
int switchBState = LOW;
int switchPins[2];
char pinsToSwitches[2] = {'a', 'b'};
char switchOrder[2] = {'Z', 'Z'};
int activeListSwitches [2] = {0,0};
int val = 0;       // variable to store the data from the serial port
int timeOut = 500;
int counter = 1;
int startTime = 0;
char processing = 'N';

void setup() {
 pinMode(switchAPin, INPUT);
 digitalWrite(switchAPin, HIGH);
 pinMode(switchBPin, INPUT);
 digitalWrite(switchBPin, HIGH);
 Serial.begin(9600);        // connect to the serial port
 Serial.println("\nReady for data entry");  // print readyiness message to serial monitor for user
}

void loop () {
 startTime = millis();
 
 for (int i=0; i < 2; i++)
 {
   activeListSwitches[i] = 0;
   switchOrder[i] = 'Z';
 }
 processing='N';
 
 while (millis() - startTime < timeOut)
 {
   switchAState = digitalRead(switchAPin);   // read the input pin for switchAPin
   switchPins[0] = switchAState;
   switchBState = digitalRead(switchBPin);   // read the input pin for switchBPin
   switchPins[1] = switchBState;
   
   if (switchAState == LOW)  // Switch A active
   {
     activeListSwitches[0] = 1;
     processing='Y';
     
     if ( switchOrder[0] == 'Z' )
     {
       switchOrder[0] = 'a';
     }
     else if ( switchOrder[1] == 'Z' && switchOrder[0] != 'a')
     {
       switchOrder[1] = 'a';        
     }
     else
     {
       //ignore - already set
     }
   }
   
   if (switchBState == LOW)  // Switch B active
   {
     activeListSwitches[1] = 1;
     processing='Y';
     
     if ( switchOrder[0] == 'Z' )
     {
       switchOrder[0] = 'b';
     }
     else if ( switchOrder[1] == 'Z' && switchOrder[0] != 'b')
     {
       switchOrder[1] = 'b';        
     }
     else
     {
       //ignore - already set
     }      
   }
 }
 
 if (processing=='Y')
 {
   for (int i=0; i < 2; i++)
   {  
     if (switchOrder[i] != 'Z')
     {
       Serial.print(switchOrder[i]);
     }
   }
   for (int i=0; i < 2; i++)
   {  
     Serial.print(activeListSwitches[i]);
   }
   Serial.println("");
 }
}


The typical output is
Code: [Select]
Ready for data entry
a10
a10
a10
a10
b01
b01
b01
b01
b01
a10
a10
a10
a10
a10
a10
a10
ab11
ab11
ab11
ab11
ab11
ab11
ab11
ab11
ba11
ba11
b01
b01
ba11
ab11
ab11
ab11
ab11
ab11
a10
a10
a10
a10
a10
a10
a10
a10
a10
a10
ab11


Thanks

mellis

You may need to make startTime an unsigned long.  Otherwise it will overflow after about 30 seconds.  

braz

Thanks David,

Its now working perfectly! I'm off to see if I can get it to work with some piezo sensors as a floor sensor for activating a group display.

Here's a copy of the first layout for a single sensor - more will follow.


Go Up