Pages: 1 2 [3]   Go Down
Author Topic: Up and Down counter with Debounce  (Read 4592 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply...


I went ahead on the Boolean approach, and I have written it this way...

Code:
 while(Serial.available() > 0)
  {
    char aChar = Serial.read();
      if (serialIndex >= 5)
      {
      startReceived = false;  
      }  
     if (startReceived)
     {
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }
     else
     {
     if(aChar == '+')
     {
     serialIndex = 0;
     startReceived = true;
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[0] = '\0';
     Serial.println(serialString);  //for debugin
     }
     }  
     Serial.println(startReceived); //for debugin
     Serial.println(serialIndex); //for debugin
  }


Good thing now is that when I reach the end of the array (5), it resets the Index back to 0 and will only start adding after it sees '+' .

Bad thing about it is that It won't store anything on the array or at least nothing shows on the terminal window stored on the serialString array.

All of my code

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 250;
int chanPolling = 10000;
unsigned long chanPrevPoll = 0;
char serialString[20];
int serialIndex = 0;
boolean startReceived = false;

void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  for (int updown = 6; updown < 8; updown++) {
         pinMode(updown, INPUT);
       }
  for (int BCD = 2; BCD < 6; BCD++) {
         pinMode(BCD, OUTPUT);
       }

}

void channelFunction()
{
  switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        Serial.println("+TSC?");
        break;
      case 2:
        Serial.println("+TSC02");
        Serial.println("+TSC?");
        break;
      case 3:
        Serial.println("+TSC03");
        Serial.println("+TSC?");
        break;
      case 4:
        Serial.println("+TSC04");
        Serial.println("+TSC?");
        break;
      case 5:
        Serial.println("+TSC05");
        Serial.println("+TSC?");
        break;
      case 6:
        Serial.println("+TSC06");
        Serial.println("+TSC?");
        break;
      case 7:
        Serial.println("+TSC07");
        Serial.println("+TSC?");
        break;
      case 8:
        Serial.println("+TSC08");
        Serial.println("+TSC?");
        break;
      case 9:
        Serial.println("+TSC09");
        Serial.println("+TSC?");
        break;
       }
}
void loop()
{
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
      {
      channel = 1;
      }
    lastBtnUp = millis();
    channelFunction();
    }
   }
  prevBtnUp = buttonUpState;

  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if (millis() - lastBtnDwn > transInt)
    {
    channel--;
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
     channelFunction();
  }
  }
  prevBtnDwn = buttonDownState;

      if (millis() - chanPrevPoll > chanPolling)
  {
    chanPrevPoll = millis();
    Serial.println("+TSC?");
  }

  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
      if (serialIndex >= 5)
      {
      startReceived = false;  
      }  
     if (startReceived)
     {
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }
     else
     {
     if(aChar == '+')
     {
     serialIndex = 0;
     startReceived = true;
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[0] = '\0';
     Serial.println(serialString);  //for debugin
     }
     }  
     Serial.println(startReceived); //for debugin
     Serial.println(serialIndex); //for debugin
  }


     if(strcmp(serialString, "+TSC01") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);

       }
     else if(strcmp(serialString, "+TSC02") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC03") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC04") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC05") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC06") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC07") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC08") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC09") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       }
     else
       {
       for (int BCD = 2; BCD < 6; BCD++)
         {
         digitalWrite(BCD, LOW);
         }
       }
}
 



Why is it not storing anything on the array serialString ?
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why is it not storing anything on the array serialString ?

You don't print serialString after filling it, so how do you know that nothing is stored in serialString?

After storing the + in serialString, you increment serialIndex to point to the next position, where the NULL is to go, but then you don't put the NULL there. Instead, you write the NULL right over the top of the + you just stored there.

Of course, then you never again write to serialString[0], so the 1st value in the array is always NULL, so even if you did print serialString, printing would stop on the 1st NULL (in
  • ), so you'd see nothing.

Change
Code:
serialString[0] = '\0';
to
Code:
serialString[serialIndex] = '\0';
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That did the trick!!!!!!!

I have learned so much over project, thank you so much...

My final Code

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 250;
int chanPolling = 10000;
unsigned long chanPrevPoll = 0;
char serialString[20];
int serialIndex = 0;
boolean startReceived = false;

void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  for (int updown = 6; updown < 8; updown++) {
         pinMode(updown, INPUT);
       }
  for (int BCD = 2; BCD < 6; BCD++) {
         pinMode(BCD, OUTPUT);
       }

}

void channelFunction()
{
  switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        Serial.println("+TSC?");
        break;
      case 2:
        Serial.println("+TSC02");
        Serial.println("+TSC?");
        break;
      case 3:
        Serial.println("+TSC03");
        Serial.println("+TSC?");
        break;
      case 4:
        Serial.println("+TSC04");
        Serial.println("+TSC?");
        break;
      case 5:
        Serial.println("+TSC05");
        Serial.println("+TSC?");
        break;
      case 6:
        Serial.println("+TSC06");
        Serial.println("+TSC?");
        break;
      case 7:
        Serial.println("+TSC07");
        Serial.println("+TSC?");
        break;
      case 8:
        Serial.println("+TSC08");
        Serial.println("+TSC?");
        break;
      case 9:
        Serial.println("+TSC09");
        Serial.println("+TSC?");
        break;
       }
}
void loop()
{
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
      {
      channel = 1;
      }
    lastBtnUp = millis();
    channelFunction();
    }
   }
  prevBtnUp = buttonUpState;

  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if (millis() - lastBtnDwn > transInt)
    {
    channel--;
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
     channelFunction();
  }
  }
  prevBtnDwn = buttonDownState;

      if (millis() - chanPrevPoll > chanPolling)
  {
    chanPrevPoll = millis();
    Serial.println("+TSC?");
  }

  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
      if (serialIndex >= 6)
      {
      startReceived = false;  
      }

     if (startReceived)
     {
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }
     else
     {
     if(aChar == '+')
     {
     serialIndex = 0;
     startReceived = true;
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }

     Serial.println(serialString);
     Serial.println(serialIndex);
     }  
  }


     if(strcmp(serialString, "+TSC01") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);

       }
     else if(strcmp(serialString, "+TSC02") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC03") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC04") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC05") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC06") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC07") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       }
     else if(strcmp(serialString, "+TSC08") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       }
     else if(strcmp(serialString, "+TSC09") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       }
     else
       {
       for (int BCD = 2; BCD < 6; BCD++)
         {
         digitalWrite(BCD, LOW);
         }
       }
}
 


Now I can trow at it something like

+11223456+TSC07jjjjjffff

and it will change to 7 on my 7-segment LED display.

The whole idea of the project was:

That I press a up/down button, it sends the command to a radio ex. "+TSC05" , when the radio changes channel it replies what channel it went to..."+TSC05". So I use the reply to display on the 7 segment LED what channel the radio is on.

I just got my resonator today, I"m about to do the hardwired test and will reply with results.





« Last Edit: February 08, 2010, 01:50:31 pm by bastukee » Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I got a Atmega368P-PU and I wired it to connect to the radio, using a Max233A for the UART. The resonator I ordered worked great and so did the Arduino. I was having one issue with it, I could not program it from the UART with a Max233A.

I like to mention for anyone that tries to do this too...I found out that in order to program it over serial, I needed to connect a small ceramic capacitor "100 p" or so from Pin 4 on the serial DB9 "DTR" to Pin 1 "Reset" on the Atmega chip which allows the Arduino program to reset the chip as it programs it. And now it works well!



For now it is completed smiley-grin

Thanks again! I will be posting on other projects...


My final final code...







Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 250;
int chanPolling = 5000;
unsigned long chanPrevPoll = 0;
char serialString[20];
int serialIndex = 0;
boolean startReceived = false;

void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  for (int updown = 6; updown < 8; updown++) {
         pinMode(updown, INPUT);
       }
  for (int BCD = 2; BCD < 6; BCD++) {
         pinMode(BCD, OUTPUT);
       }

}

void channelFunction()
{
  switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        Serial.println("+TSC?");
        break;
      case 2:
        Serial.println("+TSC02");
        Serial.println("+TSC?");
        break;
      case 3:
        Serial.println("+TSC03");
        Serial.println("+TSC?");
        break;
      case 4:
        Serial.println("+TSC04");
        Serial.println("+TSC?");
        break;
      case 5:
        Serial.println("+TSC05");
        Serial.println("+TSC?");
        break;
      case 6:
        Serial.println("+TSC06");
        Serial.println("+TSC?");
        break;
      case 7:
        Serial.println("+TSC07");
        Serial.println("+TSC?");
        break;
      case 8:
        Serial.println("+TSC08");
        Serial.println("+TSC?");
        break;
      case 9:
        Serial.println("+TSC09");
        Serial.println("+TSC?");
        break;
       }
}
void loop()
{
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
      {
      channel = 1;
      }
    lastBtnUp = millis();
    channelFunction();
    }
   }
  prevBtnUp = buttonUpState;

  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if (millis() - lastBtnDwn > transInt)
    {
    channel--;
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
     channelFunction();
  }
  }
  prevBtnDwn = buttonDownState;

      if (millis() - chanPrevPoll > chanPolling)
  {
    chanPrevPoll = millis();
    Serial.println("+TSC?");
  }

  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
      if (serialIndex >= 6)
      {
      startReceived = false;  
      }

     if (startReceived)
     {
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }
     else
     {
     if(aChar == '+')
     {
     serialIndex = 0;
     startReceived = true;
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
     }

     Serial.println(serialString);
     Serial.println(serialIndex);
     }  
  }


     if(strcmp(serialString, "+TSC01") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
         channel = 1;
       }
     else if(strcmp(serialString, "+TSC02") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
         channel = 2;
       }
     else if(strcmp(serialString, "+TSC03") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
         channel = 3;
       }
     else if(strcmp(serialString, "+TSC04") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
         channel = 4;
       }
     else if(strcmp(serialString, "+TSC05") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
         channel = 5;
       }
     else if(strcmp(serialString, "+TSC06") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
         channel = 6;
       }
     else if(strcmp(serialString, "+TSC07") == 0)
       {
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
         channel = 7;
       }
     else if(strcmp(serialString, "+TSC08") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
         channel = 8;
       }
     else if(strcmp(serialString, "+TSC09") == 0)
       {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
         channel = 9;
       }
     else
       {
       for (int BCD = 2; BCD < 6; BCD++)
         {
         digitalWrite(BCD, LOW);
         }
       }
}
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Pages: 1 2 [3]   Go Up
Jump to: