Go Down

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

raschemmel

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.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

westfw

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 = 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

raschemmel

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.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

westfw

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

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy