Hi All,
I'm actually working on a solution to forward servo signals but under special circumstances the forwarding must be stopped and manual generated servo commands must be send to the servo.
I'm using a ATTiny85 working on 8MHz connected via ArduinpISP using an Arduino UNO R3.
For testing I wrote the following code:
//#include "Servo8Bit.h"
//Servo8Bit Servo1;
int led = 3;
volatile int nDelay = 1000;
volatile uint8_t LastInterruptState = PINB; // ATTiny84 : PINA;
volatile uint32_t nLastSpeedPositionChange = micros();
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(1, OUTPUT);
digitalWrite( led, HIGH);
delay(2000);
digitalWrite(led, LOW);
/* ATTiny84
PCMSK0 |= ( 1<<PCINT4);
GIMSK |= (1 << PCIE0);
*/
// ATTiny85
PCMSK |= ( 1<<PCINT0);
GIMSK |= (1 << PCIE);
interrupts();
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(nDelay); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(nDelay); // wait for a second
}
void SpeedPositionInterrupt()
{
unsigned long nMicros = micros();
unsigned long nDifference = nMicros - nLastSpeedPositionChange;
if ( (nDifference > 2000 ) || ( nDifference < 900))
{
//Hier passiert nix, denn es handelt sich um den "Leerlauf (LOW)
}
else
{
if ( nDifference < 1300 )
nDelay = 500;
else
{
if ( nDifference < 1600 )
nDelay = 1000;
else
nDelay = 2000;
}
//Servo1.writeMicroseconds( nDifference );
}
nLastSpeedPositionChange = nMicros;
}
ISR( PCINT0_vect )
{
uint8_t PinState = PINB; // ATTiny84 PINA;
uint8_t PinChanges = PinState ^ LastInterruptState;
digitalWrite(1, digitalRead(0));
if (PinChanges & (1<<PCINT0))
SpeedPositionInterrupt();
LastInterruptState = PinState;
}
The servo signal coming from the RC sender is read by a pin change interrupt on pin 0 and on each signal change the corresponding signal on the outgoing pin 1 is switched. To get a control if it's working, a LED is attached to pin3 if the servo is in the middle, the LED blinks in a 1 second interval. If I set the servo to "up" on teh sender, the LED blinks in 2 second intervals, if I put it down, the LED blinks in 0.5 seconds interval.
Listening to the servo signal and forwarding it works perfect.
Now I want to activate the manual servo commands.... and the problems are coming up.
If I insert the
#include "Servo8Bit.h"
The uploaded code hangs immediatly. I added a LED ON/OFF in the setup() to see if the setup is started, but the LED stays dark. If I comment out the include statement, it's working again.
It's strange, because at that moment no Servo8Bit object is defined nor attached. Only the #include directive is set.
Than I suggested it might have to do with the timer handling, so I commented the complete timer handling code out in the libryry, but it's still hanging.
I already searched here in the forum and I tested with Timer0 and Timer1, I tested with the alternative delay functions, the "avr/power.h" include, etc. but it never worked.
The used servo has an own power supply with the ground connected to the chip circuit.
Do you have any hints what might be the reason for this ?
Many thanx in advance for your comments and thoughts.
Greetinx
gismow