Now learning Attiny85- code seems to revert back to example sweep despite successful load

Things are going well now that I've sorted out how to get code onto Attiny85 IC. My program is meant to use a SMD led as a sensor and then take the average reading and move a micro servo to a corresponding position. The nice thing about this code is I also wrote it for a XIAO board and it works pretty well I'm delighted to say. This is just a good candidate to try to get the hardware size down to an Attiny85 so here I am. I did have to use the Servo8bit library but having loaded the example sweep program that seems to work also! yay! The only trouble now is that when I load my custom program, it seems to load, it says its successful, I don't get any error msgs but then after it thinks it over, it seems to revert to the previous example sweep program. Strange, right? I don't know how that happened but with only 1430 bytes it shouldn't be filled up yet, and even if it did, I wouldn't guess it would be able bring in a backup copy of the example... do I have to clear the old code out somehow? Just a stab in the dark there.. thanks for any help! Code below:

#include <Servo8Bit.h>

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.

//3 suggested color combinations for LED are Light Green, Yellow, & Violet.  These result in readings
//averaging 436, 373, & 500 respectively using a SMD blue LED as a sensor. LGreen is set to center by default.

Servo8Bit myServo;  // create servo object to control a servo
int pos = 60;   // variable to store the servo position

const int numReadings = 50;   //1st group # of readings
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int blue = 0;                // the blue


int inputPin = A2;               //1st input (blue?)  A10 for XIAO


void setup() {
  {
 { 
  //myServo.attach(1,544,2400);  //this is the default values
  myServo.attach(1,544,2200);  //attach the servo to pin PB1      
}
}
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
     }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }


  // calculate the blue:
  blue = total / numReadings;
  // send it to the computer as ASCII digits
  //Serial.println ("BlueLED:");  //remove for tiny
 // Serial.println(blue);         //remove for tiny
  if (blue >= 300 && blue <= 410) pos = 0;   //yellow
  if (blue >= 411 && blue <= 470) pos = 60;  //Light Green
  if (blue >= 470 && blue <= 600) pos = 120;    //violet

  myServo.write (pos);
  delay(20);        // delay in between reads for stability
}

Here is some of the status, maybe we can learn something here?:

avrdude: Version 6.3-20190619
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

     System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

     Using Port                    : COM3
     Using Programmer              : stk500v1
     Overriding Baud Rate          : 19200
     AVR Part                      : ATtiny85
     Chip Erase delay              : 400000 us
     PAGEL                         : P00
     BS2                           : P00
     RESET disposition             : possible i/o
     RETRY pulse                   : SCK
     serial program mode           : yes
     parallel program mode         : yes
     Timeout                       : 200
     StabDelay                     : 100
     CmdexeDelay                   : 25
     SyncLoops                     : 32
     ByteDelay                     : 0
     PollIndex                     : 3
     PollValue                     : 0x53
     Memory Detail                 :

                              Block Poll               Page                       Polled
       Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
       ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
       eeprom        65    12     4    0 no        512    4      0  4000  4500 0xff 0xff
       flash         65     6    32    0 yes      8192   64    128 30000 30000 0xff 0xff
       signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
       lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

     Programmer Type : STK500
     Description     : Atmel STK500 Version 1.x firmware
     Hardware Version: 2
     Firmware Version: 1.18
     Topcard         : Unknown
     Vtarget         : 0.0 V
     Varef           : 0.0 V
     Oscillator      : Off
     SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e930b (probably t85)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "C:\Users\saluk\AppData\Local\Temp\arduino_build_327833/ColorSenseServo-at85.ino.hex"
avrdude: writing flash (1430 bytes):

Writing | ################################################## | 100% 2.08s

avrdude: 1430 bytes of flash written
avrdude: verifying flash memory against C:\Users\saluk\AppData\Local\Temp\arduino_build_327833/ColorSenseServo-at85.ino.hex:
avrdude: load data flash data from input file C:\Users\saluk\AppData\Local\Temp\arduino_build_327833/ColorSenseServo-at85.ino.hex:
avrdude: input file C:\Users\saluk\AppData\Local\Temp\arduino_build_327833/ColorSenseServo-at85.ino.hex contains 1430 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.00s

avrdude: verifying ...
avrdude: 1430 bytes of flash verified

avrdude done. Thank you.

Upload the blink sketch to check that you can upload something different .

Not sure how you use an LED as a sensor , maybe you are uploading ok , but it just doesn’t work as expected/hoped .

Wiring diagram would be useful .

"Revert" is not possible if the program loaded successfully, because the memory is completely erased before program load.

Most likely, your program is misbehaving in a way that looks like the sweep example is loaded.

To debug, remove the servo code and print out the variable "blue" to see how it is changing.

Also, servo problems are most often caused by inadequate servo power supplies. Use a separate power supply, capable of at least 1 Ampere per servo (3 Amperes for large servos). Don't forget to connect the grounds.

hammy: I should have mentioned that yes, I tried the blink sketch and it works. So that makes it even weirder (to me, anyway). Also, yes, I've gone my whole life till now not realizing that an LED can be used in reverse as a light sensor and can generate a detectable voltage. Pretty neat trick if I do say so myself. Anyway- I tried to make up a wiring diagram, I hope this is clear enough.

jremington: That's what I thought too. I guess for whatever reason it's trying to upload, then failing and then telling me errroneously that it loaded. When you say "print" I presume you mean to serial, which I'd like to do but I don't think is possible when using Attiny85, right?

1 Like

You can use some versions of software serial on the ATtiny85. Look for tutorials.

it's trying to upload, then failing and then telling my errroneously that it loaded.

I have never seen or heard of that happening. Ever.

Ok, glad to corroborate the weirdness. I guess Occam's razor says that you're right about the program acting just like sweep that was previously loaded on it by coincidence. What makes it weirder is that the code was running properly on a Xiao board right before trying to transition to Attiny, and none of that behavior was ever evident.

The Arduino IDE is pretty thoroughly debugged, so if something isn't working, suspect a mistake on your part before you blame Arduino.

And, it is much, much easier to find and fix your own mistakes than to get someone to do the same for Arduino.

Not sure how much you're reading into this- not "blaming" the arduino. Just thought it was a remarkable coincidence that the fail of the new, unrelated code exactly mimics the correct function of the previous code. If I was good at coding I wouldn't be here. Geez.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.