0

For a simple project I would like to use a ATTiny85 connected to a HC-06 Bluetooth Module so it can talk to my Android phone.
I wrote a code for my Arduino Uno and worked as expected. When I changed the code to use on my ATTiny85 I got an error saying that 'Serial' was not declared in this scope and assumed that the ATTiny does not support Hardware Serial.
I need to read a String when received and sleep the MCU when not receiving. I went to use SoftwareSerial and was not able to get a String, just the first char.
I approached it in some was, like defining a char string[10]; as global and string[i] = mySerial.read(); i++; inside the loop, but it keeps not working. Whether it is the sleep, or my work to read data, I couldn't make it work.

Can someone provide a way to put a ATTiny85 to sleep, wake up to receive a String through Serial and sleep til the next data through Serial, please?

To sleep I'm using

void sleep() {
 GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
 PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
 sei(); // Enable interrupts
 sleep_cpu(); // sleep
 // woke up
 cli(); // Disable interrupts
 PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
 sleep_disable(); // Clear Sleep Enable bit
 sei(); // Enable interrupts
}
ISR(PCINT3_vect) {
}

And my loop is something like

char inputString[10];
int i = 0;
void loop() {
 sleep();
 if (serial.available() > 0) {
 char inputChar = serial.read();
 if (inputChar == '2') { //Char to break
 //Do something and reset i
 } else {
 inputString[i] = inputChar;
 }
 i++;
 }
}

Thanks to all.

asked Nov 26, 2017 at 18:39
1
  • You could easily overflow your buffer here. It’s a fixed 10 bytes, but nothing checks to see if you’ve filled it. You might be just trashing the memory by writing outside of the array. Commented Feb 4, 2018 at 14:28

1 Answer 1

0

Hello,

I think you should do this:

char inputChar = ''; //this in global
while (serial.available()) {
 inputChar = serial.read();
 if (inputChar == '2') { //Char to break
 //Do something and reset i
 } else {
 inputString[i] += inputChar; //= to +=
 }
 i++;
}
answered Dec 6, 2017 at 8:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.