Serial was not declared in this scope! Please help

Hi! I’m working with on an IR Controlled Servo project. The code runs fine on the Arduino UNO, but I’m trying to shrink my project using an ATtiny85 and the Arduino UNO as ISP, but it says , “Serial” was not declared in this scope. I want to keep the Serial references on my code. Can anyone please help me?
this is the code:

// This code is for controlling servo motor with IR remote control
// When clicking at any of two buttons the motor is toggling between the rotation and stop

#include <IRremote.h> //must copy IRremote library to arduino libraries
#include <Servo.h>
#define plus 0x925D5B5D //clockwise rotation button
#define minus 0xCB3D6F7D //counter clockwise rotation button
#define BUTTON_1 0x6BFD8B01 //Moves servo to 0 degrees
#define BUTTON_2 0x42640C99 //Moves servo to 90 degrees
#define BUTTON_3 0xB1EFBA9D //Moves servo to 180 degrees

const int greenLed = 1;
const int redLed = 2;

int RECV_PIN = 0; //IR receiver pin
Servo servo;
int val; //rotation angle
int16_t pos; // variable to store the servo position
int16_t speed; // Number of degrees to move each time a left/right button is pressed
//bool cwRotation, ccwRotation; //the states of rotation

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600); I REPLACED THIS TEMPORARILY FOR THE NEW CODE
pos = 90; // start at midpoint 90 degrees
speed = 3; // servo moves 3 degrees each time left/right is pushed
servo.write(pos); // Set initial position
irrecv.enableIRIn(); // Start the receiver

pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);

}

void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX); // TEMP REMOVED FOR NEW CODE
irrecv.resume(); // Receive the next value

switch(results.value) {
case BUTTON_1: pos=0*90;
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println(“Gate Unlocked”); break;

case BUTTON_2: pos=1*90;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, LOW);
Serial.println(“Middle Ground”);break;

case BUTTON_3: pos=2*90;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
Serial.println(“Gate Locked”);break;
}
}
servo.write(pos); // tell servo to go to position in variable ‘pos’
delay(20); //General speed
}

The Tiny's don't have hardware serial. You can use SoftwareSerial but it will take up most of the Tiny memory.

Next time use code tags. You can even edit your original post to add the tags as per the "How to use this forum" instructions.

Hi MorganS and thank you very much for your prompt reply! I’m kind off new to the Arduino world so I would appreciate it if you expand a bit more on your reply. Besides from adding the #include <SoftwareSerial.h> library, what else should I do? How can I modify the code can still display the ‘Serial’ data?

Also, I’m not sure what do you mean by ‘code tags’, could you also elaborate a little bit more in regards to this matter! Your help is greatly appreciated! Thanks and have a great day! :slight_smile:

Code tags, read the how to use this forum sticky post, point 7.

Forget using any sort of serial with the ATtiny85, you have no serial hardware and software serial takes up too much space. Just remove all referance to serial from your code.

You could try DrAzzy’s ATTiny Core. That has software serial built in to the core. So your Uno sketch may work unaltered. But using serial in your sketch will still take more flash. With all those other libraries as well… You will have to try and see if it will fit.

If it does fit, how do you intend to view the serial output? If you have an Uno R3 with removable chip, you might be able to use that. But a separate USB to serial adaptor is much more convenient.

PaulRB:
You could try DrAzzy's ATTiny Core.

Please ignore this - I got mixed up with a different Thread - apologies

I'm not sure if that covers the Attiny13.

I wrote a very small program to send serial data from an Attiny45 some time ago, If the OP really needs serial output I will try to find it. But IIRC it was far from elegant.

...R

Robin2:
I’m not sure if that covers the Attiny13.

Robin, the OP is using attiny85. Are you confusing this with another thread? I think I read one in last day or so where both chips were mentioned. BTW you are correct to say DrAzzy’s core does not cover tiny13, but it does cover tiny85.

PaulRB:
Robin, the OP is using attiny85. Are you confusing this with another thread?

You are quite right - it was a senior moment.

Apologies for any confusion.

...R

SoftwareSerial should work on the ATTiny85. See the library documentation for examples. I don't think the bloat is that bad.

My core also includes a built-in software serial implementation named Serial on the analog comparator pins (it uses the AC interrupt, so that you can use the more generally useful PCINTs without a conflict). But despite the name (chosen for compatibility) - it's not hardware serial, it's just a different implementation of software serial with the same limitations.

Either way, you have two issues:

  • You're not using a crystal, and the internal oscillator is kinda lousy if you don't tune it (which is a pain in the ass), and even if you do, changes in voltage/temperature will throw that off - factory cal is +/- 10%, but serial requires +/- 2%. In practice, most tiny85's, at 5v and room temperature, will work for serial with the factory calibration, but this is not guaranteed.

  • Software serial sucks! All send and receive is blocking (ie, it can't do anything else). On real serial, sending happens in the background, with an interrupt popping each char off the buffer as the transmit of the last one is completed - in software serial, it blocks until the whole send is completed. On real serial, receiving happens in the background, putting chars in the buffer from the interrupt; on software serial, the same thing happens, except the interrupt has to run while it's receiving the character, instead of very briefly after each character. Because of this, software serial is half-duplex; it can't send and receive at the same time, and if you try, you generally end up sending and receiving gibberish.

In my projects, I avoid SoftwareSerial like the plague for these reasons - which is of course a big part of why I love the ATTiny841 and ATTiny1634 - they have not one, but TWO hardware serial ports - and their internal oscillator is cal'ed to within 2% (but only at 3.3v - at 5v, serial on 841/1634 won't work without either bumping OSCCAL down or using an external crystal)

I’ve used the soft serial in your core in a couple of projects, and it works quite well enough.

For example, this admittedly very short sketch to scan 16 buttons:

// 4x4 switch matrix scanner
// for ATtiny84
// Using core: https://github.com/SpenceKonde/ATTinyCore
// Pin mapping: Clockwise
// PaulRB Jan 2017

const byte rowPins[4] = {3, 2, 4, 0};
const byte colPins[4] = {10, 8, 9, 7};

unsigned int switchData; // Holds all 16 switch states in binary, 0 = switch pressed

void setup() {
  Serial.begin(9600); // Outputs on pin 1
  for (byte i = 0; i < 4; i++) {
    pinMode(rowPins[i], INPUT_PULLUP);
    pinMode(colPins[i], INPUT_PULLUP);
  }
}

void loop() {

  unsigned int newSwitchData = 0;

  // Scan each column in turn
  for (byte j = 0; j < 4; j++) {
    
    // Enable switches in this column to pull a row LOW
    pinMode(colPins[j], OUTPUT);
    digitalWrite(colPins[j], LOW);

    // Read the switches on this row
    for (byte i = 0; i < 4; i++) {
      newSwitchData = newSwitchData << 1 | digitalRead(rowPins[i]);
    }

    // Disable the column again
    pinMode(colPins[j], INPUT_PULLUP);
  }

  // Has the switch state data changed?
  if (newSwitchData != switchData) {
    switchData = newSwitchData;
    // Send new switch state data as two bytes
    Serial.print((char) lowByte(switchData));
    Serial.print((char) highByte(switchData));
  }
}

Takes only 22% of flash on tiny84.

Guys, thank you so much for your replies! I truly appreciate all of these suggestions and will try to work on them one by one. I will start with Grumpy_Mike's suggestions of removing all serial references. After doing so, i get this error code:

In file included from C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:22:0:

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp: In member function 'void IRsend::mark(int)':

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremoteInt.h:261:31: error: 'TCCR2A' was not declared in this scope

#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))

^

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:294:3: note: in expansion of macro 'TIMER_ENABLE_PWM'

TIMER_ENABLE_PWM; // Enable pin 3 PWM output

^

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:88,

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

from C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremoteInt.h:21,

from C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:22:

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremoteInt.h:261:45: error: 'COM2B1' was not declared in this scope

#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))

^

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:294:3: note: in expansion of macro 'TIMER_ENABLE_PWM'

TIMER_ENABLE_PWM; // Enable pin 3 PWM output

^

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:88,

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

from C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremoteInt.h:21,

from C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:22:

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremoteInt.h:263:44: error: 'OCIE2A' was not declared in this scope

#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))

^

C:\Users\Alan\Documents\Arduino\libraries\Arduino-IRremote-master\IRremote.cpp:349:3: note: in expansion of macro 'TIMER_ENABLE_INTR'

TIMER_ENABLE_INTR;

^

exit status 1
Error compiling for board ATtiny25/45/85.

Sorry guys, it's late, my brain is fried and I feel like a complete NOOB! Any help that you can provide in regards to this error code will be greatly appreciated! Thanks and have a good one! :slight_smile:

Basically you will not get this error simply by removing all called to the Serial libiary. It looks like you have missed out an #include file as well.

We can say no more because we have not seen your before and after code.

It looks like you are trying to run atmega 328 code on your attiny85. That's just not going to work. You'll have to make sure that any libraries you use are either specific to the attiny85 or at least don't include lines that are specific to the atmega328 like the timer register mentioned in the error code which simply does not exist on the tiny.

Yes - sorry I forgot about the ATtiny85 aspect.

The Arduino-IRremote libiary can not be run on the ATtiny.

I’m sorry guys, last night I forgot to post my code! (Sorry it was kind off late over here). But your suggestions make sense! So since I can’t use the IRremote.h Library on the ATtiny85, do you know any other IR libraries that I can use that wil be compatible with the ATtiny85 or suggest using another IC from the Atmel family that will have Hardware Serial and accept the Arduino IRremote.h Library?? :slight_smile:

``#include <IRremote.h> //must copy IRremote library to arduino libraries
//#include <SoftwareSerial.h>
#include <Servo.h>
//#define plus 0x925D5B5D //clockwise rotation button
//#define minus 0xCB3D6F7D //counter clockwise rotation button
#define BUTTON_1 0x6BFD8B01 //Moves servo to 0 degrees
#define BUTTON_2 0x42640C99 //Moves servo to 90 degrees
#define BUTTON_3 0xB1EFBA9D //Moves servo to 180 degrees

const int greenLed = 1;
//const int redLed = 2;

int RECV_PIN = 0; //IR receiver pin
Servo servo;
int val; //rotation angle
int16_t pos; // variable to store the servo position
int16_t speed; // Number of degrees to move each time a left/right button is pressed
//bool cwRotation, ccwRotation; //the states of rotation

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
//Serial.begin(9600); I REPLACED THIS TEMPORARILY FOR THE NEW CODE
delay(5000);
servo.attach(3);
pos = 90; // start at midpoint 90 degrees
speed = 3; // servo moves 3 degrees each time left/right is pushed
servo.write(pos); // Set initial position
irrecv.enableIRIn(); // Start the receiver

pinMode(greenLed, OUTPUT);
//pinMode(redLed, OUTPUT);

}

void loop()
{
if (irrecv.decode(&results)) {
//Serial.println(results.value, HEX); // TEMP REMOVED FOR NEW CODE
irrecv.resume(); // Receive the next value

switch(results.value) {
case BUTTON_1: pos=0*90;
digitalWrite(greenLed, HIGH);
//digitalWrite(redLed, LOW);
//Serial.println(“Gate Unlocked”); break;

case BUTTON_2: pos=1*90;
//digitalWrite(greenLed, LOW);
//digitalWrite(redLed, LOW);
//Serial.println(“Middle Ground”);break;

case BUTTON_3: pos=2*90;
//digitalWrite(greenLed, LOW);
//digitalWrite(redLed, HIGH);
//Serial.println(“Gate Locked”);break;
}
}
servo.write(pos); // tell servo to go to position in variable ‘pos’
delay(20); //General speed
}

Paste your sketch between the code tags, not after them!

If you want to shrink your project from Uno size, you could use a pro-mini. You will need to buy a usb-serial adaptor also, but you can often buy them as a pair. The adaptor does not go into the permanent project, you can keep that in your toolbox for future projects.

Paul, thank you so much for this suggestion! I guess for the time being, this might be my best option given my poor coding skills! Will I be able to use the same code I run in the arduino UNO on the pro-mini? If so, I'll buy it today! Please let me know!

p.s. I apologize about the code tags confusion, this gives you an idea of how much of a NOOB I am! LOL Thanks again!

Uno and Promini can run the exact same code. You can even bootload the Promini with the same Bootloader and then select Uno as the board type for downloads (which is what I do for all my '328P projects).

Great, thank you so much CrossRoads, just to double check, I'll order the:

Development Boards & Kits - AVR Arduino Pro Mini 328 - 5V/16MHz

http://www.mouser.com/ProductDetail/SparkFun/DEV-11113/?qs=sGAEpiMZZMuWWq7rhECaKWnONxwX7kb29N9RTtea6CI%3D

This board should be able to run the same code I was running on my Arduino UNO and use the same Servo.h and IR Remote.h libraries, right? :slight_smile:

Yes. You need an FTDI Basic or equivalent as well for the USB/Serial interface to download code.
http://www.mouser.com/ProductDetail/Gravitech/ARD-PROGRAMMER/?qs=sGAEpiMZZMvq007EO%2bXAYaAHWNCaRpQ%2b

I buy them from www.tinyosshop.com. $6.90 + shipping, mini-USB and micro-USB connector types available.


I also use MIKROE483 from Mouser when I want to install an FTDI module on a board.