Go Down

Topic: waiting for two inputs (Read 235 times) previous topic - next topic

devasri

how can i say wait for both input values to change before I can have my other code compare them?

Delta_G

Use an empty while loop?  You haven't really given much to go on there.  Maybe it down and think for a minute.  You're asking a bunch of professionals for help with something.  You don't want to waste their time or they might not help.  What might they need to know about what you want in order to help you?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

devasri

I just want to be able to see if both inputs will change, because I am already continuously changing them but i want to compare only the values I haven't compared yet, for example i dont want to compare the old t1 with the new t2, and I want to know how to poll the inputs if maybe that would work?

Code: [Select]
void loop() {
 
  if (t2 == t1)
  {
    OCR2A = 0;
    OCR2B = 0;
  }
  //compare time that one signal goes high to time the other signal goes high
  else if(t2 > t1)
    {
    OCR2A = (t2-t1) ; //output PWM based on phase difference on pin 9
    OCR2B = 0;
    }
  else if(t2 < t1)
    {
    OCR2B = (t2-t1);    //output PWM on pin 10
    OCR2A = 0;
    }}

Grumpy_Mike

When you post code post all it it please.

Why are you messing about with the timers, it seems nothing related to your question.

Do you know about a compound if statement?

Code: [Select]
if( this == that && then == theOther)

The && is a logic AND and both statements have to be true.

Delta_G

If you want to see if something changed, then you need to have a variable where you keep up with the last state.  And everytime you update your variable, you first copy it to the other variable so you know what it used to be. 

Go have a look at the "State Change Example" that comes with the IDE.  You can see there how they track the last state of the pin in the lastButtonState variable. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

devasri

when I use the logic AND gates it doesn't work properly, the old inputs are still stored

Delta_G

You must have done it wrong.  Why don't you post the new code?

Why do we always have to ask?  Is it really that hard to think that someone would need to be able to see what you did in order to tell you what you did wrong?  Or are people really getting that dumb these days?  I know intelligence is on its way out but is it really that bad already?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Grumpy_Mike

#7
Nov 08, 2017, 05:29 pm Last Edit: Nov 08, 2017, 05:30 pm by Grumpy_Mike
Quote
when I use the logic AND gates
Nobody mentioned AND gates. These are AND gates


BUT people did mention posting ALL your code which you have still not done.

devasri

sorry for that

this is the code I'm using now:
Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>

void setup() {
  // put your setup code here, to run once:
  noInterrupts();
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(CS22);
  OCR2A = 0;
  OCR2B = 0;
  TCNT2 = 0;
  interrupts();

  attachInterrupt(digitalPinToInterrupt(2), t1_ISR, RISING);     //enable interrupt service routines on pins 2 and 3
  attachInterrupt(digitalPinToInterrupt(3), t2_ISR, RISING);
}

volatile unsigned int t1;
volatile unsigned int t2;
volatile unsigned int oldt1 = 0;
volatile unsigned int oldt2 = 0;

void loop() {
  // put your main code here, to run repeatedly:
  while((t1 != oldt1) && (t2 != oldt2)){
    if(t1<t2){
      OCR2A = t2-t1;   //duty cycle   50%
      OCR2B = 0;
    }
    else if(t2<t1){
      OCR2B = t1-t2;
      OCR2A = 0;
    }
  }
  oldt1 = t1;
  oldt2 = t2;
}

void t1_ISR(){
  t1 = TCNT2;
}

void t2_ISR(){
  t2 = TCNT2;
}

Grumpy_Mike

Thanks but I have no idea what this code is trying to do and more specifically why it is trying to do it. Also why are you messing with the timer's registers when it would seem doing a simple analogWrite would do the job.

However their are things you are doing wrong with handling the interrupts.

The variables you are using are int types and this is an 8 bit processor. Therefore any operations using them can have the rug pulled out from underneath them by the interrupt service routine changing the value while the main loop accesses one byte of the int and then the next byte of the newly changed int. As their is no need for them to be ints change them to byte type.

devasri

I am trying to output a pwm signal to one of two output pins depending on whether the delay is leading or lagging and the duty cycle of the pwm should reflect the time delay of the two input square waves, we are reading the phase shift basically so it can directly go to the Voltage controlled oscillator after we filter it and use a summing amplifier

Go Up