Pages: 1 [2] 3   Go Down
Author Topic: Up and Down counter with Debounce  (Read 4295 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

OK!!!

So once millis reaches the maximum value, then it rolls over to 0, good to know!!!!

My code works perfectly when powered on USB to the terminal window.

My issue now is:

I power the Arduino Duemilanove ATMEGA168 board externally with 12V, since Rx and Tx are TTL, I thought I could loop Pin0 and Pin1, but it does not seem to work...

How do you make a Loop on the Arduino Pin0 and Pin1?
Logged

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do you make a Loop on the Arduino Pin0 and Pin1

A couple of centimetres of hookup wire.

But why?
Logged

Per Arduino ad Astra

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

Basically, the code sends a channel # Ex. "+TSC04" and if it echos back, the Arduino will display the current channel on the BCD/7-segment display...so if I could loop both serial pins it would act like I have it connected to the radio I plan to change channels too.

But it was a no-go when I did the mentioned hoop up wire from Pin0 to Pin1.
Logged

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You've disconnected the FTDI chip?
Logged

Per Arduino ad Astra

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

No, can I do this with jumper?
Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33448
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can I do this with jumper?
No look at the schematic you will find two 1K resistors in series with the TX and RX lines, remove them to isolate the line. However you have to solder them back to up load another sketch, so maybe you could add your own jumpers.
Logged

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

Thanks for all your replays...

At this point I'm more than happy with my program, yet per the schematics I can't do a hardware loop back successfully.

I tried even removing Pin 2 and 3 on the Duemilanove and crossing them, but stil when I press the buttons I don't see the channel change. I even went and bought a 16 Mhz osc, but when I would plug the MAx233A TTL to RS232 converter, it would like reset the programming and I would have to re-program the device constantly. I can probably blame it on static, "tomorrow I try with 0.1 caps" but still, what I really need is the Serial 232 board that predates the Duemilanove.

Also, if anyone knew a good Software Loop Back for a Com Port it would be awesome! I tried making it on Visual Studio and got nowhere hehe.

I will keep you guys posted on the progress as it happens, but this is where I'm now...
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

While I await for parts, I found a odd bug.

If I send over serial a command "+TSC01" to "+TSC09" all works fine.

Once I start sending commands different than those, it does odd things, example:

+TSC33

It shows 3 on the BCD 7 Segment LED

or

+TSC55

it shows 5 on the BCD 7 Segment LED

I like to add to my code a way that when it receive anything other than those in the switch case it would show a error.

Like on the if and else statements...but for the switch...

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 chan;

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?");
  }

  if (Serial.available() > 0)
  {
     chan = Serial.read();
     switch (chan){
     case '+TSC01':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC02':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC03':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC04':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC05':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC06':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC07':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC08':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC09':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     default:
       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.

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

You have this code:

Code:
if (Serial.available() > 0)
  {
     chan = Serial.read();
     switch (chan){
     case '+TSC01':

The chan variable is a single character. The case statement has multiple characters in the single character quotes.

That won't work.

If none of the previous switch cases are true, the default case will be executed. Put your error stuff there.
Logged

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

PaulS

I believe I found out why my Arduino was not working, I have got a few 16 MHz crystals that I bought at a local store, for what I have being reading, you need the correct pairing of capacitors to make the crystal oscillate correctly. I went ahead and ordered the same crystal on the Arduino with a matching pair of 22p caps. I also ordered a ceramic resonator just in case.

http://www.cutedigi.com/product_info.php?cPath=277&products_id=4299&osCsid=fa52452e4320cff6523c735974aaf920

Now on the coding, I used:

Code:
char chan

So if I want to use a variable with more than a character then I must use something like this?

Code:
int chan

and for the code, I should replace this...

Code:
case '+TSC01'

with these?

Code:
case "+TSC01"

or what do I use for a multi-character variable case?

When I compile it gives me:

error: invalid conversion from 'const char*' to int
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: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to read a string from the serial port, you have to do that one letter at a time. You store each letter in an array, and increment the index into that array.

Code:
char serialString[20]; // Set whatever size is appropriate
int serialIndex = 0;
char TC = '#'; // Some terminating character that you send

void loop()
{
   while(Serial.available() > 0)
   {
       char aChar = Serial.read();
       if(aChar == TC)
          break;
       serialString[serialIndex] = aChar; // Store the character
       serialIndex++;
       serialString[serialIndex] = '\0'; // Null terminate the string
   }

   // Now, serialString contains something like "+TSC01".

   if(strcmp(serialString, "+TSC01") == 0)
   {
       // The strings match. Do something...
   }
   else if(strcmp(serialString, "+TSC02") == 0)
   {
      // These strings match. Do something...
   }
   else
   {
       // Unrecognized string. Let some smoke out...
       // Or something...
   }
}

You can't use a switch statement with non-constant cases (like strings), so you have to emulate the switch statement with if/else if/else.
Logged

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

I can't program a termination character, so I used part of example code to array the serial characters, also strcmp and if else statements.

So far this works

I set my array for 6, now the issue is...what if I get garbage data in between?

for example...

1234567+TSC0189012


since is more than 6, when it gets to 7 it starts the array over and it sees:

123456
7+TSC0
189012

which when comparing with strcmp it can't recognize it.

I was wondering how to fix this...if I have to increase the size of my array and use ** or something alike?

This is my current 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[6];
int serialIndex = 0;

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();
     serialString[serialIndex] = aChar;
     serialIndex++;
     serialString[serialIndex] = '\0';
  }
 
     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
       {
         Serial.println(serialString);  //for de-bugin
       for (int BCD = 2; BCD < 6; BCD++)
         {
         digitalWrite(BCD, LOW);
         }    
       }
}
« Last Edit: February 05, 2010, 01:57:56 pm by bastukee » 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: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the valid part of the string always starts with +TSC, start storing data only when the + is encountered.

If not, you will need to find a way to send a start or stop (or both) character.
Logged

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

I tried this approach...

Code:
 while(Serial.available() > 0)
  {
     char aChar = Serial.read();
     if (aChar or serialString[0] == '+')
     {
       serialString[serialIndex] = aChar;
       serialIndex++;
       serialString[serialIndex] = '\0';
     }
     else
     {
       serialString[0] = '\0';
     }
  }

But it does exactly what it use to do...
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: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this then:
Code:
boolean startReceived = false;
char serialString[20];
int serialIndex = 0;

void loop()
{
   while(Serial.available() > 0)
   {
       char aChar = Serial.read();
       if(startReceived)
       {
          serialString[serialIndex] = aChar;
          serialIndex++;
          serialString[serialIndex] = '\0';
       }
       else
       {
          if(aChar == '+')
          {
             strartReceived = true;
             serialString[serialIndex] = aChar;
             serialIndex++;
             serialString[serialIndex] = '\0';
          }
      }
   }
}
Logged

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