When using this code in my Arduino ATmega it works fine and the moment of engagement can be trimmed with a potmeter.
Code:
/* 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);
The code works fine with my ATmega1280 as can be seen here:
When I upload the code in an ATtiny45 it doesn't work. When the transmitter is off the led slowly blinks and when I turn the transmitter on the led just fades and does nothing. When the transmitter is turned on again the led starts blinking slowly again.
Here's the code:
Code:
const int ledPin = 0; // number of the LED pin
int ledState = LOW; int t; int x; int y; int z;
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 100; // interval at which to blink (milliseconds)
void setup() { pinMode(ledPin, OUTPUT); // set the digital pin as output: t = 0; z = 1400; }
void loop() { y = pulseIn(1, HIGH); // read PPM signal on ch.2 from receiver if(y > z){ x = 1; } else if(y <= z){ x = 0; }
unsigned long currentMillis = millis();
if(x == 0 && t == 0){ previousMillis = currentMillis; ledState = HIGH; digitalWrite(ledPin, ledState); t = 1; }
I've made this configuration with a couple of resistors to get exactly 120 ohms.
Now I get this error message after the Rx led on the Uno flashes three times:
This is the orientation of the ATTiny in the bread board wrt it's connections.
(I previously bent the legs of the cap for a better fit in the bread board but no results there)
(I'm going to clear everything today to put an extra lamp on the ceiling tomorrow)
EDIT:
It's working now!!!
I made the rookie mistake of not connecting the ground of the chip after I used the ground lead for the resistors and I left the cap and resistors out and now it works. So the auto reset is not disabled. I also connected a led to pin 9 to see the heartbeat.
Thanks guys for the support.
Here a short vid of the result. Brake lights for my Team Associated SC10 short course race truck. (you cannot see it clear on the vid but the leds burn when the brake is engaged, with the potmeter I can tune the moment of engagement to follow trim of the ESC if necessary)
Instead of that I get this: After Burning Bootloader to ATtiny45 This should be ok. And then it happened: After trying to upload the sketch to the ATtiny
Yikes! Sounds like the ISP gods are very unhappy with you
I'm afraid that I cannot offer any advice. I can only say again that my Uno board no longer requires that autoreset be disabled when programming with ArduinoISP. Before I flashed the chip with the new version of optiboot, my Uno board would not work without physically disabling autoreset. I have only used ArduinoISP to program ATmega328P chips, though.
Jim
Hi Jim,
Where can we find the latest version of optiboot and how to flash it to the Uno board?
(I'm going to try with my board as is because I have just bought it so it might be new but just in case it's an old one)
3 weeks ago I bought an Arduino UNO to use as an ISP board.
Now I read this on the Arduino site.
"NOTE: Currently, you cannot use an Arduino Uno as an ISP programmer because the optiboot bootloader does not support this sketch. A revision for this is in progress."
Because no target is mentioned I wonder when the new revision will be ready, I'm almost ready to start programming some Attiny chips, just waiting for some capacitors to arrive.
Thanks for the micros function, but what if my second PWM changes while the interrupt on the other pins is executed? (is it possible to monitor 2 PWM signals simultaneously with one attiny)
@AWOL,
I will look into it but I think PPM is the signal type used between the transmitter and receiver.
The receiver makes a PWM signal for each channel (receiver output to esc, servo's and my attiny) from the PPM signal (receiver input). I'm 99% sure about this.
I'm afraid I cannot use PulseIn as it waits for the end of the pulse and I want to run 2 processes at the time (read out 2 channels from the receiver).
There is a function called millis which would be perfect if it would be in microseconds. Is there a way to measure time in the background in microseconds?
I would like to use PWM signals from my RC car receiver to steer some leds.
I would like to activate brake lights when the car is braking and flash a set of head lights for x times when I push the button on the 3rd channel. (like used at le mans when a prototype is getting close to a slower vehicle) Also the voltage from the LiPo should be monitored and when it droppes below 6,6V a bright red led should be turned on in the cockpit. There are IC's readily available which switch at a certain voltage, I just have to look for them to make an input signal for the chip.
I intend to use a tiny45 chip and wonder how the chip can read the PWM signals from the receiver.
Can the chip "read" the PWM signal and know the pulse width or should I make an RC network to make an analog input and map it?
void doEncoderA(){ if (digitalRead(encoder0PinA) == HIGH) { // look for a low-to-high on channel A if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way encoder is turning encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } else // must be a high-to-low edge on channel A { if (digitalRead(encoder0PinB) == HIGH) { // check channel B to see which way encoder is turning encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out }
void doEncoderB(){ if (digitalRead(encoder0PinB) == HIGH) { // look for a low-to-high on channel B if (digitalRead(encoder0PinA) == HIGH) { // check channel A to see which way encoder is turning encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } else { // Look for a high-to-low on channel B if (digitalRead(encoder0PinA) == LOW) { // check channel B to see which way encoder is turning encoder0Pos = encoder0Pos + 1; // CW } else { encoder0Pos = encoder0Pos - 1; // CCW } } Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out }
Should I use resistors between the encoder and the pins? Are the pins analog or digital pins?
There are no images showing how to connect stuff in the playground.
as I rotate the encoder and the serial monitor is started nothing happens.
Did I break my Arduino not using resistors and shorting the pins to 5V? (I have tested all used pins as inputs and they still seem to work fine)