Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Serial transmission duration different with serial monitor on and off...???? on: November 14, 2011, 12:59:29 pm
Ok, another question for the experts.... 

I found a multi-button debounce program online that is perfect for what i am looking for.  When i run it with the serial monitor, i press a button, it outputs to the serial monitor for as long as i hold the button down (working perfectly).  When i close the serial monitor, i press and release the button, the serial TX light stays on for an additional 5 seconds, and it is sending that data for a longer period of time than the button is pressed.  Any ideas on why this would continue to send data?

Here is the code:
#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {3,4,5,6}; // using digital pins 3,4,5,6
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed'
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  byte i;

  // set up serial port
 // Serial.print("Button checker with ");
 // Serial.print(NUMBUTTONS, DEC);
 // Serial.println(" buttons");

  // pin13 LED
  pinMode(13, OUTPUT);

  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);

  // Run timer2 interrupt every 15 ms
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;



void check_switches()
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() < lasttime) {
     // we wrapped around, lets just try again
     lasttime = millis();

  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();

  for (index = 0; index < NUMBUTTONS; index++) {

    currentstate[index] = digitalRead(buttons[index]);   // read the button

    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");

    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons

void loop() {
  for (byte i = 0; i < NUMBUTTONS; i++) {
    if (justpressed[i]) {
      justpressed[i] = 0; //this sends a single serial Byte regardless of whether button is held down
      Serial.print(i+1);  //i+1 because we want the button "0" to be sent as button 1
      //Serial.println(" Just pressed");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    if (justreleased[i]) {
      justreleased[i] = 0;
    //  Serial.print(i, DEC);
    //  Serial.println(" Just released");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    if ((pressed[i]) && (i < 2)) { // want to stream serial data for button 1 and 2 while button is pressed
      Serial.print(i+1, BYTE);  //i+1 because we want button "0' to be sent as button 1
      //Serial.println(" pressed");
      // is the button pressed down at this moment

Thanks guys!
2  Using Arduino / Networking, Protocols, and Devices / Re: Serial Communication between 3 arduino on: November 14, 2011, 11:52:29 am
Thank you all for your replies..   I found that it works up to 255 with a Serial.print(value, BYTE) command.

3  Using Arduino / Networking, Protocols, and Devices / Re: Serial Communication between 3 arduino on: October 27, 2011, 03:01:14 pm
Thank youl, i'll put it up asap
4  Using Arduino / Networking, Protocols, and Devices / Serial Communication between 3 arduino on: October 27, 2011, 02:51:05 pm
Hey all,

I am working on a project that requires 3 separate arduino boards working together passing information back and forth via serial (open to other communication protocols, but I am relatively new to this).  Ultimately they will be wireless.

Set up: 
1 arduino uno (we will call it transmitter ) to transmit analog data (need 20 discreet values to be sent)
1 ardunio mega to receive data from transmitter, as well as be connected to (5) analog sensors, and process and use said data to control digital outputs and send sensor data to the next arduino
1 arduino uno connected to a 16x2 LCD which will be updated with information from the Mega, and provide a menu system that is navigated by "transmitter" (listed above).

 - reason for three arduinos is that each module will be in a different location.

I don't have the code handy to post, but it seems that in configuring the transmitter and mega (connected with wires between serial ports) i can only pass values 0-9, and nothing larger....

Sorry for the ambiguity in this question - ultimately i am reaching out to see if there is any advice for this type of set up, and how to pass more data serially between them.  especially to be able to pass variables or sensor data between the mega and the UNO driving the LCD.

Am I using the wrong data type ? (char, int...?)

FWIW, i have been searching online and through the forums for the last couple of weeks before posting.  Most of the information i have found about preparing data to be sent serially has to do with mapping larger values to a small range, which won't work for this application.

Thanks in advance.
Pages: [1]