Skip to content

Added concept image and code #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added prototype/baculus backpack.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions prototype/logCurrent/logCurrent.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

float sensorValue[4]; // value read from the pot
// A0: ACS7812 Sensor, A1-A4: USB voltage inputs, A5: Raspberry Pi voltage input
float sensorPin[4] = {A1, A2, A3, A4};
int num = 100;

void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");

//we are using Adafruit Adalogger M0 wirh a SAM D21 board.
//It has a ADC resolution of 12 bits. Use the highest available resolution
analogReadResolution(12);
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

getData(dataString);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

// changes dataString to have data in the following format:
// num1, num2, num3, num4, num5
// num1: time elapsed in milli seconds
// num2-num5: current through corresponding USB port in milliAmps

void getData(String &dataString) {
dataString = String(millis());
dataString += ", ";

// zero all readings
for (int i = 0; i < 4; i++) {
sensorValue[i] = 0;
}

// read and average analog inputs
for (int j = 0; j < num; j++) {
for (int i = 0; i<4; i++) {
sensorValue[i] += analogRead(sensorPin[i]) * 3300 / 4096;
//wait for ADC to stabilise
delay(2);
}
}
for (int i = 0; i < 4; i++) {
sensorValue[i] /= num;
}

// add data to string
for (int i = 0; i < 3; i++) {
dataString += String(sensorValue[i]);
dataString += ", ";
}
dataString += String(sensorValue[3]);

// Serial.println(dataString);
}