Go Down

Topic: External interrupt on Atmega168 (Read 3321 times) previous topic - next topic

Whandall

#30
May 02, 2016, 10:18 pm Last Edit: May 02, 2016, 10:22 pm by Whandall Reason: added note, rephrasing
If you want to see signals per revolution, you have to reset the count on each round(*).

Printing the counts as fast as the (blocking!!!!) serial line allows, shows nothing, apart from ascending numbers.

(*) Yes, this reset has to be guarded by noInterrupts/interrupts too, because the fields are multi-byte and volatile.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Whandall

You could try to print the counts each second
Code: [Select]
volatile long a = 0;
volatile long b = 0;

void GestIntCodM01() {
  a++;
}
void GestIntCodM02() {
  b++;
}
void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);     //DR M02
  pinMode(9, OUTPUT);     //PWM M01
  pinMode(1, OUTPUT);     //DR M01 ARE YOU SHURE ABOUT PIN 0 AND 1 ???
  pinMode(0, OUTPUT);     //DR M02 ARE YOU SHURE ABOUT PIN 0 AND 1 ???
  pinMode(10, OUTPUT);    //PWM M02
  pinMode(11, OUTPUT);    //DR M02
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  analogWrite(9, 0); analogWrite(10, 75);
  digitalWrite(8, 0); digitalWrite(11, 1);
  digitalWrite(1, 1); digitalWrite(0, 0);
  attachInterrupt(0, GestIntCodM01, RISING);
  attachInterrupt(1, GestIntCodM02, RISING);
}
void loop() {
  static unsigned long lastPrint;
  unsigned long topLoop = millis();

  if (topLoop - lastPrint >= 1000) {
    lastPrint = topLoop;

    noInterrupts();
    long copy_a = a;
    long copy_b = b;
    a = 0;
    b = 0;
    interrupts();

    Serial.print("M01 = ");
    Serial.print(copy_b);
    Serial.print("    M02 = ");
    Serial.println(copy_a);
  }
}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Wahrani31

You could try to print the counts each second
Code: [Select]
volatile long a = 0;
volatile long b = 0;

void GestIntCodM01() {
  a++;
}
void GestIntCodM02() {
  b++;
}
void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);     //DR M02
  pinMode(9, OUTPUT);     //PWM M01
  pinMode(1, OUTPUT);     //DR M01 ARE YOU SHURE ABOUT PIN 0 AND 1 ???
  pinMode(0, OUTPUT);     //DR M02 ARE YOU SHURE ABOUT PIN 0 AND 1 ???
  pinMode(10, OUTPUT);    //PWM M02
  pinMode(11, OUTPUT);    //DR M02
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  analogWrite(9, 0); analogWrite(10, 75);
  digitalWrite(8, 0); digitalWrite(11, 1);
  digitalWrite(1, 1); digitalWrite(0, 0);
  attachInterrupt(0, GestIntCodM01, RISING);
  attachInterrupt(1, GestIntCodM02, RISING);
}
void loop() {
  static unsigned long lastPrint;
  unsigned long topLoop = millis();

  if (topLoop - lastPrint >= 1000) {
    lastPrint = topLoop;

    noInterrupts();
    long copy_a = a;
    long copy_b = b;
    a = 0;
    b = 0;
    interrupts();

    Serial.print("M01 = ");
    Serial.print(copy_b);
    Serial.print("    M02 = ");
    Serial.println(copy_a);
  }
}

Thank you Mr.Whandall
i let my robot todey with a friend and i will recuperate it tomorrow to test the code that you provided to me, i hope that this one will work properly,Thank you Sir and I apologize for the disturbance   :smiley-red:   

Wahrani31

Here is how to modify your loop to make the protected copies of the volatile variables.
Declare your protected copies along with the voltatile variables.

Code: [Select]

volatile long a=0;
volatile long b=0;
long copy_a = 0;
long copy_b = 0;


Code: [Select]

void loop() {
  
noInterrupts()
copy_a = a;
copy_b = b;
interrupts()
  
Serial.print("M01 = ");
Serial.print(copy_b);
Serial.print("    M02 = ");
Serial.println(copy_a);
}

Thank you too Mr.cattledog i will test the variables copy_a and copy_b and i will provide you with results

The next video shows the first test of my modest robot following a short black line
My_Line_Follower_First_Steps


Wahrani31

#34
May 05, 2016, 01:42 am Last Edit: May 05, 2016, 01:48 am by Wahrani31


Mr.Whandall, yes i am 100% sure about the OUTPUTS to control the motors direction,
however when i implemented your code into my robot i catched just one motor and the other was not moving.. this are the results for 2 different speeds "pwm" when the motor runs for 10 round and the 1 tiks chanels of the encoder are councted, so the results are for the same motor not for 2 motors


-------------------------------------------------------------------------------------------------------------------------------------------------
Thank You Very Much Mr.cattledog I got the following ressults using the modification that you told to me and the deffernce b'twin the number of ticks counted for slow and high speed is now Reduced


I made the motors to run until the ticks counted reach 90 ticks but i had a passing for about 10 or 15 ticks and my motor was doing 1.15 round, but when i diminuate the number of ticks to 80 ticks the motor was doing a perfect 1 round and the arduino counted 92 ticks/rd


Thank You So Much Sir, You and The Other Memebrs helped me alot.


Go Up