Global variabale can't update.... see running var

HI ALL, Here is my code, the main perpose is, when the IR get a single, let the servo running, to avoid more command to servo, I set global "running" to flag the servo status. but I always equal 0. why?

#define IR_RECEIVE_PIN 2
#define IR_SEND_PIN 3
#define SERVO_PIN 9  // D9 是舵机脚


#include <IRremote.hpp>
#include <Arduino.h>
#include <Servo.h>

uint8_t sAddress = 0;
uint8_t sCommand = 0x63;  // 十进制 99
uint8_t rCommand = 0x58;  // 十进制 88
uint8_t sRepeats = 0;
int running = 0;  // set it the Servo runing.


volatile uint8_t hitData;
Servo myservo;  // 创建一个舵机.

void setup() {
  Serial.begin(9600);  // 初始化串口通信
  IrSender.begin();
  IrReceiver.begin(IR_RECEIVE_PIN);
  pinMode(LED_BUILTIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(IR_RECEIVE_PIN), HIT, CHANGE);

  trun();
}

void HIT() {
  if (IrReceiver.decode()) {
    hitData = IrReceiver.decodedIRData.command;
    Serial.print("Reciev.");
    Serial.print(hitData);
    Serial.print("....running.....");
    Serial.println((int)&running,HEX);
    if (hitData == rCommand && running==0) {  // indicates received signal, checking it runing or not.
      Serial.println("Get Drop Command....");
      hitData = 0x00;
      running=1;  // set 1, flag it is running.
      trun();
      running=0;  // set it back to ready to run....
    }
  }
  IrReceiver.resume();
}

void loop() {
  // Serial.print("Send.");
  // Serial.println(sCommand);
  IrSender.sendNEC(sAddress, sCommand, sRepeats);
  delay(355);

  // trun();
}

void trun() {
  digitalWrite(LED_BUILTIN, HIGH);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
  // 开始旋转电机,100度
  int pos = 0;                              // variable to store the servo position
  for (int pos = 0; pos <= 60; pos += 1) {  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);  // tell servo to go to position in variable 'pos'
    delay(12);           // waits 15 ms for the servo to reach the position
  }
  delay(200);
  for (int pos = 60; pos >= 0; pos -= 1) {  // goes from 180 degrees to 0 degrees
    myservo.write(pos);                     // tell servo to go to position in variable 'pos'
    delay(12);                              // waits 15 ms for the servo to reach the position
  }
  digitalWrite(LED_BUILTIN, LOW);
  myservo.detach();
}

you set it back to 0

you should not attach and detach the servo each time you call this function.
detaching will stop the servo-signal and this means your servo does not receive any servo-signal and then is in an undefined state.

As long as you want the servo to keep its position = standing still you must send a servo-signal to the servo. myservo.detach() terminates signal-creation.
==> the servo does not know what to do and might start wiggling.

myServo.attach() is called only once in function setup.

read about how RC-servos work.

best regards Stefan

You are spending way too much time in an interrupt service routine, besides the fact that delay() should not be working at all while interrupts are disabled.

1 Like

trun() will take some time, the IRreceive 3 time each second, turn will spend more than 2 seconds, I need trun once after got IRReceive, if got more single from IR, don't need trigger turn.

not understandable
write with more words
and correct grammar

it doesn't matter how long turn() takes. at the line where you print running it is always 0

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.