Proximity sensor with ATTiny 45

Hi All,

I hoped I posted this on the correct forum. I am not sure whether I should post it in this forum or other microcontroller forum.

I use Sharp IR Sensor Long Range (Sharp GP2Y0A02YK0F) on ATTiny 45.

I just want to use the sensor to detect if there is any object in front of it.

My code works on Arduino, but it did not work on ATTiny45. Can anyone please advise how should I do the coding for analog input from proximity sensor on ATTiny45?

const int CONTROL1 = 0;
const int CONTROL2 = 1;
const int proxSensor = A2; // input from Infrared proximity sensor

void setup() {
// put your setup code here, to run once:
pinMode(CONTROL1, OUTPUT);
pinMode(CONTROL2, OUTPUT);
pinMode(proxSensor, INPUT);
}

void loop() {
int proxSensorVal = analogRead (proxSensor);

if (proxSensorVal >= 200){
digitalWrite (CONTROL1, HIGH);
digitalWrite (CONTROL2, LOW);
}

else if (proxSensorVal<200){
digitalWrite (CONTROL1, LOW);
digitalWrite (CONTROL2, HIGH);
}

Thanks

Delete this:

pinMode(proxSensor, INPUT);

And try again.

Hi Outsider,

Thank you for the reply. I tried, but it did not work.

I replaced the proximity sensor with a potentiometer. It works. I am confused now.

This is the code with potentiometer and it works

#define CONTROL1 0
#define proxSensor A1 // input from potentiometer

void setup() {
// put your setup code here, to run once:/
pinMode(CONTROL1, OUTPUT);
pinMode(proxSensor, INPUT);
}

void loop() {
int proxSensorVal = analogRead (proxSensor)/4;
analogWrite (CONTROL1, proxSensorVal);
}