5

私は電子機器と Arduino に関しては初心者なので、とにかくいじってみるのが一番ですよね?

LDR (Light Density Resistor) を利用する小さなプロジェクトを開始し、それを使用して光線がブロックまたはオフになる頻度を計算したいと考えています。

デバッグの目的で、定義された周波数 (5 Hz など) で点滅する小さな LED をセットアップし、LCD を使用して出力を表示します。

画面

右上隅に問題があります...正しく動作していないようです。登録した周波数を表示するつもりでしたが、デバッグ時は5秒(5,000ミリ秒)間隔でカウント数を表示するように設定しました。しかし、設定した周波数に関係なく、24 が最大のように見えます (正しい数値 [5 秒 x 5 Hz = 25] を表示するには、時間間隔で割り、結果を Hz で取得します)。また、9 Hz の場合は 24.0 なども表示されます。

私もこれを持っています:YouTubeビデオ

...しかし、最初のいくつかの手探りにより、LED が少し移動したため、間違ってカウントされました。しかし、最終的には「機能します」..しかし、24.0は一定のままです

これは私のコードです:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
int booBlocked = 0;
int counter = 0;
int checkValue = counter + 1;

int ledPin = 3;                // LED connected to digital pin 3
int value = LOW;                // previous value of the LED
long previousMillis = 0;        // will store last time LED was updated
long freqency = 5; // Hz (1/sec)
long thousand = 1000;
long interval = thousand / freqency; // milliseconds
//long interval = 59;           // interval at which to blink (milliseconds)

int tValue = 0; // Threshold value used for counting (are calibrated in the beginning)
long pMillis = 0;
long inter = 5000;
int pCount = 0;
float freq = 0;  // Calculated blink frequency...

void setup() { 
  lcd.begin(16, 2);
  lcd.setCursor(0,1);  lcd.print(interval);
  lcd.setCursor(4,1);  lcd.print("ms");

  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
}

void loop() {
  // Print LDR sensor value to the display  
  int sensorValue = analogRead(A0);
  lcd.setCursor(7,1);
  lcd.print(sensorValue);
  delay(100);

  if (millis() > 5000){
    doCount(sensorValue);
    updateFreq();
    lcd.setCursor(7+5,0);
    lcd.print(freq);
  } else {
    setThresholdValue(sensorValue);
    lcd.setCursor(7+5,1);
    lcd.print(tValue);
  }

 // LED BLINK
  if (millis() - previousMillis > interval) {
    previousMillis = millis();   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }    
}

void updateFreq(){
 long now = millis();
 long t = now - pMillis;
 if (t >= 10000) {
   freq = (float) (counter - pCount);
   //freq = ((float) (counter - pCount)) / (float) 10.0;
   pMillis = now;   // remember the last time we blinked the LED
   pCount = counter;
  } 
}

void setThresholdValue(int sensorValue){
  if (sensorValue > int(tValue/0.90)){
    tValue = int (sensorValue*0.90);
  }
}

void doCount(int sensorValue){
    // Count stuff
  if (sensorValue < tValue){
    booBlocked = 1;
    //lcd.setCursor(0,0);
    //lcd.print("Blocked");
  } else {
    booBlocked = 0;
    //lcd.setCursor(0,0);
    //lcd.print("       ");    
  }

  if (booBlocked == 1) {
   if (counter != checkValue){
    counter = counter + 1;
    lcd.setCursor(7,0);
    lcd.print(counter);
   }   
  } else {
   if (counter == checkValue){
    checkValue = checkValue + 1;
     }
   }
}

アップデート

より「クリーンな」コード (私自身の回答を参照してください)

#include <LiquidCrystal.h>
// Initiate the LCD display
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 150;                // ms
long updateTime = 0;

// Declare the pins
int ledPin = 3;                       // LED connected to digital pin 3

// LED setup
int value = LOW;                      // previous value of the LED
long previousMillis = 0;              // will store last time LED was updated 
long freqency = 16;                   // Hz (1/sec)
long thousand = 1000;
long blinkInterval = thousand / freqency;  // milliseconds

//// LDR counter variables ////
// Counting vars
static int counter = 0;
int booBlocked = 0;
int checkValue = counter + 1;
// Calibration vars
long onBootCalibrationTime = 5000;     // time [time] to use for calibration when the system is booted
static int threshold = 0;              // Value used for counting (calibrated in the beginning)
float cutValue = 0.90;                 // Procent value used to allow jitting in the max signal without counting.

// Frequency vars
float freq = 0;                        // Calculated blink frequency...
long frequencyInterval = 5000;        // time [ms] 
long pMillis = 0;
int pCount = 0;



void setup() {
  // Setup the pins
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  // display static values
  lcd.begin(16, 2);
  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
  lcd.setCursor(0,1);  lcd.print(blinkInterval);
  lcd.setCursor(4,1);  lcd.print("ms");

  // Setup that allows loggin
  Serial.begin(9600);  // Allows to get a readout from Putty (windows 7)
}

void loop() {  
  long time = millis();
  int sensorValue = analogRead(A0);

  // Blink the LED
  blinkLED(time);

  // Calibrate or Count (AND calculate the frequency) via the LDR
  if (time < onBootCalibrationTime){
    setThresholdValue(sensorValue);
  } else {    
    doCount(sensorValue);
    updateFreq(time);
  }


  // Update the LCD
  if (time > updateTime){
    updateTime += updateInterval;  // set the next time to update the LCD

   // Display the sensor value
    lcd.setCursor(7,1);  lcd.print(sensorValue);
   // Display the threshold value used to determined if blocked or not
    lcd.setCursor(7+5,1);  lcd.print(threshold);
   // Display the count
    lcd.setCursor(7,0);
    lcd.print(counter);
   // Display the calculated frequency
    lcd.setCursor(7+5,0);  lcd.print(freq);   
  }  
}

void blinkLED(long t){
  if (t - previousMillis > blinkInterval) {
    previousMillis = t;   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }
}

void setThresholdValue(int sValue){
  if (sValue > int(threshold/cutValue)){
    threshold = int (sValue*cutValue);
  }
}

void doCount(int sValue){
  if (sValue < threshold){
    booBlocked = 1;
  } else {
    booBlocked = 0;  
  }

  if (booBlocked == 1) {
   if (counter != checkValue){
    counter = counter + 1;
   }   
  } else {
   if (counter == checkValue){
    checkValue = checkValue + 1;
     }
   }
}

void updateFreq(long t){
 long inter = t - pMillis;
 if (inter >= frequencyInterval) {
   freq = (counter - pCount) / (float) (inter/1000);
   pMillis = t;           // remember the last time we blinked the LED
   pCount = counter;
  } 
}

このコードは私の質問を解決しませんが、読みやすくなっています。

4

4 に答える 4

0

バグの1つを見つけたと思います。使用してdelay()いたので問題が発生しました。

コードをクリーンアップしました:

#include <LiquidCrystal.h>
// Initiate the LCD display
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 150;                // ms
long updateTime = 0;

// Declare the pins
int ledPin = 3;                       // LED connected to digital pin 3

// LED setup
int value = LOW;                      // previous value of the LED
long previousMillis = 0;              // will store last time LED was updated 
long freqency = 16;                   // Hz (1/sec)
long thousand = 1000;
long blinkInterval = thousand / freqency;  // milliseconds

//// LDR counter variables ////
// Counting vars
static int counter = 0;
int booBlocked = 0;
int checkValue = counter + 1;
// Calibration vars
long onBootCalibrationTime = 5000;     // time [time] to use for calibration when the system is booted
static int threshold = 0;              // Value used for counting (calibrated in the beginning)
float cutValue = 0.90;                 // Procent value used to allow jitting in the max signal without counting.

// Frequency vars
float freq = 0;                        // Calculated blink frequency...
long frequencyInterval = 5000;        // time [ms] 
long pMillis = 0;
int pCount = 0;



void setup() {
  // Setup the pins
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  // display static values
  lcd.begin(16, 2);
  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
  lcd.setCursor(0,1);  lcd.print(blinkInterval);
  lcd.setCursor(4,1);  lcd.print("ms");

  // Setup that allows loggin
  Serial.begin(9600);  // Allows to get a readout from Putty (windows 7)
}

void loop() {  
  long time = millis();
  int sensorValue = analogRead(A0);

  // Blink the LED
  blinkLED(time);

  // Calibrate or Count (AND calculate the frequency) via the LDR
  if (time < onBootCalibrationTime){
    setThresholdValue(sensorValue);
  } else {    
    doCount(sensorValue);
    updateFreq(time);
  }


  // Update the LCD
  if (time > updateTime){
    updateTime += updateInterval;  // set the next time to update the LCD

   // Display the sensor value
    lcd.setCursor(7,1);  lcd.print(sensorValue);
   // Display the threshold value used to determined if blocked or not
    lcd.setCursor(7+5,1);  lcd.print(threshold);
   // Display the count
    lcd.setCursor(7,0);
    lcd.print(counter);
   // Display the calculated frequency
    lcd.setCursor(7+5,0);  lcd.print(freq);   
  }  
}

void blinkLED(long t){
  if (t - previousMillis > blinkInterval) {
    previousMillis = t;   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }
}

void setThresholdValue(int sValue){
  if (sValue > int(threshold/cutValue)){
    threshold = int (sValue*cutValue);
  }
}

void doCount(int sValue){
  if (sValue < threshold){
    booBlocked = 1;
  } else {
    booBlocked = 0;  
  }

  if (booBlocked == 1) {
   if (counter != checkValue){
    counter = counter + 1;
   }   
  } else {
   if (counter == checkValue){
    checkValue = checkValue + 1;
     }
   }
}

void updateFreq(long t){
 long inter = t - pMillis;
 if (inter >= frequencyInterval) {
   freq = (counter - pCount) / (float) (inter/1000);
   pMillis = t;           // remember the last time we blinked the LED
   pCount = counter;
  } 
}

思ったほど正確ではありませんが、LEDの点滅の仕方が原因かもしれません。

また、影響があることも発見しfloat cutValue = 0.90;ました...バーを0.85に下げると、計算された頻度が減少します。

于 2013-02-16T15:58:39.617 に答える
0

あなたの計画の問題は、光密度抵抗器が周囲のすべての周囲光を拾うため、完全に環境に敏感になることです。

他のプロジェクトの希望はありますか?これは、コーディングではなく、エンジニアリングの学習体験のようです。

モータープロジェクトについて考えたことはありますか?個人的にはホーム オートメーションの方が好きですが、モーター プロジェクトはすぐにやりがいがあります。

于 2013-02-13T01:48:01.247 に答える
0

アルバートが彼の素晴らしいFreqPeriodCounter ライブラリを使用して私を助けてくれた後、私はコードを完全に変更しました

周波数を制御するためのポテンショメータも追加しました

これが私のコードです:

#include <FreqPeriodCounter.h>
#include <LiquidCrystal.h>

// FrequencyCounter vars
const byte counterPin = 3;  // Pin connected to the LDR
const byte counterInterrupt = 1; // = pin 3
FreqPeriodCounter counter(counterPin, micros, 0);

// LCD vars
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 200;                // ms
long updateTime = 0;

// LED vars
int ledPin = 5;                       // LED connected to digital pin 3
int value = LOW;                      // previous value of the LED
float previousMillis = 0;              // will store last time LED was updated 
static float freqency;                   // Hz (1/sec)
static float pfreqency;
static float blinkInterval;  // milliseconds

boolean logging = true;              // Logging by sending to serial

// Use potentiometer to control LED frequency
int potPin = 5;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor

void setup(void){ 
  // Setup the pins
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  val = analogRead(potPin);
  freqency = map(val, 0, 1023, 0, 25);                   // Hz (1/sec)
  pfreqency = freqency;
  blinkInterval = 1000 / (freqency*2);                  // milliseconds


  // LCD display static values
  lcd.begin(16, 2);
  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
  lcd.setCursor(14,0);  lcd.print("Hz");
  lcd.setCursor(0,1);  lcd.print(blinkInterval);
  lcd.setCursor(4,1);  lcd.print("ms");

  //   
  attachInterrupt(counterInterrupt, counterISR, CHANGE);

  // Logging
  if (logging) {Serial.begin(9600);}
}

void loop(void){ 
  // Loop vars
  float time = (float) millis();
  float freq = (float) counter.hertz(10)/10.0;

  // Blink the LED
  blinkLED(time);


  if (logging) {
    if(counter.ready()) Serial.println(counter.hertz(100));
  }

  // Update the LCD
  if (time > updateTime){
    updateTime += updateInterval;  // set the next time to update the LCD
    lcdNicePrint(7+3, 0, freq); lcd.setCursor(14,0);  lcd.print("Hz"); 
    val = analogRead(potPin);
    freqency = map(val, 0, 1023, 1, 30);

    if (freqency != pfreqency){
      pfreqency = freqency;      
      blinkInterval = 1000 / (freqency*2);                  // milliseconds

      lcdNicePrint(0,0, freqency);  lcd.setCursor(4,0);  lcd.print("Hz");
      lcd.setCursor(0,1);  lcd.print(blinkInterval);
      lcd.setCursor(4,1);  lcd.print("ms");
    }
  }
}

void lcdNicePrint(int column, int row, float value){
  lcd.setCursor(column, row);  lcd.print("00");
  if (value < 10) {lcd.setCursor(column+1, row);  lcd.print(value);}
  else {lcd.setCursor(column, row);  lcd.print(value);}
}  

void blinkLED(long t){

  if (t - previousMillis > blinkInterval) {
    previousMillis = t;   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }
}

void counterISR()
{ counter.poll();
}
于 2013-02-24T10:32:27.680 に答える