LoginSignup
0
0

More than 1 year has passed since last update.

Arduino UNOとマイコン応用I2C超音波距離センサーで遊ぶ(タイマー割り込み)(LPF)

Last updated at Posted at 2022-04-29

目的
オリジナルI2C超音波距離センサーをつかい
任意の時間でローパスフィルター(LPF)を掛けた値の表示

o_con367.jpg

o_con366.jpg



//i2c_master_interrupt_HC_SR04_UNO_1

#include <Arduino.h>
#include <Wire.h>

// 340m 1000ms,34m 100ms,3.4m 10ms

// 1/100秒の指定
#define AA 50 
#define BB (65536-((62500/100)*AA))


//初期化
void setup()
{
 
  //シリアルポートの初期化
  Serial.begin(9600);

  //I2Cの初期化
  Wire.begin(); //UNO

  //タイマー割り込み
  TCCR1A = 0; // 初期化
  TCCR1B = 0; // 初期化
  //TCNT1 = 3036; 
  TCNT1 = BB;
  TCCR1B |= (1 << CS12);  //CS12 -> 1  prescaler = 256
  TIMSK1 |= (1 << TOIE1); //TOIE -> 1

} //setup

int  h1,h2,h3;
int  p;
char in_l;

//int  h;

ISR(TIMER1_OVF_vect) {

  //Serial.println(millis());

  Serial.print("80,");  
  Serial.print( (int)in_l ); //UNO
  Serial.print(",0");
  Serial.println(" ");

  //TCNT1 = 3036;
  TCNT1 = BB;

}//ISR


//メインループ
void loop()
{

  int  l=0;

  unsigned char b = 200;

  //読み込み
  Wire.requestFrom(8, 1);

  while(Wire.available())  {    // 要求より短いデータが来る可能性あり
    b = Wire.read();       // 1バイトを受信
  }//while

  if( b <= 139 ) {
    l = b + 60;
  } else {
    l = ((int)(b - 140 + 20)) * 10;
  }

  //Serial.print("80,");

  // h = (h/2)+(h/4)+(l/4)
  // h = (h * 0.75) + ( l * 0.25 )
  // h = (h>>1)+(h>>2)+(l>>2); 
  
  p = (h1+h2+h3+l)>>2;
  h3 = h2;
  h2 = h1;
  h1 = l;
  in_l = (char)(p/10);

  //Serial.print( (int)in_l ); //UNO

  //Serial.print(",0");
  //Serial.println(" ");

  delay(20); // 0.5秒の待ち

} //loop





0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0