Photo Resistor Detection ATTINY13

I am trying to program an ATTINY13a to analogue read a photo resistor and write to flash a red LED in low light conditions and a white LED in high light conditions. I have the system working on an Arduino, with pins 13, 12, and A0 in place for 5, 6, and 2 on the Attiny13a.

Here is the code:

/* 
Tower blink
This program sets up a blinking pattern to simulate that
of radio towers.  By day, it will blink sunny white. By
Night, it will blink red.
*/

int wled = 5;
int rled = 6;
int sensor = 2;

int sensorLow = 40;

unsigned long previousMillis;
unsigned long previousMillis2;

long OnTimeWhite = 100;
long InterBlinkWhite = 80;
long OffTimeWhite = 1000;
long OnTimeRed = 1000;
long OffTimeRed = 1500;

void setup() {
  pinMode (wled, OUTPUT);
  pinMode (rled, OUTPUT);
  pinMode (sensor, INPUT);
}

void loop() 
{
  unsigned long currentMillis = millis();
  unsigned long timeDifference = (currentMillis - previousMillis);
  {
   if (analogRead (sensor) < sensorLow - 5) {
      digitalWrite (wled, LOW);
      if (timeDifference <= OnTimeRed) {
        digitalWrite (rled, HIGH);
      }
      else if (timeDifference <= OnTimeRed + OffTimeRed) {
        digitalWrite (rled, LOW); 
      }
      else {
        previousMillis = currentMillis;
      }
    }
    else if (analogRead (sensor) > sensorLow + 5) {
      digitalWrite (rled, LOW);
      if (timeDifference <= OnTimeWhite) {
        digitalWrite (wled, HIGH);
      }
      else if (timeDifference <= OnTimeWhite + InterBlinkWhite){
        digitalWrite (wled, LOW);   
      }
      else if (timeDifference <= 2*OnTimeWhite + InterBlinkWhite){
        digitalWrite (wled, HIGH);
      }
      else if (timeDifference <= 2*OnTimeWhite + InterBlinkWhite +OffTimeWhite) {
        digitalWrite (wled, LOW);
      }
      else {
        previousMillis = currentMillis;
      }
    }
    else {
      digitalWrite (rled, LOW);
      digitalWrite (wled, LOW);
    }
  }
}
  
  

Here is a screenshot of the circuit I built in TinkerCad:
Screen Shot 2022-09-18 at 9.25.35 PM

Nothing is working, so I am wondering where to start fixing it.

Thanks!

Hi, Welcome to Engineer’s Asylum.

It seems to me that you have configured the pins wrong. Can you change the pins number as given below and see if it’s working??

int wled = 0;
int rled = 1;
int sensor = 3;

2 Likes

Thanks! That did the trick!

2 Likes