OK prosze, chwilę bardzo krotko działa a potem koniec tz. po wgraniu do procesora i uruchomieniu monitora chwile widze jakis wyniki moze sekunde a potem koniec
Kod:
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
// piny stanu PB5>13 PB4>12 PWM PB1>9 PB2>10
int pwM1a;
int pwM1b;
int in1a;
int in1b;
volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriod = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
// we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
// is clearer for this first example
void setup()
{
// tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
// catching these changes will allow us to calculate how long the input pulse is
attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);
// piny stanu PB5>13 PB4>12 PWM PB1>9 PB2>10
//#define pwM1a 15 //Atmega PB1-15
//#define pwM1b 16 //Atmega PB2-16
//#define in1a 9 //At PB5-13 mostek H
//#define in1b 10 //At PB5 mostek H
pinMode(pwM1a, OUTPUT);
pinMode(pwM1b, OUTPUT);
pinMode(in1a, OUTPUT);
pinMode(in1b, OUTPUT);
Serial.begin(115200);
}
void loop()
{
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
if(bNewThrottleSignal)
{
//Serial.println("szer imp: ");
Serial.println(nThrottleIn);
Serial.println(" wart PWN1");
Serial.println( pwM1a);
// set this back to false when we have finished
// with nThrottleIn, while true, calcInput will not update
// nThrottleIn
bNewThrottleSignal = false;
}
// other processing ...
}
void calcInput()
{
// if the pin is high, its the start of an interrupt
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
{
// get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
// easy to understand and works very well
ulStartPeriod = micros();
}
else
{
// if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
// start time ulStartPeriod from the current time returned by micros()
if(ulStartPeriod && (bNewThrottleSignal == false))
{
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
// piny stanu PB5>13 PB4>12 PWM PB1>9 PB2>10
//poczatek do produ
//podział sygnału +/- 50
if(nThrottleIn>1550){
if (pwM1a>=255) pwM1a=255;
pwM1a = map(nThrottleIn ,1550, 1995, 0,255);
digitalWrite(in1a , HIGH);
digitalWrite(in1b , LOW);
digitalWrite(pwM1b,LOW);
analogWrite(pwM1a,9);
}
//do tyłu
else if (nThrottleIn<1450){
if (pwM1b>=255) pwM1b=255;
pwM1b = map(nThrottleIn ,1005, 1450, 255, 0);
digitalWrite(in1a , LOW);
digitalWrite(in1b , HIGH);
digitalWrite(pwM1a,LOW);
analogWrite(pwM1b,10);
}
else if (nThrottleIn <1550, nThrottleIn> 1450 ) {
digitalWrite(in1a , LOW);
digitalWrite(in1b , LOW);
digitalWrite(pwM1a,HIGH);
analogWrite(pwM1b,HIGH);
}
}
}
//delay(1000);
}