Last active
March 13, 2016 06:21
-
-
Save dwblair/f736a07d9d30e5bc0b43 to your computer and use it in GitHub Desktop.
turning a pin on and off at a given frequency, and measuring the current while it's on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TimerOne.h> | |
// This example uses the timer interrupt to blink an LED | |
// and also demonstrates how to share a variable between | |
// the interrupt and the main program. | |
float vcc = 5; // volts | |
float vground = 2.51; // volts -- the virtual ground value | |
const int led = 3; // the pin with a LED | |
float r1 = 3190; // the current-voltage resistance --> amplification | |
void setup(void) | |
{ | |
pinMode(led, OUTPUT); | |
Timer1.initialize(500); | |
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds | |
Serial.begin(9600); | |
} | |
// The interrupt will blink the LED, and keep | |
// track of how many times it has blinked. | |
int ledState = LOW; | |
volatile int flag = 0; //whether we're in the 'on' state | |
void blinkLED(void) | |
{ | |
if (ledState == LOW) { | |
ledState = HIGH; | |
flag = 1; | |
//blinkCount = blinkCount + 1; // increase when LED turns on | |
} else { | |
ledState = LOW; | |
} | |
digitalWrite(led, ledState); | |
} | |
// The main program will print the blink count | |
// to the Arduino Serial Monitor | |
void loop(void) | |
{ | |
noInterrupts(); | |
flag = 0; // write to the flag without conflicting with ISR writes | |
interrupts(); | |
while (flag==0) {}; // loop | |
// if get here, then flag=1, and we're in the 'on' state | |
delayMicroseconds(50); // try to hit the middle of the squarewave (which will be a sinusoid via water), but also give analogRead some time; | |
// note that this delay function works well between 3 and 16383 (uS) | |
// we might need to come up with a better method later | |
// we should put current into water and measure the sinusoidal output to test this out | |
// do that, and take photos, tonight | |
// then see if you can take an analog reading in the middle of the sinusoid | |
// based on where the peak on the scope is at | |
// just a test to see if we can independently use pin 9, which might conflict with timer 1 -- not a definitive test | |
// read the input on analog pin 0: | |
int sensorValue = analogRead(A0); | |
// show that we're done reading | |
/* | |
digitalWrite(12, HIGH); | |
delayMicroseconds(50); | |
digitalWrite(12, LOW); | |
*/ | |
//float r1 = 250000; // kohms -- the resistor used in the first op amp transimpedance amplifier current -> voltage converter | |
float v1 = (float(sensorValue)/1024)*vcc; // volts out from the current measurement | |
float dv = v1-vground; | |
float c1 = float(dv)/float(r1)*1000; // current, in milliamps | |
float ca = float(v1)/float(r1)*1000; // current, in milliamps | |
// print out the value you read: | |
Serial.print("bits="); | |
Serial.println(sensorValue); | |
Serial.print("volts="); | |
Serial.println(v1,5); | |
Serial.print("dv (volts) ="); | |
Serial.println(dv,5); | |
Serial.print("current (mA) ="); | |
Serial.println(c1,5); | |
//Serial.print("AC current (mA) ="); | |
//Serial.println(ca,5); | |
Serial.println("\n\n\n\n"); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment