Hello, I am currently working on a project that I dubbed the T-Rex Battery tester. It essentially uses a dual SG90 servo sweep to clamp down on a AA battery on the base with contact pads on the top and bottom when it senses the battery is in the holder using a photocell. Then I had my code set up to test the voltage of the battery using an analog input and have some RGB LEDs switch from red to green and also have the SD card play a small T-Rex roar if the battery was good according to if statement values in serial monitor. The problem is that now for some reason the SD card shows up as failing every time in Serial Monitor and this essentially voids most of my project. I used a multimeter and found something around 3.7 volts going to the module when I connected the positive lead of the multimeter to the 5v pin and ground on the module. I believe this is correct, but I still cannot figure out what the heck is going on!
Here is my code:
// Polygonasaurus Rex Battery Tester
// Created by Daniel Rudzik
// This code is Public Domain. Feel free to use it as you please! =)
#include <Servo.h> //This is the standard servo library
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch (new code)
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 90; // variable to store the servo position
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int BatteryPin = A5; // select the input pin for the potentiometer
int BatteryValue = 0; // variable to store the value coming from the sensor
unsigned long time = 0; //Used to help store number value
void setup()
{
digitalWrite(3, LOW); //Displays red light from the RGB Led
digitalWrite(2, HIGH); // Keeps the RGB green light off
servo1.attach(8); // attaches the servo on pin 8 to the servo object
servo2.attach(7); // attaches the servo on pin 7 to the servo object
pinMode(BatteryPin, OUTPUT);
pinMode(2, OUTPUT); //green is digital pin 2
pinMode(3, OUTPUT); //red is digital pin 3
Serial.begin(9600); //Allows values to be displayed in serial monitor
pinMode(9, OUTPUT);
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
if (!SD.begin(SD_ChipSelectPin )) { // see if the card is present and can be initialized:
Serial.println("SD fail"); //SD card not detected. Try taking the card out of the module and putting it back in.
return; // don't do anything more if not
}
else{
Serial.println("SD ok"); //The SD card was found to be in the Module
}
}
void loop()
{
BatteryValue = analogRead(BatteryPin); // read the value from the AA battery
Serial.println(BatteryValue); //Displays the Photocell sensor values in Serial Monitor
if (BatteryValue>100){ // Value is greater than 100 when the AA is connected and has power left in it.
tmrpcm.play("a.wav"); // Plays the T-Rex Roar when the value is greater than 100 and the SD card is ok.
digitalWrite(2, LOW); //Turns off the Red light from the RGB led
digitalWrite(3, HIGH); // Turns on the Green light from the RGB led
}
else{digitalWrite(3, LOW); // Turns on the Green light from the RGB led
digitalWrite(2, HIGH);} //Turns on the Red light from the RGB led
sensorValue = analogRead(sensorPin); //Detects the value on the Photocell to determine whether it is blocked from the light
if (sensorValue>700){ // When the Photocell value is greater than 700, it is being blocked
for(pos = 90; pos >= 19; pos --){
servo1.write(pos);
servo2.write(180-pos);
}
delay(5000);
for(pos = 19; pos <= 90; pos ++) // goes from 19 degrees to 90 degrees
{
servo1.write(pos); // tell servo to go to position in variable 'pos'
servo2.write(180-pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
Also attached is my current breadboard setup I made with Fritzing. Thanks!
