I'm beginning to code a TVC (thrust vector control) rocket, and I wrote some simple code that will write data to an SD card, plus some other simple things. This was working just fine, but after coming back to the project after a couple of days the code stopped working. I didn't change the code at all between these two times, so I don't really understand why it would. Normally what the code does is print out that the SD card has been successfully (or not successfully) initialized, and then prints out that it had success (or failure) opening the file on the SD card. This is not the case anymore, it will seemingly pass over that code without printing anything out. It will then print out some more things, but it will turn on the ignition without waiting for me to press the button. If anyone has any ideas about what is going on, it would be great if you could help me. Thanks!
Here is my code:
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
File sdCard;
Servo xAxis;
Servo yAxis;
int loopNumber;
int digital1;
double timer;
double startTimer;
const int greenLight = 24;
const int redLight = 32;
const int ejectionCharge = 4;
const int ignitionPin = 26;
const int launchPin = 22;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!SD.begin(43)) {
Serial.println("SD card initialization has failed!");
while (1==1){
digitalWrite(redLight,HIGH);
delay(500);
digitalWrite(redLight,LOW);
delay(500);
}
}
Serial.println("SD card initialization was a success");
//digitalWrite(greenLight,HIGH);
//delay(500);
//digitalWrite(greenLight,LOW);
//delay(500);
sdCard = SD.open("data.txt", FILE_WRITE);
if (sdCard){
sdCard.println("Success with opening the SD card!");
digitalWrite(greenLight,HIGH);
delay(500);
digitalWrite(greenLight,LOW);
delay(500);
}
else {
Serial.println("Error opening the file on the SD card");
while (1==1){
digitalWrite(redLight,HIGH);
delay(500);
digitalWrite(redLight,LOW);
delay(500);
}
}
digitalWrite(greenLight,HIGH);
xAxis.attach(5);
yAxis.attach(3);
xAxis.write(180);
delay(10);
xAxis.write(0);
delay(10);
xAxis.write(90);
yAxis.write(180);
delay(10);
yAxis.write(0);
delay(10);
yAxis.write(90);
delay(10);
digitalWrite(greenLight,LOW);
delay(100);
Serial.println("All servos tested, waiting for ignition signal.");
Serial.println("All systems go");
sdCard.println("All systems operational, rocket will launch shortly");
while (1==1){
digitalWrite(redLight,HIGH);
digitalWrite(greenLight,HIGH);
delay(15);
digital1 = digitalRead(launchPin);
if (digital1 == LOW){
digitalWrite(ignitionPin,HIGH);
digitalWrite(redLight,LOW);
digitalWrite(greenLight,LOW);
Serial.println("Ignition confirmed");
sdCard.println("Ignition confirmed.");
startTimer = millis();
break;
}
}
}
void loop() {
sdCard = SD.open("data.txt", FILE_WRITE);
digital1 = digitalRead(launchPin);
timer = (millis()-startTimer);
sdCard.print("Timer: "); sdCard.println(timer/1000);
sdCard.print("X Reading: "); sdCard.println("0");
sdCard.print("Y Reading: "); sdCard.println("0");
sdCard.print("Z Reading: "); sdCard.println("0");
Serial.println("0");
Serial.println("0");
}