Ok so basically I put together this code, Using serial comm the code executes and the hardware functions well with the software, however when I hook up a Battery (in this case a 9V battery to 5V buck converter, connected to 5V and GND on a teensy 4.1) the code does not execute and Servos dont go off / data isnt saved. not to sure what im doing wrong here. any help appreciated
#include "Wire.h"
#include <MPU6050_light.h>
#include "HCPCA9685.h"
#include <PID_v1.h>
#include <SD.h>
#include <SPI.h>
#define I2CAdd 0x40
// PID Controller definitions
double Setpoint ; // will be the desired servo output value
double Input; // GYRO
double Output ; //servo actual output
// select sd card on board
const int chipSelect = BUILTIN_SDCARD;
//PID parameters
double Kp = 4, Ki =.1 , Kd = .1;
unsigned long clocktime;
//create PID instance
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
MPU6050 mpu(Wire);
unsigned long timer = 0;
HCPCA9685 HCPCA9685(I2CAdd);
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card...");//SD card check
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1) {
// No SD card, so don't do anything more - stay stuck here
}
}
Serial.println("card initialized.");
//setup for mpu
Serial.begin(115200);
Wire.begin();
byte status = mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(status);
while (status != 0) { } // stop everything if could not connect to MPU6050
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
mpu.calcOffsets(); // gyro and accelero
Serial.println("Done!\n");
/* Initialise the library and set it to 'servo mode' */
HCPCA9685.Init(SERVO_MODE);
/* Wake the device up */
HCPCA9685.Sleep(false);
Serial.begin(115200);
//Hardcode the gyro value
Setpoint = 0;
//Turn the PID on
myPID.SetMode(AUTOMATIC);
//Adjust PID values
myPID.SetTunings(Kp, Ki, Kd);
}
void loop() {
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
mpu.update();
char result[4]; // Buffer big enough for 7-character float
if ((millis() - timer) > 10) { // print data every 10ms
Serial.write("X : ");
Serial.write (dtostrf(mpu.getAngleX(), 6, 2, result));
Serial.write("\tY : ");
Serial.write(dtostrf(mpu.getAngleY(), 6, 2, result));
Serial.write("\tZ : ");
Serial.write(dtostrf(mpu.getAngleZ(), 6, 2, result));
Serial.println();
timer = millis();
}
//Read the value from the GYRO
Input =(mpu.getAngleZ());
//PID calculation
myPID.Compute();
//Write the output as calculated by the PID function
HCPCA9685.Servo(0, (Output*.2)+250);
HCPCA9685.Servo(1, (Output*.2)+250);
Serial.print(Input);
Serial.print(",");
Serial.print(Output);
Serial.print(",");
Serial.print(Setpoint);
Serial.print(((Output*.2)));
}
// open the file.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(mpu.getAngleZ());
dataFile.print(",");
dataFile.print(mpu.getAngleX());
dataFile.print(",");
dataFile.print(mpu.getAngleY());
dataFile.print(",");
dataFile.println(millis());
dataFile.close();
} else {
// if the file isn't open, pop up an error:
Serial.println("error opening datalog.txt");
}
delay(5);
}}