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.