VAŽNO! Ovdje pročitajte kako se registrirati na forum. Bez registracije, nećete biti u mogućnosti pisati na forumu.

digitalWrite() i delay u voidu od interrupta ne radi

Zapeli ste s projektom? Ovdje je pravo mjesto za postaviti pitanje. Imajte na umu da ekipa e-radionice neće uvijek i/ili redovno odgovarati na pitanja postavljena u ovom podforumu.
Odgovorite
karlo.schweiger#3350
Novak
Novak
Postovi: 2
Pridružen/a: sri tra 07, 2021 11:31 am

digitalWrite() i delay u voidu od interrupta ne radi

Post Postao/la karlo.schweiger#3350 » sri tra 07, 2021 11:39 am

Pozdrav, problem je taj da kada se vrti void od interrupta digitalWrite ne radi i delay kao da nema svrhu

volatile int pwm_value = 0;
volatile int prev_time = 0;

volatile int pwm_value2 = 0;
volatile int prev_time2 = 0;


const byte ledPin = 13;
const byte interruptPin = 3;
const byte interruptPin2 = 2;

int sendPin = 12; // pin koji šalje pulseve dalje u aparat
int FiskalPulseSendCoin = 5; //Pin koji salje pulseve zetonjere na fiskalizaciju - 1kn = 1puls,20ms...
int FiskalPulseSendNote = 7; //Pin koji salje pulseve akceptora na fiskalizaciju - 10kn = 1puls,50ms...

void setup() {
Serial.begin(115200);

pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), FiskNov, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), FiskCoin, FALLING);
digitalWrite(interruptPin2, HIGH);

pinMode(ledPin, OUTPUT);
pinMode(sendPin, OUTPUT);
pinMode(FiskalPulseSendCoin, OUTPUT);
pinMode(FiskalPulseSendNote, OUTPUT);
digitalWrite(FiskalPulseSendCoin, HIGH);
digitalWrite(FiskalPulseSendNote, HIGH);
digitalWrite(sendPin, HIGH);
}

void loop() {

}

void FiskCoin()
{
prev_time = millis();
attachInterrupt(digitalPinToInterrupt(interruptPin2), PulsTimeCoin, HIGH);



}


void PulsTimeCoin(){
attachInterrupt(digitalPinToInterrupt(interruptPin2), FiskCoin, LOW);

digitalWrite(interruptPin2, HIGH);
pwm_value = millis()-prev_time;
pwm_value = pwm_value ;
Serial.println (pwm_value);

if((pwm_value > 6) && (pwm_value < 13 ))
{
digitalWrite(FiskalPulseSendCoin,LOW );
delay(20);
digitalWrite(FiskalPulseSendCoin, HIGH );

Serial.print("Fiskalizirao kovanicu");
Serial.print('\n');
pwm_value = 0;

}
else
{
pwm_value = 0;

Serial.print("PREKRATAK ILI PREDUGACAK PULS");
Serial.print('\n');
}


}

void FiskNov() {
prev_time2 = millis();

attachInterrupt(digitalPinToInterrupt(interruptPin), PulsTimeNote, HIGH);

}
void PulsTimeNote() {
attachInterrupt(digitalPinToInterrupt(interruptPin), FiskNov, LOW);


digitalWrite(interruptPin, HIGH);
pwm_value2 = millis()-prev_time2;
pwm_value2 = pwm_value2 ;
Serial.println (abs(pwm_value2));



if((pwm_value2 > 45) && (pwm_value2 <65 ))

{

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);

digitalWrite(sendPin, LOW);
delay(10);
digitalWrite(sendPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);


Serial.print("proslijedio puls novčanice u aparat");
Serial.print('\n');

//-----------------fiskal novcanice

digitalWrite(FiskalPulseSendNote,LOW );
delay(2000);
digitalWrite(FiskalPulseSendNote,HIGH );

Serial.print("Fiskalizirao novčanicu");
Serial.print('\n');
pwm_value2 = 0;


}
else
{
Serial.print("PREDUG ILI PREKRATAK PULS ACCEPTOR");
Serial.print('\n');
}


}
Avatar
haluzanpavao#5085
Ozbiljni hardveraš
Ozbiljni hardveraš
Postovi: 80
Pridružen/a: sri lip 13, 2018 12:19 pm
Lokacija: Zagreb

Re: digitalWrite() i delay u voidu od interrupta ne radi

Post Postao/la haluzanpavao#5085 » sri tra 07, 2021 1:06 pm

kAO PRVO NAREDBA ZA PVM = analogwrite(broj_pina.vrijednost_PWM);
A za vrijeme zabranjenog interupta ne radi niti delay ni pwm, jer su vezani na interupte koji su zabranjeni!!
Baci pogled na moje interupt rutine ovdije https://forum.e-radionica.com/hr/viewto ... f=15&t=326 redovi od 258-274
pozdrav
Pavel
karlo.schweiger#3350
Novak
Novak
Postovi: 2
Pridružen/a: sri tra 07, 2021 11:31 am

Re: digitalWrite() i delay u voidu od interrupta ne radi

Post Postao/la karlo.schweiger#3350 » sri tra 07, 2021 5:35 pm

ja sam teži početnik pa me zanima dali se može negdje uplatiti da mi to napišete ?
Sve radi osim slanja impulsa na pin 5 i 7
Hvala
Avatar
haluzanpavao#5085
Ozbiljni hardveraš
Ozbiljni hardveraš
Postovi: 80
Pridružen/a: sri lip 13, 2018 12:19 pm
Lokacija: Zagreb

Re: digitalWrite() i delay u voidu od interrupta ne radi

Post Postao/la haluzanpavao#5085 » sri tra 07, 2021 9:28 pm

pokušaj ovako oblikovan kod
on radi, ali nemam hardvare da ga komplet isprobam
pažljivo si pročitaj ovaj link. https://www.arduino.cc/reference/en/lan ... interrupt/
I napomena "ovdije se niš ne plača a pogotovo ne meni."
Ako sam pomogao, biče mi drago :)

Kod: Označite sve


volatile int pwm_value = 0;
volatile int prev_time = 0;

volatile int pwm_value2 = 0; //
volatile int prev_time2 = 0; //


const byte ledPin = 13;
const byte interruptPin = 3;
const byte interruptPin2 = 2;

int sendPin = 12; // pin koji šalje pulseve dalje u aparat
int FiskalPulseSendCoin = 5; //Pin koji salje pulseve zetonjere na fiskalizaciju - 1kn = 1puls,20ms...
int FiskalPulseSendNote = 7; //Pin koji salje pulseve akceptora na fiskalizaciju - 10kn = 1puls,50ms...

void setup() {
Serial.begin(115200);

pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
//digitalWrite(interruptPin2, HIGH);
attachInterrupt(digitalPinToInterrupt(interruptPin), FiskNov, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), FiskCoin, FALLING);


pinMode(ledPin, OUTPUT);
pinMode(sendPin, OUTPUT);
pinMode(FiskalPulseSendCoin, OUTPUT);
pinMode(FiskalPulseSendNote, OUTPUT);
digitalWrite(FiskalPulseSendCoin, 1);
digitalWrite(FiskalPulseSendNote, 1);
digitalWrite(sendPin, 1);
}

void loop() {

}

void FiskCoin()
{
prev_time = micros();
//attachInterrupt(digitalPinToInterrupt(interruptPin2), PulsTimeCoin, HIGH);



//}


//void PulsTimeCoin(){
//attachInterrupt(digitalPinToInterrupt(interruptPin2), FiskCoin, LOW);

//digitalWrite(interruptPin2, HIGH);
pwm_value = micros()-prev_time;
pwm_value = pwm_value ;
Serial.println (pwm_value);

if((pwm_value > 6) && (pwm_value < 13 ))
{
digitalWrite(FiskalPulseSendCoin,0 );
delayMicroseconds(20000);
digitalWrite(FiskalPulseSendCoin, 1 );

Serial.print("Fiskalizirao kovanicu");
Serial.println('\n');
pwm_value = 0;

}
else
{
pwm_value = 0;

Serial.print("PREKRATAK ILI PREDUGACAK PULS");
//Serial.print('\n');
}


}

void FiskNov() {
prev_time2 = micros();
/* //proba vremena
long a=micros();
delayMicroseconds(2000000);
Serial.println(micros()-a);

*/
//ovi redovi koje sam stavio pod remark nesmiju postojati
//attachInterrupt(digitalPinToInterrupt(interruptPin), PulsTimeNote, HIGH);

//}
//void PulsTimeNote() {
//attachInterrupt(digitalPinToInterrupt(interruptPin), FiskNov, LOW);


//digitalWrite(interruptPin, HIGH); pin je pool up_an i netreba ga ručno dizati
pwm_value2 = micros()-prev_time2;
// pwm_value2 = pwm_value2 ;// nepotrebna naredba
Serial.println (abs(pwm_value2));



if(((pwm_value2/1000) > 45) && ((pwm_value2/1000) <65 ))//Variabla sadrži microsekunde pa trreba broj podijeliti sa 1000
{

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);

digitalWrite(sendPin, 0);
delayMicroseconds(10000);
digitalWrite(sendPin, 1);
delayMicroseconds(10000);
digitalWrite(ledPin, 0);


Serial.println("proslijedio puls novčanice u aparat");


//-----------------fiskal novcanice

digitalWrite(FiskalPulseSendNote,0 );
delayMicroseconds(2000000);
digitalWrite(FiskalPulseSendNote,1 );

Serial.println("Fiskalizirao novčanicu");
//Serial.print('\n');
pwm_value2 = 0;


}
else
{
Serial.println("PREDUG ILI PREKRATAK PULS ACCEPTOR");
//Serial.print('\n');
}
}
robertperic#7900
e-radionica.com team
e-radionica.com team
Postovi: 1
Pridružen/a: pon ožu 08, 2021 12:31 pm

Re: digitalWrite() i delay u voidu od interrupta ne radi

Post Postao/la robertperic#7900 » čet tra 08, 2021 9:10 am

Pozdrav, prvo digitalWrite neće raditi nakon što je pin postavljen kao INPUT_PULLUP.
zatim delayMicroseconds će raditi umjesto delay(), ali nije ga pametno koristiti unutar interrupta. Trebali bi razmisliti o implementaciji na drugi način ako će to biti veći projekt. Za manje stvari bi mogao raditi ok sa delayMicroseconds().
Ovaj post je jako dobro objasnio kako pravilno raditi sa intrerruptima:
https://roboticsbackend.com/arduino-interrupts/
Odgovorite