Code on ATTiny85 acting ... strange

I have had my code working well on a breadboard using the Arduino Uno, on a breadboard using the ATTiny85, as well as soldered to a protoboard. Now, after a recent code change, I'm experiencing strange issues and I'm not quite sure if it's hardware or software related. It seemed to work fine right after I burned the code to the chip, but now it's acting strange

Here's a quick rundown of what HW I have:
ATTiny85
TIP120 Transistor
1k ohm resistor
1N4001 Diode
5v voltage reg (radio shack, don't have P/N handy)
LED with appropriate resistor
Parallax PIR sensor (Rev. A)
Screw headers for +/- out (for the PIR sensor), and signal input from the PIR
12V power supply (also supplying the motor with power)

What it's supposed to do:
When powered on, wait n seconds for PIR to calibrate, blink LED during this time
After calibration sequence start loop
When PIR signal is high (motion detected) set a random power level for the motor and a random number of seconds to run the motor
After the motor sequence is done, wait n seconds before allowing the motor to start (blinking LED during this time)
When wait is over LED is on solid, and nothing happens until the PIR senses motion, starting the whole sequence again

Pretty straight forward, but what's happening now is that the motor sequence starts, the wait time happens, then the motor starts again, even if no motion detected. I even completely disconnected the PIR to take out the possibility that the sensor was sending a signal even when there was no motion, and it's still not working right.

Here's the code as of now, am I missing something obvious, or should I go down the route of checking the HW?

// Control of 12v motor based on motion detected by PIR sensor 
// LED turns on when motor is off, and off when motor is on, blinks during calibration and wait time (time between activations)


  int calibrationTime = 20;  // Time we wait for sensor to calibrate before starting loop (in seconds)
  int waitTime = 15;     // Time we wait between motor stopping, and next possible activation (in seconds)
  int pirPin = 1;  // pin for reading the PIR sensor 
  const byte ledPin = 3; // pin for the LED 
  int TIP120pin = 0; //Base pin on TIP120 Transistor
  int powerLvl ;   // variable for storing random power level
  int delayVal ;   // variable for storing random delay value

void setup() {
  pinMode(pirPin,INPUT);
  digitalWrite(pirPin, LOW);
  pinMode(ledPin, OUTPUT);  
  digitalWrite(ledPin,LOW);  
  pinMode(TIP120pin,OUTPUT);
  digitalWrite(TIP120pin, LOW);

  //give the sensor some time to calibrate
    for(int i = 0; i < calibrationTime; i++){
      delay(500);
      digitalWrite(ledPin,HIGH);
       delay(500);
      digitalWrite(ledPin,LOW);
     }
      digitalWrite(ledPin,HIGH); //indicates that sensor is active.
      delay(50);
     }

void loop(){
     if(digitalRead(pirPin) == HIGH){ // motion detected
    //enter loop code here
     

    powerLvl = random(128,255);   // Random power level to run the motor (speed) (1/2 to full speed)
    delayVal = random(10000,20000);  // Random length of time to run the motor (10-20 seconds)
     
    digitalWrite(ledPin,LOW); //LED off when motor running
    analogWrite(TIP120pin, powerLvl); // Run motor at random speed 
    delay(delayVal);  // stay on for random time 
    digitalWrite(ledPin,HIGH); //LED on while motor stopped
    analogWrite(TIP120pin, 0); // Stop motor

// for loop to wait n seconds before starting up again
    for(int i = 0; i < waitTime; i++){
      delay(500);
      digitalWrite(ledPin,HIGH); //blink LED while waiting for the sequence to start again
       delay(500);
      digitalWrite(ledPin,LOW);
     }
      digitalWrite(ledPin,HIGH); //indicates that sensor is active, waiting for PIR sensor to start sequence
      delay(50);
  }
}

The change was to the last part of the code where instead of just putting a delay, I'm using the for loop to blink the LED for n seconds while it's waiting. This is the same code I used above it to calibrate so I thought it would work fine.

You said the original code worked on an Uno and a breadboard ATtiny. Does the new version work on those or does it behave the same on all the MCUs?

Does it work if you comment out the blinking stuff and replace it with a delay()?

...R

My guess is AC induced "hum" or perhaps your power supply is not well filtered.

Be certain you have the right bypass capacitors and bulk capacitors on the stripboard; the bypass capacitors being as close to the uC as possible.

You may need to add bypass caps from ground to the PIR terminals (well, all but the Gnd, of course.)

Ray

Schematics or at least a detailed photo please....

Maybe it is autoresetting.
Put a 10 kohm resistor from reset to VCC

mrburnette:
My guess is AC induced "hum" or perhaps your power supply is not well filtered.

Be certain you have the right bypass capacitors and bulk capacitors on the stripboard; the bypass capacitors being as close to the uC as possible.

You may need to add bypass caps from ground to the PIR terminals (well, all but the Gnd, of course.)

Ray

I did some research on bypass capacitors (since I'm new to this I didn't have any clue).

This (attachment) is how it's set up now (excuse the spaghetti mess, it's my first protoboard, and I could have arranged it better)

If I understand correctly the second attachment is what I should do with caps, with the addition of caps on the PIR pins?

ATTiny85_MotorControl.jpg

ATTiny85_MotorControl_CAPS.jpg

Erni:
Maybe it is autoresetting.
Put a 10 kohm resistor from reset to VCC

I have noticed that it does reset once in a while, I'll try this as well

I just finished a project eerily similar to yours.
See schematics, board layout and photos in this thread:
http://forum.arduino.cc/index.php?topic=189671.0

The diode (that you already have) and a capacitor very close to the VCC pin (which you're missing) of the T85 are important. I'm not sure the external pull-up on the reset pin matters that much, but it sure doesn't hurt. My board worked fine without it, but I put it in for the final version.

If I understand correctly the second attachment is what I should do with caps, with the addition of caps on the PIR pins?

YES to first part.

Cap of 10uF (or larger) on the PIR pins V+ to Gnd. at the screw-terminals may be useful, may not. Some designs call for this.
http://www.electroschematics.com/wp-content/uploads/2010/09/pir-motion-sensor-switch.gif

Design thinking: all external wiring is an antenna. The longer the wires, the move voltage that is introduced as noise into your circuit; this includes RF noise, powerline noise, and powerswitching noise. Just a few fluorescent lights can play havoc with your uC projects if not properly shielded. Unless there is a very good reason not to do so, all lenghts of external wiring should be shielded and the shield grounded at the project leaving the shield open at the sensor. Internal wires should be twisted inside the shield. This is a general rule. Some PIR sensors for uC come with the bulk capacitor already on the board:
http://www.ladyada.net/wiki/lib/exe/fetch.php?hash=e5c06c&w=650&h=419&media=http%3A%2F%2Fwww.ladyada.net%2Fimages%2Fsensors%2Fpirtitx.gif

You may find this of interest:
http://www.freescale.com/files/microcontrollers/doc/app_note/AN2321.pdf

Ray

Way under capacitated. :slight_smile: I would have the 0.1uF close to the IC, 100uF at the power input, 10uF on the output side of the regulator, 10uF next to every power output (to sensors or whatever), and often use 0.1uF paralleled with the 10uF in all those places too.

Even with all that, sometimes you'll fight switching noise from high-current devices, or into sensitive devices (analog parts mostly).