ATTiny85 Serial communication with Bluetooth Module

815 views Asked by At

For a simple project, I would like to use an ATTiny85 connected to an HC-06 Bluetooth module, so it can talk to my Android phone.

I wrote code for my Arduino Uno and it 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 way, 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 until 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.

1

There are 1 answers

0
dda On
if (serial.available() > 0) {

That's a one-time thing. You should put this in a while loop.

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

Wouldn't hurt to check i after incrementing, too.