Interrupts

I have a UNO generating 5 pulses repeated each second. The sequence in uSec is
3000 HIGH, 1000 LOW, 900 H 900 L, 300 H, 900 L, 300 H 900 L, 300 H 900 L
The attched sketc1 displays in SM all the HIGHs in sequence. Sketch2 chnages the order from X,Y to Y,X and when run displays all the LOWs sequentially. My many attempts which have been extensive, to combine these two sketches into one have failed. I want one sketch to display in SM the HIGHS and the LOWS in order. Extensive internet searches reveal no solution

IntChange-09.ino (605 Bytes)

Please show your sketch in the post between code tags. The </> button is for code tags.
It this your very first program ever ?

You could make it more readable. This is exactly the same as what you have:

// Example Code IntChange-09 Idee nouvelle
volatile int pwm_value = 0;
volatile int prev_time = 0;
int x = 0;  char C = CHANGE;
char X = RISING;  char Y = FALLING;


void setup()
{
  Serial.begin(115200);
  
  // when pin D2 goes high, call the GUP function
  attachInterrupt(0, GUP, C);
}


void loop()
{
}


void GUP()
{
  attachInterrupt(0, GND, Y);
  prev_time = micros();
}


void GND()
{
  attachInterrupt(0, GUP, X);
  pwm_value = micros() - prev_time;
  if (pwm_value > 200)
  {
    Serial.println(pwm_value);
  }

  x++;
  if (x > 24)
  {
    while (1);
  }
}

Can you explain what your project is. What is a SM ? Are there two sketches ?
It seems as if you want to invert a signal. You can do that with a logic gate or a transistor.

About your sketch: Use one interrupt and keep that active. Keep the interrupt routine as short and as fast as possible. Don't use a Serial function in the interrupt routine, and no while-loop. Read the level with digitalRead() in the interrupt function. Then gather two times with micros, but perhaps just one will work or perhaps more than two. If you really want to recognize the pulses at high speed, then I would add a ringbuffer.

Hi 17,

you want to do - ...... ---- ....... ------- ....... ------ what?

you attached some code that really makes no sense in relation to what you have described.
I'm asking myself is this a school-assignment?

draw a timing diagram of how the switching between LOW and HIGH should look like.

and take 20 sentences space to describe what you really wanto to do.

best regards Stefan

Just relax, have a drink or two and forget about it? (:O)

17391939:
Just relax, have a drink or two and forget about it? (:O)

To whom is that addressed?

And does it mean that you really have no interest in figuring out a solution to your problem?

...R

I have a UNO(1) generating 5 pulses repeated each second. The sequence in uSec is
3000 HIGH, 1000 LOW, 900 H 900 L, 300 H, 900 L, 300 H 900 L, 300 H 900 L
A separate UNO(2) recives the pulse train on D2 Int0, its interrupt.
The attached sketch1 displays in Serial Monitor all the HIGHs in sequence. Sketch2 loaded on UNO(2) changes the order from X,Y to Y,X and when run displays all the LOWs sequentially. My many attempts which have been extensive, to combine these two sketches into one have failed. I really want one combined sketch to display in Serial Monitor the HIGHS and the LOWS in time order. Sketch2 is identical to Sketch1 except that X and Y are reversed.
I'm sorry you cannot understand my English too well.

Robin2:
To whom is that addressed?

And does it mean that you really have no interest in figuring out a solution to your problem?

...R ... forgive me ... I really do want and answer.

StefanL38:
Hi 17,

you want to do - ...... ---- ....... ------- ....... ------ what?

you attached some code that really makes no sense in relation to what you have described.
I'm asking myself is this a school-assignment?

draw a timing diagram of how the switching between LOW and HIGH should look like.

and take 20 sentences space to describe what you really want to do.

best regards Stefan

My first language is english, I describe things as I see them, sorry. How do I draw a timing diagram in this post?

Timestamp the edge times with micros?

I don't really understand your problem.

Please post the program that is transmitting the pulses and the two different programs that are receiving the pulses.

And also post the program that represents your best attempt at merging the two receive programs together with a detailed description of what happens when you run the merged program.

...R

Thanks I will post the generator sketch tmro. It's 23.15hrs here time for sleep.

17391939:
Thanks I will post the generator sketch tmro. It's 23.15hrs here time for sleep.

Interrupt generator Sketch 433TestGen-01.ino and combined Interrupt Sketch, which is rather messed up at the moment, is Sketch IntCombo-02.ino. Both are attached

433TestGen-01.ino (789 Bytes)

IntCombo-02.ino (982 Bytes)

Those codes are small enough that you can post them in-line. Please do so using code tags so they are nicely formatted as the code in Reply #1 is. That will make it easier on the folks you are asking for free help from. Why make them download your code?

As suggested in the first sentence of reply #1.

OP's code:

// 433TestGen-01.ino

 int Tx = 2; int T = 240; int F = 960;
 
void setup() {
 Serial.begin(9600);  
 pinMode(Tx, OUTPUT);
}

void loop() {
  digitalWrite(Tx, HIGH); delayMicroseconds(3200);
  digitalWrite(Tx, LOW); delayMicroseconds(F);  

  digitalWrite(Tx, HIGH); delayMicroseconds(F);
  digitalWrite(Tx, LOW); delayMicroseconds(F); 
  //delay(10);
  digitalWrite(Tx, HIGH); delayMicroseconds(T);
  digitalWrite(Tx, LOW); delayMicroseconds(F);

  
  digitalWrite(Tx, HIGH); delayMicroseconds(T);
  digitalWrite(Tx, LOW); delayMicroseconds(F); 
  //delay(10);

  digitalWrite(Tx, HIGH); delayMicroseconds(T);
  digitalWrite(Tx, LOW); delayMicroseconds(F);

  //digitalWrite(Tx, HIGH); delayMicroseconds(F);
  digitalWrite(Tx, LOW); 
    delay(1000);  

}
// IntCombine-02 20 Jan 2021
volatile int pwm_value = 0;
volatile int prev_time = 0;
volatile int PWM_value = 0;
volatile int last_time = 0;
int x = 0;  char R = RISING;  char F = FALLING;
         //   char f = FALLING;  char r = RISING;
 
void setup() {
  Serial.begin(115200);
  // when pin D2 goes high, call the GUP function
  attachInterrupt(0, GUP, R);  // was RISING
} 
void loop() { }
//************** 1st PART *******************              
void GUP() {
  attachInterrupt(0, GND, R); 
  prev_time = micros();
} 
void GND() {
  attachInterrupt(0, gnd, F);  
  pwm_value = micros()- prev_time;
  Serial.println(pwm_value); 
 // x++; if(x>18){while(1);}
}
// ---------- 2nd PART ------------------
void gup() {
  attachInterrupt(0, gnd, F);
  last_time = micros();
} 
void gnd() {
  attachInterrupt(0, gnd, R);  
  PWM_value = micros() - last_time;
  if(PWM_value > 300 ) {
  Serial.println(PWM_value);  }
  x++; if(x>24){while(1);}
}

An attachInterrupt() call inside an ISR. You've got a lot of explaining to do...

If you want an interrupt to run different code on different occasions just put all the code in the ISR and select which piece is appropriate depending on the value of a variable. Something like

void myISR() {
  if (mode == 1) {
    // code for mode 1
    mode = 2
  }
  else (if mode == 2) {
     // code for mode 2
     mode = 3
   }
   // etc
}

...R

Attached slightly better version of the combined Sketches. This further attempt does show on Serial Monitor either HIGHs or LOWs when order of interrupts is change but not both during same run-time.
Rgds Michael

IntCombine-03.ino (1.06 KB)

Hello Robin2 I will give that code idea a whirl. BTW I have read all the documentation on interrupts widely. My next challenge will be to user timers directly for more accuracy of needed, but I'm a long way off doing that yet.
Rgds
Michael

17391939:
Attached slightly better version of the combined Sketches.

Seriously??? Is there something about the previous requests that you didn't understand?

Koepel:
Please show your sketch in the post between code tags. The </> button is for code tags.

gfvalvo:
Those codes are small enough that you can post them in-line. Please do so using code tags so they are nicely formatted as the code in Reply #1 is. That will make it easier on the folks you are asking for free help from. Why make them download your code?

They were quite clear and reasonable.