Files
Arduino/ATMEGA_HALL_EMETTEUR/ATMEGA_HALL_EMETTEUR.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

175 lines
4.5 KiB
C++
Executable File

#include <RCSwitch.h>
#include <Narcoleptic.h>
#include <Wire.h>
#define SEND_MESSAGE_DELAY 3000 // Ne pas dépasser 32000 !! Delay in ms between each value's extraction
#define SEND_433_PAUSE 160 // 16 multiple
#define DEBUG true
const unsigned long activation = 111269; // Radiateur
const unsigned long idRad=331969;
const unsigned long desactivation = 962111; // Fin radiateur
const unsigned int delai = 11;
const unsigned long TIME = 512;
const unsigned long TWOTIME = TIME*2;
const int analogIn = A0;
int mVperAmp = 185; // 185 pour 5A, use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
RCSwitch mySwitch = RCSwitch();
void setup() {
#ifdef DEBUG
Serial.begin(9600);
Serial.println("\n[Oregon V2.1 encoder]");
#endif
pinMode(13, OUTPUT);
mySwitch.enableTransmit(9);
}
void loop() {
enableADC();
delay(100);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for a second
digitalWrite(13, LOW);
long vcc = readVcc();
#ifdef DEBUG
Serial.print("Send vcc=");
Serial.println(vcc);
#endif
mySwitch.send(activation, 24);
delay(delai); //delayMicroseconds
double amp = getAmp();
myMessageSend(idRad,(220.0 * amp));
mySwitch.send(desactivation, 24);
delay(delai);
delayMicroseconds(TWOTIME*8);
disableADC();
Narcoleptic.delay(SEND_MESSAGE_DELAY);
Narcoleptic.delay(SEND_MESSAGE_DELAY);
}
void myMessageSend(long id, long value) {
#ifdef DEBUG
Serial.print("Send id="); Serial.print(id);
Serial.print(" value="); Serial.println(value);
#endif
mySwitch.send(id, 24); //"000000000001010100010001");
delay(delai);
mySwitch.send(value, 24); //"000000000001010100010001");
delay(delai);
//delay(5000);
//delayMicroseconds(TWOTIME*8);
}
void enableADC() {
//bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
delay(2); // Wait for Vref to settle
while (bit_is_set(ADCSRA,ADSC));
}
void disableADC() {
//ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
//while (bit_is_set(ADCSRA,ADSC));
delay(2); // Wait for Vref to settle
}
//--------------------------------------------------------------------------------------------------
// Read current supply voltage
//--------------------------------------------------------------------------------------------------
long readVcc() {
bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
long result;
// Read 1.1V reference against Vcc
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
#endif
// ADCSRB = 0;
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate Vcc in mV
// ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
// analogReference(DEFAULT);
return result; // Vcc in millivolts
}
double getAmp() {
int i = 0;
RawValue = 0;
// Somme du courant alternatif pendant 20 ms ==> 50hz
// Détermination du max et max pour hauteur de crete
int vmin = 1024;
int vmax = 0;
for (i = 0; i < 20; i++) {
int value = analogRead(analogIn);
if (value >= 0) {
RawValue += value;
vmax = max(value,vmax);
vmin = min(value,vmin);
} else {
i--;
}
delay(1);
}
#ifdef DEBUG
Serial.print("min = " );
Serial.print(vmin);
Serial.print(" max = " );
Serial.print(vmax);
#endif
// La valeur maxi * racine carrée de 2 pour obtenir la tension "réelle"
// La tension efficace pour l'effet Hall étant réduite d'un facteur 0,707
Voltage = ((vmax - vmin) / 430.0) * 5000;
Amps = 5.5 * (vmax - vmin) / 473.0;
// Serial.print(" Raw Value = " ); // shows pre-scaled value
// Serial.print(RawValue);
#ifdef DEBUG
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
#endif
return Amps;
}