Honda Insight Forum banner

1 - 20 of 42 Posts

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #1 (Edited)
This is a DIY guide how to build a simple IMA battery discharger controlled by an Arduino for around $20. It will automatically discharge your IMA battery to a predetermined voltage and disconnect itself. You'll need some basic electronics skills and be able to solder, but not a lot more. I am calling it the SAD discharger, because that’s how we feel when our batteries are playing up.

One of the frustrating things about charge/discharge cycling the IMA battery with a ‘dumb’ charger and simple bulb discharger is having to regularly check the battery voltage during discharge. It would be much easier if you could hook up a discharger which would simply discharge the battery to a particular voltage and disconnect itself.

This is currently work in progress. If you've seen my posts on the IMA Boost or LSAS threads, you'll have noticed that my projects operate on a glacially slow timeline. I thought I'd post this up now because the cold weather is coming and that means P1449 season is upon us. It might mean some folks can get one of these made quickly in their own time and make their charge/discharge cycling easier and might kick my *** in gear to get my own one finished. Also gives folk a chance to improve it, correct my mistakes etc. I have come up with two setups. One simply discharges to a single set voltage and shuts off, the other has 3 selectable voltage presets. Either of these can be modified to include switching your dumb charger in and out and doing full automatic charge/discharge cycling to various voltages in as many steps as you like.

The circuit to do this with an Arduino is quite simple with basic electronics. The Arduino reads the IMA battery voltage through a potential divider. A 100k Ohm trimming potentiometer across the IMA battery is used for this, set so that one side is 2.5k Ohms and the other side is 97.5k Ohms. The Arduino reads a 0-5V range on the potential divider which is equivalent to 0-200V at the IMA battery end. It is therefore relatively easy to use the Arduino to calculate the battery voltage and use it as a controller to discharge the battery and shut it off when it reaches a certain voltage.

The main components are shown in the attached pic. I'm using an Arduino Uno. Why an Arduino and not something 'better'? Sometimes good enough really is good enough and these couldn't be simpler to use. There are bigger and smaller Arduinos you can use and much cheaper clone versions. I'll be discharging through resistors. The ones shown are 100 Watt 500 Ohm resistors, which will discharge the IMA battery at 320mA at 160V, dropping down to 200mA at 100V, 150mA at 75V and 100mA at 50V if used singly. The current is doubled if wired in parallel, halved if wired in series. Aluminium extrusion is for mounting the resistors and providing some rudimentary heatsinking. The Arduino needs a 5V power supply and ideally the resistors want a fan on them, so I opted to use a cheap ATX computer power supply with a switch. This conveniently provides the case, power and cooling, plus an additional 12V line to power the battery fan if needed..

88272


I'm going to gut much of the power supply and move the fan to the outside of the case to make space for the Arduino and resistors. It needs the green lead on the ATX connector connected to 0V so that it switches on and off when the power switch is flicked. I'll replace the bundle of leads with just the connector to my battery charging harness, which is conveniently one end of a 4-pin computer CPU power extension cable. 12V is available to power the battery fan if needed. For a more stable 5V supply, I've read that a 5-10 Ohm resistor should be put across the 0V and 5V rails so that there is a basic load at all times. You could also do this with a 6V bulb.

The discharge resistors will have to be switched in and out with a relay. The Arduino won't be able to power a relay directly, so you'll have to drive the relay through a small NPN transistor with a diode across the relay coil. I'm planning to mount all these small components on a small piece of prototyping board.

The only additional components on the single voltage discharge box are a couple of LEDs to say that discharge is in progress and discharge is complete. For the 3-preset box, I intend to use a 3-position switch with some resistors to tell the Arduino which voltage to go to.

Code is in the next two posts and works on the bench with a 0-5V variable supply as a doing the triggering. Next step for me is to wire it up for real.
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #2 (Edited)
This is the basic code to get the simple, single preset discharger to work.

Code:
/*
  Simple Arduino Discharger

  Code is based on ReadAnalogVoltage example

  A 100k trimming potentiometer goes across the IMA battery, set to 2500R on the 0V side and 97.5k on the +ve side. On a 0-200V scale, this creates a 0-5V range on the potential divider that the Arduino can read. Arduino pin A0 goes between 0V and the 2500R tap.
 
  Reads the analog voltage input on pin 0, converts it to equivalent voltage at the IMA battery, and prints the result to the Serial Monitor.
 
  Uses if statement to turn on the discharge circuit above xx volts and off below it.

  Dumps into a long, long delay cycle at the end of the cycle to prevent the Arduino clicking the relay back in when the resting voltage creeps back up. There is probably a much batter way of doing this...

  Uses pin 4 as an output to switch a relay on/off to switch the discharge resistors in and out.
 
  Recommend to add LEDs to show discharge in progress (use pin 4) and finished status LED on pin 5.

*/

// constants
const int DischargeOutput =  4;      // sets pin 4 for discharge output
const int FinishedLED =  6;      // sets pin 6 for 'finished' LED


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
    // initialize these as outputs:
  pinMode(DischargeOutput, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
 
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 200V):
  float voltage = sensorValue * (200.0 / 1023.0);
  // print out the values on the serial monitor:
  Serial.println("Voltage");
  Serial.println(voltage);
 
  // if the voltage value is high enough, turn on the discharge output. Change this value to suit:
  if (voltage > 144) {
    digitalWrite(DischargeOutput, HIGH);
    digitalWrite(FinishedLED, LOW);
  } else {
    digitalWrite(DischargeOutput, LOW);
    digitalWrite(FinishedLED, HIGH);
    Serial.println("Discharge off");
    Serial.println(" ");
    // now stop the Arduino clicking back in if the resting voltage creeps up:
    endDelay();
  } 
 
  delay(1000);
}

// This is a very, very, very dirty way of stopping the Arduino clicking in and out as the resting voltage creeps back up.
void endDelay() {
  Serial.println("End of discharge");
  delay(999999999);
  delay(999999999);
}
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #3 (Edited)
This is the basic code to get the 3-preset discharger to work. Same potential divider as above. You'll want to add in some status LEDs as above too. Steal it from the code above, though I'll come back and edit it in later when I get a chance.

Code:
/*
  switchCase IMA discharger with 3 preset discharge voltages

  Code is based on the switch statement. The switch statement allows you
  to choose from among a set of discrete values of a variable. It's like a
  series of if statements.

  The circuit:
  - 3 resistor array between 0V and +5V (0V, 2.2k, 10k, 10k, 2.2k, +5V works). 3 position selector switch to analog in 1, switching between the centre point and either side of the 10k resistors
  - 100k trimming potentiometer goes across the IMA battery, set to 2500R on the 0V side and 97.5k on the +ve side. On a 0-200V scale, this creates a 0-5V range on the potential divider that the Arduino can read. Arduino pin A0 goes between 0V and the 2500R tap.


  Original SwitchCase code by Tom Igoe and is in the public domain:

  http://www.arduino.cc/en/Tutorial/SwitchCase
*/

// these constants won't change. They are the lowest and highest readings you
// get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 900;    // sensor maximum, discovered through experiment

const int DischargeOutput =  4;      // pin number for discharge output

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(DischargeOutput, OUTPUT);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A1);
  // map the sensor range to a range of three options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 2);

  // select discharge voltage depending on the range value:
  switch (range) {
    case 0:    // switch position is at 2.2k end of array, reads approx 0.5V
      Serial.println("Discharge to 140V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 200V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value you read:
      Serial.println("Voltage reading:");
      Serial.println(voltage);  
      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 140) {
    digitalWrite(DischargeOutput, HIGH);
    Serial.println("Discharge on");
    Serial.println(" ");
  } else {
    digitalWrite(DischargeOutput, LOW);
    Serial.println("Discharge off");
    Serial.println(" ");
    endDelay();
  }
      }  
      delay(1000);
      break;

    case 1:    // switch position is at middle of array, reads approx 2.8V
      Serial.println("Discharge to 120V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 200V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value you read:
      Serial.println("Voltage reading:");
      Serial.println(voltage);
      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 120) {
    digitalWrite(DischargeOutput, HIGH);
    Serial.println("Discharge on");
    Serial.println(" ");
  } else {
    digitalWrite(DischargeOutput, LOW);
    Serial.println("Discharge off");
    Serial.println(" ");
    endDelay();
  }
      }
      delay(1000);
      break;

    case 2:    // switch position is at 5V end of array
      Serial.println("Discharge to 100V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value you read:
      Serial.println("Voltage reading:");
      Serial.println(voltage);
      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 100) {
    digitalWrite(DischargeOutput, HIGH);
    Serial.println("Discharge on");
    Serial.println(" ");
  } else {
    digitalWrite(DischargeOutput, LOW);
    Serial.println("Discharge off");
    Serial.println(" ");
    endDelay();
  }
      }  
      delay(1000);
      break;

  }
  delay(1);        // delay in between reads for stability
}

void endDelay() {
  Serial.println("End of discharge");
  delay(999999999);
  delay(999999999);
}
 

·
Registered
Joined
·
18 Posts
Very interested! Ive dabbled in arduino and coding for another car of mine that turned out quite well. You say an arduino can't activate a relay but, as you know, there are an abundance of electrical component options. What voltage do you need to flow through the relay thats controlled by the transistor and diode?
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #5
I've added the basic code in posts 2 and 3. Both work on the bench. I've used a 0-5V variable supply to fake the 0-5V that the Arduino is expecting to see across the potential divider. To stop the Arduino clicking the relay back in when the resting voltage creeps back up, I added a bit of a dirty way of dumping it out the loop. I added a void routine with a very long delay, so it just sits there counting hopefully for a sufficient time that you'll have plenty of time to disconnect it. There is probably a much better way of doing this, so please chip in if you know how.

Something I might want to add to the code is a more accurate voltage reading. Using a very precise Fluke meter, I already set the potential divider to show as close to 5.000V as I could with 20.00V across it. The Arduino makes its Analog voltage readings relative to the supply voltage, so it can be +/- 5% out if the supply voltage is +/- 0.25V. That's probably a bit more variation than we want with our discharges. Here is a way of working around it: Making accurate ADC readings on the Arduino

Very interested! Ive dabbled in arduino and coding for another car of mine that turned out quite well. You say an arduino can't activate a relay but, as you know, there are an abundance of electrical component options. What voltage do you need to flow through the relay thats controlled by the transistor and diode?
Which side of the relay do you mean? The contacts will need to be able to deal with at least 200V because the IMA battery might be anywhere up to around 190V. For simplicity, I was initially planning to use a 5V relay. If I can't find a 5V relay with contacts rated at 200V or more, I'll use one of the 12V rails on the PSU because I know you can get 12V relays rated that high.
 

·
Registered
Joined
·
458 Posts
Question: Could this rig be modified to run outside of the pack, say with a connector already hooked to the IMA for lightbulb discharge?
 

·
Registered
Joined
·
18 Posts
For simplicity, I was initially planning to use a 5V relay.
Thats what I was getting at. Not sure what part of the system you were going to switch but i understand now. I know it may make it somewhat more complex but you could use a 5v to control a 12v just so the arduino is doing all the work, but then again youd need a 12v supply and diode for the 12v relay.

I saw your delay 99999 code :p That works but ive found long delays sometimes create other odd loops or other parts of the code to be active sometimes. Im not incredibly well versed in any programming language but in one of my projects for another car I needed an indefinite delay like youve done and found this to be the solution to my problems.

Put this code before everything, even before the void setup()

Code:
void stopSketch(void)
    {
     noInterrupts();
     while(1) {}
    }
then whenever you want stop the code indefinitely just insert

Code:
stopSketch();
I have a "fan module" i made out of a nano that I needed to analog.read a coolant temp sensor. Once the temp sensor reached its target voltage it would activate the fans and deactivate the sensing circuit. At the end of the fan activation, and sensing circuit deactivation i put stopSketch(); and it kept the fans activated and sensing circuit deactivated indefinitely until a RESET was performed or the Power was removed to cycle the nano. It has worked flawlessly since using that code.

Hope that helps! Still very anxious to see your results. I just bought my first Insight this last weekend so unfortunately i wont be able to participate in your experiment until im more familiarized with the cars systems. Although i will be watching the thread closely!
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #8
Question: Could this rig be modified to run outside of the pack, say with a connector already hooked to the IMA for lightbulb discharge?
Yes, absolutely. In fact, it would probably make more sense to do it your way if you already built a lightbulb discharger. If you had a pair of plugs you'd plug one side of the Arduino discharger in to the battery and the other end to the lightbulb discharger. Omit the resistors and just use the Arduino box as a controller to shut it off when discharged.

Put this code before everything, even before the void setup()

[snip]

Hope that helps!
Yeah, that's loads of help - thanks!
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #9
I had a chance to look into the relay situation at lunchtime. Mechanical relays don't like DC and are generally rated around 20-30 VDC. So there are two easier ways I can think of to tackle this: solid state relay or MOSFET switch.

Solid state relays are dead cheap from China, about £3.50 each. The dealbreaker with those is the shipping time and I need mine before the end of December, ideally by the end of this week so I can tinker. I've gone for a couple of pre-made modules from a UK seller on eBay which are a bit more expensive, but it means I can have them by the end of the week. They have a flywheel diode onboard and screwdown terminals ready to use. They will also make it much easier for a DIYer to see how this is put together.

88318


High voltage N-channel MOSFETS look pretty easy to set up e.g. IRF712. In the diagram below, VDD would be the IMA battery +ve, RL would be the discharge resistor and Vin would be the +5V output trigger from the Arduino. Downside is lack of isolation if it goes pop and if I wanted to add a charge cycle later, I haven't quite worked out how to use a MOSFET to switch it in from my dumb charger. It's also not easy to test the circuit without a dummy supply and load, because that's how MOSFETS work.

88319
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #10
I did a little bit of experimenting as the switch position for the 3-preset version wasn't working consistently at the 100V end. It looks like the switchCase array isn't always happy if it's at the extremes of 0V or 5V. To work around this, I put a 2.2k resistor at both ends and changed the array range from 0-600 to 0-900. Now it works reliably. Have updated the code in post 3 to reflect this.

I've tested the stopSketch code that JuicyBait suggested and it works perfectly. I've got it so that it lights a LED when discharge has finished, as a visual indicator that the discharge cycle is over. I'll add that to the next version I post up.

I am now working on adding a two-stage discharge, i.e. rapid discharge down to say 145V or so and then switching to a lower rate. This should speed up the discharge process significantly but not hurt the battery at lower volts where there might be voltage reversals.

Just waiting for the rest of the hardware to arrive now, then I'll be able to wire it up and test it for real.
 

·
Registered
Joined
·
337 Posts
I modified my "Simple Cycler" similar to what you have proposed to control with an Arduino. Here's a schematic of how I kept the HV battery isolated. It's called a "flying cap" because the HV battery is connected to a small capacitor via the voltage divider. When it's time to sample the voltage, the DPDT relay is energized and the leads of the capacitor are transferred to be read by the computer's ADC in:
88355


This circuit works well for tracking the HV pack voltage for charge/discharge purposes and keeping it all isolated. (The 100k voltage divider resistor load calculates out to 0.342 watts, so I used 1 watt 1% metal film resistors for the divider.) Sampling rate was once per minute.
Link: Simple Cycler Voltage Fix
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #12 (Edited)
^ Thanks for that. I didn't know there was already a PIC-based cycler and I must have missed your post about it.

My concern with my 100k voltage divider is the current going through it. Like you say, it's around 0.3W which I think will be pushing it a bit for a trimming pot, but going for a higher resistance might make the potential divider affected by having the Arduino attached. I'm going to experiment with a 200k pot but I think the Arduino will interfere with the reading once in the range of 500k or 1M. I guess if I put a 10k pot on a 20V bench supply I'll find out if it can take the current...

Edit: could have check this in advance. Trimming pot I'm using is rated to 0.5W according to datasheet. Link: https://www.bourns.com/pdfs/3296.pdf

Got a 10k version on the 20V supply, so 0.4W going through it and it's fine. Equivalent to putting 200V across the 100k pot.
 

·
Registered
Joined
·
337 Posts
FYI... Peter Perkins created the "Simple Cycler" several years ago, but I don't think he sells the boards any more. I'll soon be installing a new set of NiMh batteries in my car (2 packs in parallel) and will probably utilize an Arduino as well for discharge/charge control, so I'm interested in an Arduino solution as well. For safety though, I think isolation is important.
Link: Simple Cycler
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #14
Ah, I misunderstood the role of the capacitor there. I saw it initially wired in parallel with the flywheel diode on the relay. Ok, I can see what it does now. That wouldn't be too difficult to incorporate into the code. It would need an extra output pin set for the relay. Then it would need to be initiated before the if statements in the loop, and switched off again. The loop could have a 1 minute delay put in each time so it does a single initiate/read/decision every minute rather than looping about every second as mine currently does, to save the relay cycles.
 

·
Administrator
Joined
·
12,445 Posts
FYI... Peter Perkins created the "Simple Cycler" several years ago, but I don't think he sells the boards any more. I
Link: Simple Cycler
I do have half a dozen Simple Cycler PCB boards left people can PM if interested.
Anyway back to this interesting project..
 

·
Registered
Joined
·
337 Posts
I am now working on adding a two-stage discharge, i.e. rapid discharge down to say 145V or so and then switching to a lower rate. This should speed up the discharge process significantly but not hurt the battery at lower volts where there might be voltage reversals.
I think the two stage discharge is the way to go, for the same reasons you suggest. (y)
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #17 (Edited)
I think it'll be as simple as inserting an else if statement in between the existing if and else statements where it reads the voltage. E.g. for a discharge to 120V: high rate discharge above 145V, low rate discharge below 145V, shut off below 120V

Code:
if (voltage > 145) {
  // enable both discharge outputs
}
else if (voltage >= 120) { // 120 <= voltage < 145
  // enable one discharge output
}
else { // voltage < 120
  // disable outputs
  // light the finished discharging LED
  // stopSketch
}
If that works, there's no real reason why it couldn't have a number of outputs with a fairly sizeable discharge load to quickly take it down to like 155V, then start incrementally dropping the load as the voltage goes down. The relay module I've got can handle 5A and they come with up to 8 on one board!

Edit: 500W J118 floodlight bulbs and ceramic holders look like a cost-effective way of doing that. However, that does mean shedding a fair bit more heat in a short space of time, which means you probably won't want it sat on the carpet in the back of the car!

Edit 2: or down your skinny grid charging cable. So don't do this!
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #19
I don’t really know how they work because I’m not that familiar with SSRs. I think the DC ones are MOSFET-based, however, so the two sides are insulated in as much as the device is a semiconductor and there is an insulating layer in a FET between the gate and the N or P channel. But there’s no physical air gap like there is in a mechanical relay.
 

·
Registered
Japanese 05 CVT
Joined
·
117 Posts
Discussion Starter #20 (Edited)
Latest version: switchCase IMA discharger V3.1, tested on the bench and working. Brief description:

This is a simple discharger with 3 voltage presets and two outputs, controlled by an Arduino.

As with previous versions, a 100k trimming potentiometer is put between the IMA battery 0V and +VE. The 0V end should be set to 2500 Ohms and the other side to 97.5k Ohms. This creates a 0-5V range across the 2500 Ohms for the Arduino to read on Analog In pin A0. In the code, this relates to a 0-200V range at the IMA end. Fixed resistor values will have the same effect. You may wish to isolate the input, as discussed above.

For the discharge voltage presets, a 4 resistor potential divider goes between 0V and +5V (0V, 2.2k, 10k, 10k, 2.2k +5V works). A 3-position switch is used, by switching between the centre point and either side of the 10k resistors. This goes to Analog In pin A1 on the Arduino.

There are two discharge outputs on pins 4 and 5. This enables the use of a high discharge rate above a particular voltage and a lower discharge rate below it. Above that voltage, both pins 4 and 5 are enabled (+5V). Below it, pin 4 is enabled (+5V) and pin 5 is disabled (0V). These outputs can directly power solid state relay modules or an N-channel MOSFET set as a switch. These are then attached to your discharge device of choice (resistors or light bulbs). The lower discharge load goes on pin 4 and the higher load goes on pin 5, but be mindful not to overload your grid charge cable or connector!

When the preset discharge voltage is reached, both outputs are disabled (0V). Pin 6 is enabled (+5V) for a notification LED and the code is ended. The cycle and outputs will not re-enable unless the reset button on the Arduino is pressed or the power switched off and on.

What you may wish to change:

I have set the discharge voltage presets to 140V, 120V and 100V. Feel free to change these values to your discharge voltages of choice. There's a lot of discussion on this forum about how deep to discharge, so set them to whatever you want.

I have set the code to enable both the discharge outputs when above 150V, then one output below. Again, feel free to change this value up or down. If you want to only use one output (e.g. if you wanted to simply hook up your existing lightbulb discharger), just use the pin 4 output. There is no check to see if anything is connected, so it won't throw an error.

Code:
/*
  switchCase IMA discharger V3.1

  Code is based on the switch statement. The switch statement allows you
  to choose from among a set of discrete values of a variable. It's like a
  series of if statements.

  The circuit:
  - 4 resistor array between 0V and +5V (0V, 2.2k, 10k, 10k, 2.2k +5V works). 3 position selector switch to analog in 1 switches between the centre point and either side of the 10k resistors
  - A 100k trimming potentiometer goes across the IMA battery, set to 2500R on the 0V side and 97.5k on the +ve side. On a 0-200V scale, this creates a 0-5V range on the potential divider that the Arduino can read. Arduino pin A0 goes between 0V and the 2500R tap.

  Changes from switchCase IMA discharger V2:
  1) Added a second output on pin 5 to allow use of a high rate discharge above a set voltage
  2) Added else if statements to the loop code to enable the second discharge output above a certain voltage
  3) Wrote voids for high/low discharge instead of using bulky code in the loop. V3.1 removed old code from loop for testing.

  Original SwitchCase code by Tom Igoe and is in the public domain:

  http://www.arduino.cc/en/Tutorial/SwitchCase
*/

// these constants won't change. They are the lowest and highest readings you
// get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 900;    // sensor maximum, discovered through experiment

const int DischargeOutput1 =  4;      // pin number for discharge output 1
const int DischargeOutput2 =  5;      // pin number for discharge output 2
const int FinishedLED =  6;      // pin number for finished discharging LED

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(DischargeOutput1, OUTPUT);
  pinMode(DischargeOutput2, OUTPUT);
  pinMode(FinishedLED, OUTPUT);
  digitalWrite(DischargeOutput1, LOW);
  digitalWrite(DischargeOutput2, LOW);
  digitalWrite(FinishedLED, LOW);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A1);
  // map the sensor range to a range of three options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 2);

  // select discharge voltage depending on the range value:
  switch (range) {
    case 0:    // switch position is between 2.2k and 10k at 0V end of array, reads approx 0.45V
      Serial.println("Discharge to 140V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0); 
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 200V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value:
      Serial.println("Voltage reading:");
      Serial.println(voltage);     

      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 150) {
    highDischarge();
  }
  else if (voltage > 140) {
    lowDischarge();
  }
  else {
    endDischarge();
    stopSketch();
  } 
      }     
      delay(1000);
      break;
   
    case 1:    // switch position is at middle of array, reads approx 2.5V
      Serial.println("Discharge to 120V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0); 
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 200V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value:
      Serial.println("Voltage reading:");
      Serial.println(voltage);
     
      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 150) {
    highDischarge();
  }
  else if (voltage > 120) {
    lowDischarge();
  }
  else {
    endDischarge();
    stopSketch();
  }
      }
      delay(1000);
      break;
   
    case 2:    // switch position is between 10k and 2.2k at 5V end of array, reads approx 4.55V
      Serial.println("Discharge to 100V");
      {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0); 
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
      float voltage = sensorValue * (200.0 / 1023.0);
      // print out the value:
      Serial.println("Voltage reading:");
      Serial.println(voltage);
     
      // if the voltage value is high enough, turn on the discharge output:
  if (voltage > 150) {
    highDischarge();
  }
  else if (voltage > 100) {
    lowDischarge();
  }
  else {
    endDischarge();
    stopSketch();
  }
      }     
      delay(1000);
      break;

  }
  delay(1);        // delay in between reads for stability
}

void highDischarge() {
    digitalWrite(DischargeOutput1, HIGH);
    digitalWrite(DischargeOutput2, HIGH);
    Serial.println("High rate discharge on");
    Serial.println(" ");
    }

void lowDischarge() {
    digitalWrite(DischargeOutput1, HIGH);
    digitalWrite(DischargeOutput2, LOW);
    Serial.println("Low rate discharge on");
    Serial.println(" "); 
    }

void endDischarge() {   
    digitalWrite(DischargeOutput1, LOW);
    digitalWrite(DischargeOutput2, LOW);
    Serial.println("Discharge off");
    Serial.println(" ");
    Serial.println("End of discharge");
    digitalWrite(FinishedLED, HIGH);
    delay(1000);
    }

void stopSketch(void) {
     // this is used to stop the code indefinitely after discharging
     noInterrupts();
     while(1) {}
    }
 
1 - 20 of 42 Posts
Top