Problem when porting from MEGA328 to TINY85/TINY13

Go To Last Post
95 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there.

I have written a little program which outputs PCM audio by mixing four sinewaves with different amplitude and phase (4 phase accumulators run in parallel with envelope data stored in arrays). Timer 0's OVF interrupt calculates the new output amplitude at a frequency of F_CPU/256 (actually it happens at half that freq because the interrupt takes more than one period to complete). Anyway, it works just fine on a 328.

When I take the same code (with minor adjustments in the setup department to cater for different pin and port numbers) and build it for a TINY device, I get audio which is slow and staticky sounding and where the frequency hops around where it shouldn't. It appears I have plenty of memory on the TINY85 at least. Can you think of any reason why the TINY would not be able to perform this kind of task under the same conditions clock speed etc, as the MEGA?

At this stage I am just interested to know if there are some inherent limitations or inabilities of the TINYs of which I'm unaware.

Thanks !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since Tinys do not have a hardware multiplier, many math operations take much longer than on Megas.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MBedder wrote:
Since Tinys do not have a hardware multiplier, many math operations take much longer than on Megas.
Thanks. I had never noticed that info in the datasheets, but I see now that it is there! Any idea how many clock cycles the various types of multiplication take on a TINY?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Write a simple test and simulate - should be several times slower than in Mega.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you did set the fuses for the right clock?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meslomp wrote:
you did set the fuses for the right clock?
Yessir, that I did.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello guys, I am new to this, however, I have a code that works on Arduino's UNO just fine (ATmega328P)... I have checked for memory (3..kb, and 244 bytes for global vars), it looks it fits to AT Tiny 85... The application is only using one PIN and I2C :) 

But, now I am getting bunch of errors, namely certain libraries do not compile. Anyway, I would like to use the Arduino UNO, namely mega and simulate the tiny85 environment, so that I will be able to debug the errors, etc. before I am ready to load the program to tiny.

 

Has anyone tried to do that?

 

BEST.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bear in mind that the Tiny85 does not have a proper USART, SPI, TWI, ... or even proper bootloader hardware.
.
All these things are kludged on any "Arduino" style setup. e.g. TinyWireM.h compared to Wire.h
And since there is no official tiny85 Arduino, there is no official core or set of libraries that the world can rely on.
.
If you have a a simple project that works on the Uno, post the sketch here. Someone might show you how to adapt it for your tiny85.
.
Just saying that you have "unspecified" errors with un-named libraries and tacking onto a 6-year old thread is not going to get you very far.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David cheers, I will do my best and try to work it out via forum. Since there is not official support for tiny, what would you suggest to use otherwise?

 

OK, I am reading the sensor via the wire.h library and the sensor library VCNL4010.h in the Arduino Uno and IDE, works!

 

1) This is my first problem, and I would like to get it working on tiny85!

 

Code that works on mega Arduino:

 

//libraries

#include <Wire.h> // I2C
#include <VCNL4010.h> // sensor https://github.com/SV-Zanshin/VC...

 

VCNL4010 Sensor; // This is my sensor

int value; 
 

void setup(){

//set the LED on pin 11

pinMode(11 , OUTPUT);

 

  // Loop until sensor found
    while (!Sensor.begin()) {
    //Serial.println(F("Error, unable to find or identify VCNL4010."));
    // Wait 2 seconds before retrying  
    delay(2000);                                                              
  } // of if-then we can't initialize or find the device 

}

 

void loop(){

//Use the proximity value to change the brightness of LED

 

//get the proximity from the sensor

value = Sensor.getProximity();

//simple map

value = map(value, 0, 65535, 0, 255);

//now change the brightness

analogWrite(11, value);

}

 

 

Hope this helps!

 

BEST.

 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

How to properly post source code: https://www.avrfreaks.net/comment... (also how to post images)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hobbytronics wrote:

did you know that you can use the Arduino IDE to program ATtiny45 and ATtiny85 8-pin chips?

 

Well you can, and this tutorial will show you how. We will program an ATtiny45/85 with the blink example sketch

 

http://www.hobbytronics.co.uk/ar...

 

and:

The following Arduino commands are supported by the ATtiny library you have installed:

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. May 23, 2017 - 10:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The VCNL4010 library expects to see the Wire.h library

Your Tiny85 will have the TinyWireM.h library

 

You have two choices:

1. you just use TinyWireM.h in your sketch to read and write the VCNL4010 registers directly.   There is nothing complex about this chip.

2. you copy-paste the VCNL4010 library .CPP and .H to tabs in your sketch folder.   Change all refs from Wire to TinyWireM in your local copy.   #include with quotes instead of wedges. Possibly even change names to TinyVCNL4010 

 

I would choose (1).   You would learn how to talk to I2C chips in any future project.

(2) is a little fiddly.  But after you have done it,   you can always reuse your local TinyVCNL4010 library in other projects.

 

The real question is: WHY?

The Uno works.  A Nano is small and cheap.    A Tiny85 is a lot of faffing about.   And for zero gain.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
The application is only using one PIN and I2C 

david.prentice wrote:
Bear in mind that the Tiny85 does not have a proper USART, SPI, TWI, ...

@mu234: You do realise that "TWI" means "I2C" ?

 

david.prentice wrote:
The real question is: WHY?

Absolutely!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. May 23, 2017 - 11:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers, and sorry to not posting the code correctly. 

 

I have tried the solution you are suggesting for programming the tiny and it works, tiny programmed :) 

 

The only problem I am having now is the communication between the sensor and tiny...

 

BEST.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.   ASk your questions properly.    Either paste your code or attach a ZIP of your project.  

 

Say what you expected and what you actually got.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

david.prentice wrote:

You have two choices:

 

Thank you for the summary:

1. you just use TinyWireM.h in your sketch to read and write the VCNL4010 registers directly.   There is nothing complex about this chip.

 

OK, looking at the chip (datasheet) I am trying to access the registers, but to be honest, I am not sure (doing it first time) whether I am doing it correctly for the tiny, see below. How do I use the someByte as my proximity value within the loop, quite a problem without reading a value of variable, not println, serial monitor in place, is there a way to simulate 85 on mega Arduino?

 

#include <TinyWireM.h>  // I2C Master lib for ATTinys which use USI
#define MY_Sensor 0x13 // Address of the slave, sensor

int ledPin = 3;                 // LED connected to digital pin 3 on AT tiny 85
byte someByte;

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  //Blink(3, 2);                  // show we are alive
  
  //i2c 
  TinyWireM.begin();            // initialize I2C lib
  // to send:
  TinyWireM.beginTransmission(MY_Sensor);  // setup slave's address (7 bit address - same as Wire)
  TinyWireM.send(0x87);                    // buffer up bytes to send - can be called multiple times (Proximity reg.)
  someByte = TinyWireM.endTransmission();          // actually send the bytes in the buffer
                                                   // returns (optional) 0 = sucess or see USI_TWI_Master.h for error codes
                                   
  //receive:
  someByte = TinyWireM.requestFrom(MY_Sensor, 7);      // reads 'numBytes' from slave's address
                                                   // (usage optional) returns 0= success or see USI_TWI_Master.h for error codes
  someByte = TinyWireM.receive();                  // returns the next byte in the received buffer - called multiple times
  someByte = TinyWireM.available();                // returns the number of unread bytes in the received buffer
}

void loop(){
    //nothing at the moment
}

 

2. you copy-paste the VCNL4010 library .CPP and .H to tabs in your sketch folder.   Change all refs from Wire to TinyWireM in your local copy.   #include with quotes instead of wedges. Possibly even change names to TinyVCNL4010 

Option 2 sounds interesting, maybe more for my level of programming... however, David you are right, I should aim to learn how to access the reg. :/

 

I would choose (1).   You would learn how to talk to I2C chips in any future project.

(2) is a little fiddly.  But after you have done it,   you can always reuse your local TinyVCNL4010 library in other projects.

 

The real question is: WHY?

The Uno works.  A Nano is small and cheap.    A Tiny85 is a lot of faffing about.   And for zero gain.

 

David, the gain is in having a tiny and sensor on a small plate (both are small), rather then using either nano or uno... which both seem bigger.

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seriously,  you have a Uno.   Use it to educate yourself.   e.g. use my option (1) with the Wire.h library and report results with Serial.println().

 

Try all the Wire library examples.   This will give you experience in how to use the Wire library.

Then everything that you do with Wire can be done with TinyWireM on your Tiny85.

 

You can see what the VCNL4010 registers do by reading its datasheet.

Effectively you write values to some registers.    You "set up a register" before you read.   You read one or more bytes from the device.

Most I2C devices work like this.

 

Look at the actual library CPP file.   There are only two sorts of operation:  writing to a register.  reading from a register.

 

I did give you some clues for (2).   i.e. do a few name changes and everything should "work".   probably without you understanding anything.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers David, I have give it a go with the option 1, but I am having problems with understanding how to use the registers... can you give me an example how to read the device, for example?

 

The data-sheet is available here: https://www.digchip.com/datashee... , I am referring to https://github.com/SV-Zanshin/VC... , 0x13; // Device address, fixed value See here for more: https://github.com/SV-Zanshin/VC...

 

Is this the right way to approach selecting the reg. ? Anyway, I am confused about reading the content from them? :(

An example of that would be much appreciated too! 

 

I guess this is a good example to think about? https://www.arduino.cc/en/Tutori...

 

BEST.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seriously,   just try the Wire.h examples.   Experiment with how to talk to a genuine I2C chip.   e.g. DS1307, AT24Cxxx, PCF8574, ...

 

If the VCNL4010 is the only I2C chip that you have,   do the learning with the VCNL4010. e.g. from your datsheet:

Register #1 Product ID Revision Register
Register address = 81h. This register contains information about product ID and product revision.
Register data value of current revision = 21h.

Learn how to address the Slave (0x13), read the contents of register(0x81).   And if you get a result of 0x21,  you have succeeded !!

 

If you just want to get up and running on the Tiny85 without any understanding,   choose my method (2).

 

I could write everything "for you".   If you want to learn,  or even just enjoy as a hobby,  you have to try things for yourself.

Of course you will have questions.    Ask specific questions when you have a problem.    Please let us know how you get on.

 

Good Luck.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
can you give me an example how to read the device, for example?

Go on - simply putting "VCNL4010" into google gives this as the very first hit:

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, but , I have tried this library, indeed, it works on Arduino MEGA, but it does not compile on AT Tiny :(

 

Have wrote about that on Adafruit forum, see here: https://forums.adafruit.com/view...

 

It is interesting, and I am feeling weird, because I am not an expert to know what could be wrong, hence I have a feeling I am doing something wrong along the way...  

 

I wish I could fix this, use it, but it looks nobody has yet come up with a solution... I guess I will turn to the option 2., however, if you come up with the solution I would be VERY interested to see it :)

 

BEST.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
indeed, it works on Arduino MEGA

So David's point was that you should study this and experiment with it to gain an understanding of how the sensor works - ie, how to deal with its registers.

 

Once you understand how the sensor works - ie, how to deal with its registers - then you can move on to trying to do that on the Tiny!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, thanks for the effort, I got across something, but the numbers are different, then you are suggesting... probably due to bit, bytes... conversion, is it 16bit?

 

The code that I am running on MEGA is giving me the following: Register containing Proximity (0x87) = 23835

 

However, after few seconds of "playing", e.g. I have tried to change the reading of the register to 0x81  // sets register pointer to the Register containing product ID//

as suggested, and I got around 4hundred, however, now is only 0... Wondering why the program is not stable, if the code (see below) is uploaded I cannot get the same readings anymore, but only 0 grrrr...

 

Any comments what is wrong, going on... are very welcome!

BEST

//code for MEGA

#include "Wire.h" // I2C
#define Sensor 0x13 // sensor's (slave) address

byte someByte;
int reading = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  //delay(30);
}

void loop() {

  // adapted from: https://www.arduino.cc/en/Tutorial/SFRRangerReader
  Serial.println("1");
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(Sensor); // transmit to device
  // the address specified in the datasheet is 224 (0xE0)
  // but i2c adressing uses the high 7 bits so it's 112
  Serial.println("2");
  // read proximity
  Wire.write(byte(0x87));      // sets register pointer to the Register containing Proximity
  //Wire.write(byte(0x81));      // sets register pointer to the Register containing product ID
  Wire.endTransmission();      // stop transmitting
  Serial.println("3");
  // step 2: wait for readings to happen
  delay(70);                   // other datasheets suggests at least 65 milliseconds
  //  // read ambient
  //  // step 3: instruct sensor to return a particular echo reading
  //  Wire.beginTransmission(Sensor); // transmit to device
  //  Wire.write(byte(0x85));      // sets register pointer to the Register containing ambient data
  //  Wire.endTransmission();      // stop transmitting
  // step 4: request reading from sensor
  Wire.requestFrom(Sensor, 2);    // request 2 bytes from slave device
  // step 5: receive reading from sensor
  if (2 <= Wire.available()) { // if two bytes were received
    reading = Wire.read();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read(); // receive low byte as lower 8 bits
    Serial.print("And the reading is: ");
    Serial.println(reading);   // print the reading
  }
  Serial.println("end of cycle");
}

 

Bravo!!!

Last Edited: Wed. May 24, 2017 - 09:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

YAY just got the sensor back on line :)

 

and I get on reg. 0x81 = 8448, however, I am still getting 0 on the reg. 0x87, which should give me proximity ... until I have used the library functions and set it back to work :) Indeed,  I can fetch certain data from the reg. however, now it is time to start write to reg. rather then just reading of them.

 

Any suggestions how to do that?

 

BEST.

 

PS indeed I should turn on continues reading of proximity, however, I am fetching prox values in a loop, would expect to receive a different number every time... but the number is equal each loop/cycle, hmm, of course the code I am having is only reading from the reg... so, the value is the same all the time? 

 

 

Bravo!!!

Last Edited: Wed. May 24, 2017 - 09:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//code for MEGA

#include "Wire.h" // I2C
#define SENSOR 0x13 // sensor's (slave) address

byte someByte;
int reading = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(SENSOR); // transmit to device
  Wire.write(byte(0x81));      // sets register pointer to the Register containing ID
  Wire.endTransmission();      // stop transmitting
  delay(70);                   // other datasheets suggests at least 65 milliseconds
  Wire.requestFrom(SENSOR, 1);    // request 1 bytes from slave device
  while (Wire.available()) { // if bytes were received
    reading = Wire.read();  // 
    Serial.print("And the reading is: 0x");
    Serial.println(reading, HEX);   // print the reading
  }
  delay(10000);                // read ID every 10 seconds.
}

Untested.   I just removed a lot of your dross.    The convention is to use CAPITALS for a macro like SENSOR.   Your spelling is fine.

 

Note that the write_register() is pretty simple.   And requestFrom() operation pretty easy too.

Sometimes you select a register AND write data to it.   i.e. you add another write_byte(data)

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers David, compile and run it, works like a charm and print's out 0x21, I guess this code will work on tiny? ok, next step is to start writing to reg., will keep you updated and thank you again for your help so far!

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Do everything on your Uno.   You can even steal sequences from the VCLN4010 library.

 

When everything is working 100%,   build with TinyWireM on your Tiny85.   It should work 100% too.

 

The same principle applies to learning C or learning C++.   Do the exercises and experiments on the PC.   Life is easier that way.   PC compilers have good IDEs, editors,  simulators,  good display,  ...

 

Once you have completed the task on the PC,   build on your Uno.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After few days I become eager to test the option 2, interestingly it works, and it is much easier than going directly to using reg., as in option 1. Anyway, I am attaching you the WORKING libraries for the VCNL4010, adopted from the author and my example of using the sensor to control the led... Please check it out and let me know, if the program actually works. According to my setup, it is running for the last two days and its seems its ok, however, I am wondering whether I am using the millis() ok? In addition, did I use the fading ok? For instance, how could I change the envelope of the fade? So far it is linear and as someone of you knows, it does not fade smoothly. Hence I am thinking to stop the fade at 4% - 8%... 

 

Thank you all for pitching in the ways how to tackle this problem!

 

Looking forward to your comments and I hope this is not my last project here :)

 

PS The code is not memory greedy : 

 

Sketch uses 3192 bytes (38%) of program storage space. Maximum is 8192 bytes.
Global variables use 58 bytes (11%) of dynamic memory, leaving 454 bytes for local variables. Maximum is 512 bytes.

Attachment(s): 

Bravo!!!

Last Edited: Fri. May 26, 2017 - 06:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have created an Arduino project following "my" suggestions i.e. my method (2)

 

1.   copied the regular VCN4010.cpp and VCN4010.h to local tabs: local_VCN4010.cpp and local_VCN4010.h

2.   my edits are described in the comments.

3.   put your three INO files into a single mu234_VCNL4010.ino

4.   the INO should use a VCN4010 object from the class defined in the local project files.

 

5.   I have also edited the original VCN4010.cpp to describe a completely new Tiny_VCN4010 class in Tiny_VCN4010.cpp

6.   you should be able to test it in your project.

 

7.   if the Tiny_VCN4010 works ok,  you could place it in your global User libraries.   i.e. move the Tiny_VCN4010.cpp and Tiny_VCN4010.h files to libraries/Tiny_VCN4010.    Restart IDE afterwards.

 

Untested.    I do not have a TinyCore available from my Arduino IDE.

 

I suggest that you try all the strategies.  i.e. local,  local with different class name,   global with different class name.

 

If everything goes ok,   you can see just how simple it is to develop and test a local version of a class.   e.g. fix bugs or add features.

When done,   move it to the global library directory.    It is always available to new projects.   Or you could publish (ask the original author first)

 

David.

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers David, and all the best. 

 

NOTE: the sketch needs improvements, especially with regard to millis() and delay(). In addition, the fading can be improved with creating the fading envelope with the array... 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did my untested example work straight out of the box?
.
The general principle is develop on Uno. Port debugged project to Tiny.
.
It shows how simple it is when TinyWireM has the same API as Wire library. And how the IDE handles local project files.
All the same, it is worth learning how to use the basic Wire methods for yourself. i.e. my method (1)
.
David.

Last Edited: Tue. May 30, 2017 - 08:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi David,

it compiles without errors, however, I have not yet tested on my tiny/arduino/sensor setup...

will report back in few days,

best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The attached code works just fine, however, I need to sort out the BOD!

 

It seems that when the setup (tiny and led) is turned off and (after few seconds) back on, the program/application starts to behave weird, does not work properly anymore. Has any of you programmed the "fuse" for at tiny 85?

There seem to be several instructions how to do that, however, what is the right voltage threshold, for example, when talking about BOD. In addition, should I also consider other fuses, what is actually useful for the application at hand?

 

Best.

 

PS fyi I am still working on Arduino as ISP... 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your AVR project is designed for running at 5V,  BOD=4.3V

If your AVR project is designed for running at 3.3V,  BOD=2.7V

If your AVR project is designed for running at 2.5V,  BOD=1.8V

 

There is something wrong with your Software / Hardware if the BOD fires.

If you follow the datasheet advice for 100nF ceramic capacitors on the power pins,   they should look after switching glitches.

 

Your power supply should be capable of supplying typical AVR currents.   A rectified 50Hz needs sufficient reservoir capacitors to sustain between cycles.   e.g.  100uF or even 1000uF

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
There is something wrong with your Software / Hardware if the BOD fires.
But surely BOD will always activate as power rails both ramp up and die away every time the device is repowered? I guess you mean there's a problem if the power rails are supposedly "stable" and even then BOD activates?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bot of you are on spot! 

 

David thanks for the V... BOD references, indeed, my power supply is 5V so will active the BOD at 4.3V, btw wondering to turn on the POR (Power-on Reset) too ?

 

I guess, the major issue why I need to deal with the boot-loader, namely fuses, is that the voltage of my power supply "rumps-up" when turned on, and it takes some "time" to become stable at 5V... 

 

As 'clawson' pointed out, the rails of the power supply are "supposed to be stable", at some point, however, the question goes in the direction if the device is turned on and off, or if and when the power oscillates... ? I guess the BOD is the right approach to deal with that, so that the tiny will reset/be ready for such environments ?

 

Best.

 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Did my untested example work straight out of the box? . The general principle is develop on Uno. Port debugged project to Tiny. . It shows how simple it is when TinyWireM has the same API as Wire library. And how the IDE handles local project files. All the same, it is worth learning how to use the basic Wire methods for yourself. i.e. my method (1) . David.

 

Tested and YES, it works, all good...

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Out of curiosity, when I play with Tiny and Arduino, sometimes I get this error:

 

avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.

An error occurred while uploading the sketch

 

 

If I try and upload the sketch again, works!

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

HI All

 

I am getting back to you for help!

 

I have previously managed to upload the code from Arduino to tiny85/45, but I have reprogrammed the code, namely to make it better, at least on Arduino's mega it works! But now when I upload the code on tiny, it does not work properly :/ Namely the fading function works, while the fade.in and fade.out does not... grrr wondering where did I go wrong? In addition, why it works on Arduino's mega and it does not on tiny?

 

...

void loop() {  
    if (timer0 > interval0) {
      if (Sensor.getProximity() >= HT && ledOn == false) {
        ledState = !ledState;
        ledOn = true;
        timer0 = 0;
      }
    }
    if(timer0 > 20){   //if sensor prox has dropped and is below LT
      if (Sensor.getProximity() <= HT && Sensor.getProximity() <= LT && ledOn == true ) {
        ledOn = false;
        timer0 = 0;
      }
    }
    //ON-OFF LED
    // "fade in" function and "turn on" the led
    if (ledState && ledOn == true && fadeIo) {
      fadeIn();
    } 
    // "fade out" and turn off the led
    else if (!ledState && ledOn == false && !fadeIo) {
      fadeOut();     
    }

    //FADINNG LED
    if (timer0 > interval1) {
      ledState=1;
      if (Sensor.getProximity() >= HT && Sensor.getProximity() > LT) {
        fading();
      }
    }
}  //END OF MAIN LOOP

/////////////////////////// -----> FUNCTIONS <------ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//FADE-IN (turn on to start from you have left of with fading function)
void fadeIn() {
  //Serial.println("fade-in");
  //go thru the table, start at the zero and go to the end of table
  for (int t = 0; t < brightness; t = t + 3) {   //why I cannot use byte here??
    //Serial.println(t);
    // fade the led with the table values
    analogWrite(ledPin, table[t]);
    //Serial.print("\t"); Serial.println(t);
    delay(6);
  } // end of for loop
  //the led is now on
  analogWrite(5, table[brightness]);
  //led is On, fadeIo = 0, next time we need fade-out, before it was toggle fadeIn =! fadeOut;
  fadeIo = 0;
} //end of "fed in" function and turn on the led

//FADE-OUT (turn on to start from you have left of with fading function)
void fadeOut() {
  //go thru the table, start at the top and go toward zero
  for (int t = brightness; t > 0; t = t - 3) {  //why I cannot use byte here??
    //Serial.println(t);
    // fade the led with the table (1) values
    analogWrite(5, table[t]);
    //Serial.print("\t"); Serial.println(t);
    delay(6);
  } // end of for loop
  analogWrite(5, 0);
  //led is Off, we need fede in next time (fade =1)
  fadeIo = 1;
}

//FADING
void fading() {
  //fade up, if the led is off (brightness is 0), otherwise begin from where it was
  brightness = brightness + fadeAmount;
  //Serial.print("\t"); Serial.print(brightness); Serial.print("\t"); Serial.println(table[brightness]);  //go thru the table and use the value to fade the led
  analogWrite(ledPin, table[brightness]);
  //Serial.print("\t"); Serial.print(brightness);
  if (brightness <= 0 || brightness >= 254) {
    fadeAmount = -fadeAmount;
    if (brightness >= 254) {
      ledBlinkEr(ledPin, 2, 10);
    }
    // reset the timer
    //timer0 -= interval0;  // works without !!!???
    analogWrite(5, fadeAmount);
  }
  delay(5);  // faking the length of fade
}

...

Not all code is there, but I guess enough to start investigating the problem :) anyway, let me know, if you need more information!

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Tiny85 has not got many pins.   Write down your pin budget.  Or preferably a schematic.

 

You probably need to change the pin usage for the PWM (analogWrite)

I suspect that TinyWireM only works on specific pins.

 

The third party TinyCore makes all sorts of compromises.   e.g. PWM pins, timers, ...

It is fine for a Blinky.   You need to know what you are doing for more complex projects.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David! 

 

Thank you very much for your concern. Indeed, I have been using Arduino's pin assignments within the functions, hence it did not work on Tiny , grrrr... Changed that to my variables, which I am using within the program and it works like a charm :)!!!

 

Thank you very much!!!

 

BTW what would you do to reduce the size of the global vars memory??? Want to put that code on Tiny45 :)

 

Best.

 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
what would you do to reduce the size of the global vars memory?

The obvious things:

  • Have fewer global variables;
  • Use smaller global variables.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Turn on verbose compile from IDE File Preferences
.
See what the memory report is, e.g. Flash and SRAM.
Paste it here.
.
I would guess it will fit in Tiny45 ok. You might need to reduce TinyWireM buffer.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you awneil, working on that :) indeed, it is obvious more than it can be practiced :)

 

David here is the compile Verbose:

 

Sketch uses 3314 bytes (40%) of program storage space. Maximum is 8192 bytes.
Global variables use 312 bytes (60%) of dynamic memory, leaving 200 bytes for local variables. Maximum is 512 bytes.

 

 

Best.

Bravo!!!

Last Edited: Tue. Jul 11, 2017 - 08:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on. Delete all that dross. All you needed to show was Flash is 3314 bytes, 312 bytes of dynamic RAM.
You could fit in Tiny45 Flash. There is NO chance of fitting in tiny45 SRAM.
.
If you are going to make 1000 of these, it would be worth squeezing. Post your complete project. How much is the job?
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers David,

Sketch uses 3314 bytes (40%) of program storage space. Maximum is 8192 bytes.
Global variables use 312 bytes (60%) of dynamic memory, leaving 200 bytes for local variables. Maximum is 512 bytes.

 

Indeed, oversized for the tiny45 global vars, need to remove some of the global vars... 45 offers 256 bytes...

 

Best.

 

PS what do you mean by how much is the job?

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:

PS what do you mean by how much is the job?

 

If this is a commercial project,    how much would you be prepared to pay for a Tiny45 solution (or a Tiny25 solution)?

I am sure that we could find a "Good Cause" that could benefit.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, I am honoured by your question, but unfortunately I am just an amateur who is learning... I will get back to you when opportunities arise! Btw it would be good to know where would you like to be engaged?

Is it even possible to get down to tiny25 :) for me it is even hard to reach tiny45, anyway, I though would try it out since I am already close to it :)

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post your current project. We can probably guess what reduction is possible.
.
It would be an academic exercise. There is little point in money saving when you possess tiny85.
Especially for a one-off.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, you are right,

 

most of the code has already been posted, however, I do not see much reduction of memory there. Therefore, I was thinking to start reducing the size of the global vars, namely the byte table[] which has 255 values for fading, etc. I am thinking to have a smaller table, like :

// table (30)
byte table1[] =
{ 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10,
  12, 12, 12, 12, 12,
  20, 25, 25, 25, 25,
  180, 180, 180, 190, 190,
  200, 200, 200, 200, 220,
};

... and make the lin interpolation between the values, has any of you done that? In addition, will this make a change in terms of memory?

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ZIP up the project and attach it. So we can see exactly what you have got.
I don't see why anyone should have to piece together the snippets from your posts.
.
Seriously, other readers might build it. And advise exactly how you can reduce size. They might even post back a 'solved' project.
.
David.
.
Please edit #44. It puts people off reading your thread. [done that - Cliff]

Last Edited: Tue. Jul 11, 2017 - 08:05 AM

Pages