0 Members and 5 Guests are viewing this topic.
const int analogInPin = A3; // Analog input pin that the potentiometer is attached toconst int analogOutPin = 3; // Analog output pin that the LED is attached toconst int RedLED=8;const int YellowLED=9;const int BlueLED=10;const int switchpin=7; int switchstate=0;int sensorValue = 0; // value read from the potint outputValue = 0; // value output to the PWM (analog out)void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: switchstate=digitalRead(switchpin); if(switchstate==1){analogWrite(analogOutPin, outputValue); } if(switchstate==0){ analogWrite(analogOutPin, 0); }if(outputValue>=180){digitalWrite(RedLED,HIGH); digitalWrite(YellowLED,LOW); digitalWrite(BlueLED,LOW); }if(160<outputValue&&outputValue<180){digitalWrite(RedLED,LOW); digitalWrite(YellowLED,HIGH); digitalWrite(BlueLED,LOW); }if(160>=outputValue){digitalWrite(RedLED,LOW); digitalWrite(YellowLED,LOW); digitalWrite(BlueLED,HIGH); } // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.print(outputValue); Serial.print("\t switch = "); Serial.println(switchstate); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }
//pieced together and coded by the Zanderist(AWA) int analogInPin = A3; // Analog input pin that the potentiometer is attached to int analogOutPin = 3; // Analog output pin that the LED is attached to int analougePin=A2;//battery level read point int hvoltage=5;//pin for high voltage LED int lvoltage=4;//pin for low voltage LED int RedLED=8;//pin for red level LED int YellowLED=9;//pin for yellow level LED int BlueLED=10;//pin for blue level LED int switchpin=7;//pin for switch int switchstate=0;//state of the switch int mode=0;//sets which voltage mode to run int levelshutdown=0;//int used as boolean to conserve power int fadedone=0;//int used as boolean to stop fadding int readenable=1;//int used as boolean to enable battery voltage sample.int sensorValue = 0; // value read from the potint outputValue = 0; // value output to the PWM (analog out)int heatpwm=0;//new pwm value for batmedium.int heatpwminc=-5;int samplepwm=0; //take a reading of the PWMint outputPWM=0;int voltageValue=0;//for voltage readingint VoutputValue=0;//for voltage computationfloat Rratio=0.4;//divider ratiofloat vout=0;//voltage out of the dividerfloat vin=0;//approx voltage at batteryvoid setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); pinMode(RedLED, OUTPUT); pinMode(YellowLED, OUTPUT); pinMode(BlueLED, OUTPUT); pinMode(hvoltage, OUTPUT); pinMode(lvoltage, OUTPUT); pinMode(switchpin, INPUT); }void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: // check the battery voltage voltageValue=analogRead(analougePin); vout=(voltageValue*4.68)/1024.0; vin = vout/Rratio; //read switch switchstate=digitalRead(switchpin); ////// if(readenable==1){ outputPWM=outputValue; if(vin>7.51){mode=2;} if(6.99<=vin&&vin<=7.50){mode=1;} if(vin<6.98){mode=0;} } if(switchstate==1){ readenable=0; if(mode==2){ LEDS(); bathighvoltage();} if(mode==1){ if(fadedone==0){ LEDS();} batmediumvoltage();} if(mode==0) { batlowvoltage(); } } if(switchstate==0){ readenable=1; samplepwm=1; analogWrite(analogOutPin, 0); turnoffLEDS(); fadedone=0; } // print the results to the serial monitor: Serial.print("heatpwm= \t" ); Serial.print(outputPWM); Serial.print("enable= \t" ); Serial.print(readenable); Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.print(outputValue); Serial.print("Voltage=\t"); Serial.print(vin); Serial.print("\t switch = "); Serial.println(switchstate); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }/////////////////////////////\void bathighvoltage(){ digitalWrite(hvoltage,HIGH);analogWrite(analogOutPin, outputPWM);}/////////////////////////////\void batmediumvoltage(){ if(samplepwm==1){heatpwm=outputValue;samplepwm=0;} if(fadedone==0){ digitalWrite(lvoltage,HIGH); if(heatpwm>0){ heatpwm=heatpwm+heatpwminc; analogWrite(analogOutPin,heatpwm); } if(heatpwm<=0){fadedone=1;turnoffLEDS();analogWrite(analogOutPin,0);} }}/////////////////////////////\void batlowvoltage(){digitalWrite(lvoltage,HIGH);delay(250);digitalWrite(lvoltage,LOW);delay(250);}/////////////////////////////void LEDS(){if(outputValue>=180){digitalWrite(RedLED,1); digitalWrite(YellowLED,0); digitalWrite(BlueLED,0);}if(160<outputValue&&outputValue<180){digitalWrite(RedLED,0); digitalWrite(YellowLED,1); digitalWrite(BlueLED,0);}if(160>=outputValue){digitalWrite(RedLED,LOW); digitalWrite(YellowLED,LOW); digitalWrite(BlueLED,HIGH);}}/////////////////////////////void turnoffLEDS(){digitalWrite(RedLED,0); digitalWrite(YellowLED,0); digitalWrite(BlueLED,0);digitalWrite(hvoltage,LOW);digitalWrite(lvoltage,LOW);}
You should check voltage both open circuit and under load. The reason is that you don't want cell voltage to drop below a certain voltage at any time. Minimal voltage is 3V per cell for LiPos, but high drain 18650s can go lower. The HE2s can actually go down to 2.5V, but the rest are 2.7V. Though you shouldn't take them down that low since it increases wear and does not garner much extra run time. 3V for 18650s and 3.2V for LiPos is ideal. Minimal voltage specification is for voltage under load, not open circuit. You'll find that when you take a cell down to the minimum under load, it will climb back up in voltage an amount after it's been sitting idle. It can be as much as a half volt depending on how heavy the load.What you can do is curve the battery yourself which sounds difficult but it's not. Discharge graphs only show voltage under load and you need open circuit battery voltages for your fuel gauge. Just charge up the battery fully then discharge it in precise 15 to 30 second intervals with a known current. Take a reading after the load is removed and you'll get a set of plot points for open circuit voltage versus battery capacity. It would be possible to gauge a battery under load if you know the battery's DC resistance and the load's amperage draw. In that case you can add current times DCR to the voltage reading then index it to your graph for open circuit voltage versus charge capacity. You can find a battery's DCR on the bench pretty simply by measuring voltage drop with a known current. You should measure it at about 50% capacity since it goes down somewhat with a full charge and goes up somewhat as the battery approaches discharged.Curving a battery yourself can yield a very accurate fuel gauge. On my own mods, it's pretty darn close. Checking against a hit counter I get a pretty consistent number of hits for every 10% drop in capacity shown by the fuel gauge.Oh, I should point out that your circuit is not actually going to measure true open circuit voltage since your electronics are going to draw some current. However a properly designed MCU system should not draw much so it's in effect open circuit. On my own mods, the circuit is drawing less than 10mA when taking an open circuit battery measurement, not enough to make any measurable difference. If for some reason your circuit draws a lot of current when taking a battery measurement you would have to take that into account.
If you have room on the screen you can just display the battery voltage. But I'm lazy like that :p
If you have room on the screen you can just display the battery voltage.
I'm considering using a constant current source for my next project though, unless anyone knows a better way.
can hall effect sensors be used to measure the load of a pwm mod like this.
That ends up giving you the mean, though.