Pages: [1]   Go Down
Author Topic: Is anyone interested in knowing how to get serial comm on an ATtiny85?  (Read 530 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Faraday Member
**
Karma: 100
Posts: 5988
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an ATtiny85 receiving kybd input and sending serial output (to serial monitor).
At the moment I am only able to convert a single digit (1 through 9) from the keyboard
into an integer the tiny can use for decision making. I know it is possible to get convert
multi-digit strings but I have a little more research to do. Once I get that working
I will be able to enter numerical commands (with or without onscreen menu) to command
specific responses. At the moment it blinks a LED "n" times based on the number "n"
entered on the keyboard.  If there is any interest I will post all the details and code.
If anyone knows how to convert multicharacter keyboard strings to an integer, I would
appreciate your input.
Logged

Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,
DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6783
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not any different reading the characters from a keyboard than from any other source.
The usual algorithm looks like:
Code:
int n = 0;
int c;
const int base = 10;
while (1) {  // loop forever (exit logic is inside)
  c = getchar();   // from wherever
  if (is_terminator(c))
     break;  // end of numeric field; we're done.
  if (is_digit(c)) {
     //  Got a digit; feed it into our number
    c = digit2val(c);   // convert ascii, or keycode, to 0..9 or whatever
    n = n * base + c;  // our previous number is shifted left because we have another digit.
  }
// non-digit, non-terminator characters are ignored.
}
So if you were using Serial.read() and saw "1 -1 -1 -1 -1 -1 2 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 4 -1 -1 -1 -1 -1 -1 space"
you'd do approximately:
Code:
n = 0
1 is a digit; n = 0*10 + 1 = 1
-1 is nothing ("no data" for Arduino), do nothing (several times.)
2 is a digit; n = 1*10 + 2 = 12
-1 is still nothing; skip some more of them.
3 is a digit; n = 12*10 + 3 = 123
skip more -1s
4 is a digit; n = 123*10 + 4 = 1234
skip more -1s
space is a terminator.  Exit the loop with n = 1234
Logged

Offline Offline
Faraday Member
**
Karma: 100
Posts: 5988
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply.
I'm having a slight problem with a compiler error stating "is_terminator" was not declared in this scope."
I tried declaring :
char is_terminator = " " // (space)

but I got a different error.
Logged

Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,
DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6783
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is_terminator() and is_digit() are functions (or macros) that you would have to write yourself.
for example:
Code:
boolean is_space(int c) {
  if (c == ' ')
    return true;
  return false;
}
(You'll have to write digit2val() as well.)
Logged

Offline Offline
Faraday Member
**
Karma: 100
Posts: 5988
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it.
Thanks
Logged

Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,
DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Pages: [1]   Go Up
Jump to: