-1

I was trying to use ultrasonic with digispark Attiny85 board and used separate pins for trigger and echo, and used MM5451 led controller as a display, I uploaded the code successfully the problem is if the echo pin is connected to it at startup board stops/freezes I don't know but code doesn't work, and if I connect the echo pin few seconds after the board starts everything works fine.

Below is the code I wrote

#define clock 0
#define data 1
#define trig 2
#define echo 3
byte c[5]={10,11,11,0,12};
long dist=0;
const uint8_t digits[15]={
    B1110111,
    B1000100,
    B1101011,
    B1101110,
    B1011100,
    B0111110,
    B0111111,
    B1100100,
    B1111111,
    B1111110,
    B0111011,
    B0010011,
    B1011101,
    B0111001,
    B0000000
};
void setup()
{
    pinMode(clock,OUTPUT);
    pinMode(data,OUTPUT);
    pinMode(trig,OUTPUT);
    pinMode(echo,INPUT);
    digitalWrite(clock,LOW);
    digitalWrite(data,LOW);
    digitalWrite(trig,LOW);
}
void pulse()
{
    delayMicroseconds(5);
    digitalWrite(clock,HIGH);
    delayMicroseconds(10);
    digitalWrite(clock,LOW);
    delayMicroseconds(5);
}
long ultrasonic(boolean ci)
{
    long distance;
    unsigned long pulsetime;
    digitalWrite(trig,LOW);
    delayMicroseconds(5);
    digitalWrite(trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(trig,LOW);
    pulsetime=pulseIn(echo,HIGH);
    if(ci)
    {
        distance=pulsetime/58;
    }
    else
    {
        distance=pulsetime/148;
    }
    delay(50);
    if((distance<450)&&ci)
    {
        return distance;
    }
    else if((distance<180)&&(!ci))
    {
        return distance;
    }
    else
    {
        return 0;
    } 
}
void show(byte a[5])
{
    delayMicroseconds(5);
    digitalWrite(data,HIGH);
    pulse();
    for(byte x=0;x<5;++x)
    {
        byte data1=digits[a[x]];
        for(byte y=0;y<7;++y)
        {
            if(data1&(1<<y))
            {
              digitalWrite(data,HIGH);
                pulse();
            }
            else
            {
                digitalWrite(data,LOW);
                pulse();
            }
        }
    }
    digitalWrite(data,LOW);
    pulse();
}
void loop()
{
    show(c);
    delay(2000);
    while(1)
    {
        dist=ultrasonic(true);
        if(dist>0)
        {
            byte i=3;
            c[4]=14;
            c[0]=14;
            c[1]=14;
            c[2]=14;
            c[3]=14;
            while(dist>0)
            {
                c[i]=(dist%10);
                if(i==0)
                {
                    i=4;
                }
                dist/=10;
                --i;  
            }
            show(c);
        }
        delay(200);
    }  
}
1

Pins 3 and 4 are used for USB (during programming). Try using pin 5 instead.

Or swap it with one of the clock, data, or trig outputs (i.e. 0,1,2)

  • Gerben thanks for your replay, one thing I want to mention is I used same code and powered digispark with my phone otg and it's working and it doesn't work when I connect it to phone charger's USB output, weird right. – Balkrushna Kadam Jan 4 at 3:25
  • That is weird indeed. Did my suggestion of swapping/changing pin(s) help? – Gerben Jan 4 at 16:13
  • 1
    That echo pin was sending pulse at startup without trigger. and it was messing with USB signals of bootloader at startup which making bootloader to stuck. I did what you said, swapped pin and it's working. But one problem is that USB singals on startup messing with my display by sending invalid data, I have to use pin 3,4 because 5th pin doesn't work it is set as reset pin my board is digispark clone – Balkrushna Kadam Jan 6 at 6:25
  • Try some other combinations. Like for example using pins 3 and 4 for trig and data. Having the bootloader pulsing those won't have many side effects, as far as I can tell. – Gerben Jan 6 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.