Go Down

### Topic: Is anyone interested in knowing how to get serial comm on an ATtiny85? (Read 1 time)previous topic - next topic

#### raschemmel

##### Dec 05, 2013, 11:13 pm
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
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

#### westfw

#1
##### Dec 06, 2013, 06:57 am
It's not any different reading the characters from a keyboard than from any other source.
The usual algorithm looks like:
Code: [Select]
`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: [Select]
`n = 01 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 = 123skip more -1s4 is a digit; n = 123*10 + 4 = 1234skip more -1sspace is a terminator.  Exit the loop with n = 1234`

#### raschemmel

#2
##### Dec 06, 2013, 07:19 am
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.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

#### westfw

#3
##### Dec 06, 2013, 11:56 am
is_terminator() and is_digit() are functions (or macros) that you would have to write yourself.
for example:
Code: [Select]
`boolean is_space(int c) {  if (c == ' ')    return true;  return false;}`
(You'll have to write digit2val() as well.)

#### raschemmel

#4
##### Dec 06, 2013, 03:43 pm
Got it.
Thanks
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Go Up