JU Bat Blinker Development Docs: volt.c

Gadget Sourcecode: volt.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

//#define RESISTOR  33.0
//#define RESISTOR  75.0
//#define RESISTOR  1000.0
#define RESISTOR    180.0
int bad = 0;
int lcnt = 1;
int icnt = 1;

// led current in A
double led_current(double led_v)
{
#if 0
    if (led_v < 1.9) {
        bad = 1;
        return 0.0/1000.0;  // not quite true
    }
#endif
    double i = ((50.0 * (led_v - 2.0)) + 10) / 1000.0;
    if (i<0.0) return 0.0f;
    return i;
}

void volts(double v, double r)
{
    double vr = v/2.0;
    double dv = vr;
    double ir, il, vl, x, vc;
    int nl,nh;
    
    bad = 0;
    while(1) {
        ir = vr/r;
        vl = (v-vr)/icnt;
        il = led_current(vl);
        if (dv < 0.0001) break;
        dv *= 0.5;
        if (il > ir) {
            vr += dv;
        } else {
            vr -= dv;
        }
    }


    x = vl/v;

#if 1
    vc = vl * lcnt;
#else
    vc = vr;
#endif

    nl = 24.0 * vc / v;
    nh = (32.0/v) * (vc - (v/4.0));
    if (nl <  0) nl = 0;
    if (nl > 14) nl = 14;
    if (nh <  0) nh = 0;
    if (nh > 14) nh = 14;

    printf("  %8.3fv  %8.3fv  %8.3fv  %8.3fA  %8.3f  %2d %8.3fv %8.3fv  %2d %8.3fv %8.3fv\n",
        v,vl,vr,il,x,
        nl,
        (nl*v/24.0)/lcnt,
        ((nl+1)*v/24.0)/lcnt,
        nh,
        (v/4.0 + nh*v/32.0)/lcnt,
        (v/4.0 + (nh+1)*v/32.0)/lcnt);
    
}

int main()
{
    double v;

    printf("  %8s   %8s   %8s   %8s   %8s  %2s %8s  %8s   %2s %8s  %8s \n",
        "Vbatt",
        "Vled",
        "Vr",
        "Iled",
        "VRatio",
        "nl",
        "Vnl",
        "Vnl+1",
        "nh",
        "Vnh",
        "Vnh+1");

    for (v=6.5; v>4.0; v -= 0.1) {
        volts(v,RESISTOR);
    }
    return 0;
}

This file Copyright (C) 2004 by Nathan (Acorn) Pooley
Go to Bat Blinker Development page
Go to Bat Blinker page
Go to JU Gadgets page
Go to Justice Unlimited homepage
Go to Acorn's personal webpage
Contact Acorn
See comments from others
Post your own comments
File created by do_doc at Wed Aug 4 20:17:36 2004