ATTiny85 sending garbage to serial

The fuses were set to HfuseDF and Lfuse62 on the ATTiny with arduino as ISP. But when I press "S" to print the Serial Number it send garbage data.

#include <SoftSerial.h>
#include <EEPROM.h>
 
//---------------------------------------------------
#define SW  1
long value;
char buf[3];

SoftSerial mySerial(4,3);

void setup()
{
  mySerial.begin(9600);
  delay(10);
  pinMode(SW, INPUT_PULLUP); 
  value = EEPROMReadlong();
  //value = 374;AD]
  mySerial.print(value, DEC);
}


//===================================================
void loop()
{   
  delay(10);
  if(!digitalRead(SW)){
    mySerial.print("f;");
    while(!digitalRead(SW));
  }
  while(mySerial.available()){
  
    char t = mySerial.read();
    if(t == 'C'){
      mySerial.readBytes(buf, 3);
      value = (buf[0] - '0') * 100;
      value += (buf[1] - '0') * 10;
      value += (buf[2] - '0');
      //mySerial.print(value, DEC);
      EEPROMWritelong(value);
      delay(10);
      value = EEPROMReadlong();
      mySerial.print(value, DEC);
    }
    if(t == 'S'){
      delay(10);
      mySerial.print("10100012024S;");
      mySerial.print(value);
      mySerial.print(";");
    }
    delay(10);
  }
}

long EEPROMReadlong() {
  byte four = EEPROM.read(0);
  byte three = EEPROM.read(1);
 
  
  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF);
}

void EEPROMWritelong(long value) {
  byte four = (value & 0xFF);
  byte three = ((value >> 8) & 0xFF);
  
  EEPROM.write(0, four);
  EEPROM.write(1, three);
}

Unique or repeating?

repeating

Make sure the serial port viewer is at the same baud rate as the program sets it to.

Having said that, I can see no way you are actually sending anything when you press the 'S' key.

How is your serial port monitor set up with regards to line endings?

Please clarify, you say "garbage", do you mean

  • you see this "mySerial.print("10100012024S;");, but the value is incorrect, or
  • you see #$%^&()(&*^%$)%_+ every time?

It really does help if you include clear and thorough information, not vague descriptions like "garbage".

It may not matter, but you pass a long int to EEPROM write, but only write two bytes of it; you return a long(4 bytes) from the EEPROM read, but only ever fetch two bytes from the EEPROM. Have to ask, why?

Not sure what value the other two bytes of the temporary long created within EEPROM read will have, hence can't tell you what return value you'll get back from that function call, but I'd have to call it sloppy coding regardless.

I just stumbled upon this fuse calculator... (not a solution, just a parallel)

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