Arduino Kod Birleştirme

Tekno1246

Üye
Katılım
12 Şub 2021
Mesajlar
191
Puanları
1
Yaş
18
Konum
Ankara/Yenimahalle
Merhabalar. Arduino Uno ve potansiyometre ile DC 12V 4 pin PWM fanların hızını kontrol ediyorum. Buna ek olarak devir bilgisini de görmek istiyorum. İlk başta bunu 2 adet Arduino ile yapmayı düşünmüştüm fakat tek Arduino ile de aynı anda yapılabiliyormuş. Koddan pek anlamadığım için size danışmak istedim. İnternette hız kontrolü için ve devir okumak için kodlar buldum. Devir okuma koduna ek olarak 16x2 I2C modullü LCD ekrana yazdırmak için kod istiyorum. Sizden ricam verdiğim kodları birleştirip devir bilgisini LCD ekrana yansıtan bir kod yapabilir misiniz? En üstteki kod hız kontrolü için alttaki kod ise devir okumak için. @FakirMaker hocam belki yardım edebilirsiniz.
Kod:
int potVal;
int potPin = A2;
int fanVal;

const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;

const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency
const word TCNT1_TOP = 16000000 / (2 * PWM_FREQ_HZ);

void setPwmDuty(byte duty) {
OCR1A = (word)(duty * TCNT1_TOP) / 100;
}

void setup() {
pinMode(OC1A_PIN, OUTPUT);

// Clear Timer1 control and count registers
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;

TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << CS10);
ICR1 = TCNT1_TOP;
}

void loop() {
potVal = analogRead(potPin);
fanVal = map(potVal, 0, 1023, 0, 100);
setPwmDuty(fanVal);
}
Kod:
//project done by www.theorycircuit.com
//code by Crenn from http://thebestcasescenario.com/  thank you!
//Varibles used for calculations
int NbTopsFan; int Calc;

//The pin location of the sensor
int hallsensor = 2; typedef struct{

//Defines the structure for multiple fans and
//their dividers
char fantype;
unsigned int fandiv; }fanspec;

//Definitions of the fans
//This is the varible used to select the fan and it's divider,
//set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1;

void rpm ()
//This is the function that the interupt calls
{ NbTopsFan++; }

//This is the setup function where the serial port is initialised,
//and the interrupt is attached
void setup()
{ pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING); }

void loop ()
//Set NbTops to 0 ready for calculations
{ NbTopsFan = 0;

//Enables interrupts
sei();

//Wait 1 second
delay (1000);

//Disable interrupts
cli();

//Times NbTopsFan (which is apprioxiamately the fequency the fan
//is spinning at) by 60 seconds before dividing by the fan's divider
Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv);

//Prints the number calculated above
Serial.print (Calc, DEC);

//Prints " rpm" and a new line
Serial.print (" rpm\r\n");
}
 
Son düzenleme:
Prensip olarak bitmiş proje vermiyorum o yüzden derledim ama kart üzerinde çalıştırmadım hatalar eksikler olabilir. Bu senin projen ve senin tasarlaman, gereken bilgileri öğrenmen gerek. Gönderdiklerini birleştirdim ama efektif çalışcağını sanmam. Her 100ms de okuduğu değeri ekrana yazacak ama interruptlar timera etki edebilir araştırmadım. Ayrıca i2c ekranlar paralellere göre daha yavaştır 100ms de bir güncellemeye yetmeyebilir. Aşağıdaki kod parçası sana fikir verecektir. Çalışması için library manager dan bu kitaplığı yüklemen gerekir.

C++:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x70,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

int potVal;
int potPin = A2;
int fanVal;

const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;

const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency
const word TCNT1_TOP = 16000000 / (2 * PWM_FREQ_HZ);

int NbTopsFan; int Calc;
int hallsensor = 2; typedef struct{
char fantype;
unsigned int fandiv;
}fanspec;
fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1;

void rpm (){
  NbTopsFan++;
}
 
void setPwmDuty(byte duty) {
  OCR1A = (word)(duty * TCNT1_TOP) / 100;
}

void setup() {
  lcd.init();
  lcd.backlight();
  pinMode(OC1A_PIN, OUTPUT);
  pinMode(hallsensor, INPUT);
  attachInterrupt(0, rpm, RISING);
 
  // Clear Timer1 control and count registers
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
 
  TCCR1A |= (1 << COM1A1) | (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << CS10);
  ICR1 = TCNT1_TOP;
}

void loop() {
  potVal = analogRead(potPin);
  fanVal = map(potVal, 0, 1023, 0, 100);
  setPwmDuty(fanVal);
  NbTopsFan = 0;
  sei();
  delay (100);
  cli();
  Calc = ((NbTopsFan * 600)/fanspace[fan].fandiv);
  lcd.setCursor(1,0);
  lcd.print(Calc,DEC);
  lcd.print(" rpm");
}
 

Forum istatistikleri

Konular
128,179
Mesajlar
915,658
Kullanıcılar
449,940
Son üye
yavuzturan

Yeni konular

Geri
Üst