Go Down

Topic: IR Code on Attiny gives error (Read 15983 times) previous topic - next topic


I am using this code
Code: [Select]
* IRrecord: record and play back IR signals as a minimal
* An IR detector/demodulator must be connected to the input RECV_PIN.
* An IR LED must be connected to the output PWM pin 3.
* A button must be connected to the input BUTTON_PIN; this is the
* send button.
* A visible LED can be connected to STATUS_PIN to provide status.
* The logic is:
* If the button is pressed, send the IR code.
* If an IR code is received, record it.
* Version 0.11 September, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com

#include <IRremote.h>

int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

void setup()
  irrecv.enableIRIn(); // Start the receiver

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      else {
        // Space
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      Serial.print(rawCodes[i - 1], DEC);
  else {
    if (codeType == NEC) {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
    else if (codeType == SONY) {
      Serial.print("Received SONY: ");
    else if (codeType == RC5) {
      Serial.print("Received RC5: ");
    else if (codeType == RC6) {
      Serial.print("Received RC6: ");
    else {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;

void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    else {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
  else if (codeType == SONY) {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    else {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");

int lastButtonState;

void loop() {
  // If button pressed, send the code.
  int buttonState = digitalRead(BUTTON_PIN);
  if (lastButtonState == HIGH && buttonState == LOW) {
    irrecv.enableIRIn(); // Re-enable receiver

  if (buttonState) {
    Serial.println("Pressed, sending");
    digitalWrite(STATUS_PIN, HIGH);
    sendCode(lastButtonState == buttonState);
    digitalWrite(STATUS_PIN, LOW);
    delay(50); // Wait a bit between retransmissions
  else if (irrecv.decode(&results)) {
    digitalWrite(STATUS_PIN, HIGH);
    irrecv.resume(); // resume receiver
    digitalWrite(STATUS_PIN, LOW);
  lastButtonState = buttonState;

from this tutorial
http://www.arcfn.com/2009/09/arduino-universal-remote-record-and.html to program an attiny85 to record and replay ir signals.

The program works fine on the atmega 328. I know I can program the  attiny85 with arduino as ISP because I have successfully loaded multiple sketches onto it in this way. However, when I attempt to load the sketch to the attiny85 I get this error message:
Code: [Select]
IRrecord.cpp: In function 'void setup()':
IRrecord.cpp:36:3: error: 'Serial' was not declared in this scope
IRrecord.cpp: In function 'void storeCode(decode_results*)':
IRrecord.cpp:55:5: error: 'Serial' was not declared in this scope
IRrecord.cpp:72:37: error: 'DEC' was not declared in this scope
IRrecord.cpp:78:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:86:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:89:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:92:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:95:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:96:30: error: 'DEC' was not declared in this scope
IRrecord.cpp:99:5: error: 'Serial' was not declared in this scope
IRrecord.cpp:99:36: error: 'HEX' was not declared in this scope
IRrecord.cpp: In function 'void sendCode(int)':
IRrecord.cpp:109:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:113:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:114:33: error: 'HEX' was not declared in this scope
IRrecord.cpp:119:5: error: 'Serial' was not declared in this scope
IRrecord.cpp:120:31: error: 'HEX' was not declared in this scope
IRrecord.cpp:131:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:132:33: error: 'HEX' was not declared in this scope
IRrecord.cpp:137:7: error: 'Serial' was not declared in this scope
IRrecord.cpp:138:33: error: 'HEX' was not declared in this scope
IRrecord.cpp:144:5: error: 'Serial' was not declared in this scope
IRrecord.cpp: In function 'void loop()':
IRrecord.cpp:154:5: error: 'Serial' was not declared in this scope
IRrecord.cpp:159:5: error: 'Serial' was not declared in this scope

What is going wrong?




Does the attiny85 have a hardware UART?


Just the raw attiny85 chip. My limited understanding was that UART refers to the hardware necessary to connect to the computer via USB. I am using the arduino's usb UART as i am using the arduino duemilanove as an ISP programmer to program the attiny85 on a breadboard.


A UART gives a hardware serial port. Another chip does serial to usb conversion. I don't think you can use hardware serial on an attiny85.


Yeah, attinys don't have hardware serial.  Avrfreaks seems to have a soft serial solution, although I haven't tried it because I'm still waiting for my attiny in the mail.  http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_type=project&item_id=1895


This came up earlier today: http://arduino.cc/forum/index.php/topic,91125.0.html

Coding Badly

Just a short-circuit link to the good bit...


Coding Badly and dxw00d,

How do I implement the solutions you link to into my code?

Thank you so much!


Coding Badly

Download the Tiny Core.
Install the Tiny Core.
Ensure your sketch compiles.
Upload your sketch.
Connect TTL-serial-to-USB converter (an Arduino or compatible board will work) to the ATtiny processor.
Run your favourite terminal application.


Is it ok to use the tiny core you linked to for arduino version 1.0 if I have ardiuno 022 running? I can only get arduino as ISP running properly with 022.

Coding Badly


Ah ok. This was my confusion. I am using the arduino as an ISP. In the High-Low tech tutorial with their tiny core files there was a board selection for 'attiny85 with arduino as ISP' This new core wants me to program the attiny85 directly as I understand it. How do I do that?

Coding Badly


upload.using defines which programmer is used.  Tiny Core is configured to use a board running ArduinoISP (arduino:arduinoisp).


Ok I installed the core files.

I uploaded the ISP sketch onto the arduino

I opened the blink example sketch

I selected the Attiny85 from the board menu

I attempt to compile the blink example sketch and get this error:

Code: [Select]
In file included from /usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h:44:0,
                 from /usr/lib/gcc/avr/4.5.3/../../../avr/include/avr/delay.h:37,
                 from /home/will/sketchbook/hardware/tiny/cores/tiny/wiring_private.h:32,
                 from /home/will/sketchbook/hardware/tiny/cores/tiny/WInterrupts.c:37:
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected identifier or '(' before 'double'
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected ')' before '>=' token

Coding Badly


Go Up