Author Topic: [newb] Am i doing this right? No :( Analog input -> control led (battery tester)  (Read 3333 times)

denpries

  • Newbie
  • *
  • Posts: 3
Im making a very (very) simple program for testing purposes.
With a multimeter i tested all output ports and all goed correctly! Also, the blink usb example worked great :)


Now, im trying to make the following: a battery tester!


Currently i have around 200 batteries in a box and i want to check if they are providing roughly 1 V or not. (as a test) (skip the fact that i have a multimeter, its the idea that counts)
So i connected a wire to ground on the digispark and a wire to the P2 of the digispark, and fired it up with the code below. However, the red light starts to blink arbitrarily
even when i dont connect the two wires to anything. If i connect the P2 wire to GND the red light stays off as it should. Digispark is connected to usb port of PC by the way.
Connecting one wire to PLUS of full battery and the other one to MIN does not give me any feedback, arbitrary blinking continues.



Code: [Select]

int sensorvalue = 0;
int eightbitsensorvalue=0;


void setup() {
    //All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V
    pinMode(0, OUTPUT);
   
    pinMode(2, INPUT);
}




void loop() {


   sensorvalue = analogRead(1); //reads in from pin 2
   eightbitsensorvalue = map(sensorvalue, 0, 1023, 0, 255);     
   if (eightbitsensorvalue>50) //should be roughly 1/5th of 5V is 1 V
   {
     digitalWrite(0,HIGH);
   }
   else
   {
     digitalWrite(0,LOW);
   }
}
« Last Edit: June 28, 2013, 03:01:33 am by denpries »

MichaelMeissner

  • Full Member
  • ***
  • Posts: 166
Code: [Select]

    pinMode(2, INPUT);

// ...
   sensorvalue = analogRead(1); //reads in from pin 2

Ummm, you setup pin 2 but you are reading from pin 1.  It is generally a good idea to use const int to give a more mnemonic name for the pins, and also allow you to easily change the pins in the future.  Also, in this simplified case, you don't need map, you could just check the value against the input before doing the map:

Code: [Select]
const int LED_PIN = 0;
const int VOLTAGE_PIN = 2;


int sensorvalue = 0;

void setup() {
    //All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V
    pinMode(LED_PIN, OUTPUT);
    pinMode(VOLTAGE_PIN, INPUT);
}

void loop() {
   sensorvalue = analogRead(VOLTAGE_PIN);        // reads voltage, from 0..1023
   if (eightbitsensorvalue > (1023/5))                         // test for >= ~1 volt
   {
     digitalWrite(LED_PIN,HIGH);
   }
   else
   {
     digitalWrite(LED_PIN,LOW);
   }
}

One other thing, if you are using the internal LED, be sure you know whether the LED is on pin 0 or pin 1.  If you had sparks from the original kickstarter run, and they were early enough in the production run, the LED was on pin 0.  Later versions of the sparks moved the LED to pin 1, so that it would not interfere with I2C (pins 0, 2).  People who ordered I2C shields or LCD shields (like me), were guaranteed to have Sparks with the LED on pin 1.

denpries

  • Newbie
  • *
  • Posts: 3
according to the manuals n stuff, analogread(1) reads from pin 2. However, it does not matter anymore. My spark died just now. Smokey. :|

semicolo

  • Full Member
  • ***
  • Posts: 137
Well I don't see any error in your code (I wouldn't convert the value to 8 bit and just test agains 200 instead of 50 but it doesn't really matter).

How did you fry your spark, did you connect a battery in reverse?