RegulationIntensite/RegulationIntensite.ino

48 lines
1.0 KiB
Arduino
Raw Normal View History

2017-02-08 00:45:16 +01:00
#include "RegulationIntensite.h"
#define __RESISTOR_VALUE__ 10000
#define __ANALOGIN_BR__ A0
#define __ANALOGIN_AR__ A1
#define __PWM_OUT__ 3
2017-02-09 14:12:10 +01:00
float target_curent = 0.05;//ma
2017-02-08 00:45:16 +01:00
void setup()
{
pinMode(__ANALOGIN_AR__,0);
pinMode(__ANALOGIN_BR__,0);
pinMode(__PWM_OUT__,1);
2017-02-09 14:12:10 +01:00
digitalWrite(__PWM_OUT__, 122);
2017-02-08 00:45:16 +01:00
Serial.begin(9600);
}
void loop()
{
2017-02-09 14:12:10 +01:00
static float curent;
2017-02-08 00:45:16 +01:00
static int pwm_value = 0;
curent = get_curent();
2017-02-09 14:36:48 +01:00
Serial.println(curent);
if(curent<target_curent*0.95)
2017-02-09 14:12:10 +01:00
{
2017-02-08 00:45:16 +01:00
pwm_value++;
2017-02-09 14:12:10 +01:00
analogWrite(__PWM_OUT__, pwm_value);
}
2017-02-08 00:45:16 +01:00
else if(curent>target_curent)
2017-02-09 14:12:10 +01:00
{
2017-02-08 00:45:16 +01:00
pwm_value--;
2017-02-09 14:12:10 +01:00
analogWrite(__PWM_OUT__, pwm_value);
}
2017-02-09 14:36:48 +01:00
delay(10);
2017-02-08 00:45:16 +01:00
}
float get_curent()
{
int b = analogRead(__ANALOGIN_BR__);
int a = analogRead(__ANALOGIN_AR__);
2017-02-09 14:12:10 +01:00
float dv = (b-a)*(5.0/1023.0) ; //Tension réel
2017-02-09 14:36:48 +01:00
float curent = (dv/(float)__RESISTOR_VALUE__)*1000;
2017-02-09 14:12:10 +01:00
2017-02-09 14:36:48 +01:00
//Serial.print(a);Serial.print(";");Serial.print(b);Serial.print(";");Serial.print(dv);Serial.print(";");Serial.print(curent);Serial.print(";");
2017-02-09 14:12:10 +01:00
return curent;
2017-02-08 00:45:16 +01:00
}