Hi,
When using this code in my Arduino ATmega it works fine and the moment of engagement can be trimmed with a potmeter.
/*
code to use 2 leds as brake lights on RC car
If the esc/throttle servo has to be trimmed
the pot meter can be used to set the moment
of engagement to the desired value
by Leo Groeneveld, dec 2011
*/
const int ledPin = 0;
int ledState = LOW;
int x; // variable to store trigger value
int y; // variable to store pulse width
void setup()
{
pinMode(1, INPUT); // Pin 1 (3) set as input to read the PPM signal from RC receiver for the UNO use the number between parentheses
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState); // turn/keep leds off at start
}
void loop()
{
int sensorValue = analogRead(2); // Read value from resistor
x = map (sensorValue, 0, 1023, 1000, 1500);
y = pulseIn(1, HIGH);
if(y < x && ledState == LOW){
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
else
if(y > x && ledState == HIGH){
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
When using the same code in an ATtiny45 the potmeter does nothing.
The ATtiny should be able to do an analogRead on pin 2 but it doesn't.
Should the code be changed for an ATtiny?
EDIT: solved by using pin 3, but pin 2 should be able to work also according to this image:
