The aim of my project is to turn on my 2 servos for 5 seconds in car with keypad (eg. clicking '1' or '2') and a small delay e.g. 3 seconds until it starts driving. Then it will brake on its own. After clicking keypad I also want to write acceleration on microSD card.
I have this code for SD module, Grove ADXL335 analog accelerometer and servo object and it can measure acceleration, print it in the serial monitor and SD card but I was not able to use keypad properly (remove code from loop). Could you advise me something that would work?
#include <Keypad.h> //biblioteka od klawiatury
const byte ROWS = 4; // ile wierszy
const byte COLS = 4; //ile kolumn
byte rowPins[ROWS] = {2, 3, 4, 5}; //piny wierszy - lewy skrajny przewod podlaczony do pinu nr 2
byte colPins[COLS] = {6, 7, 8, 9}; //piny kolumn - 5 przewod od lewej strony podlaczony do pinu nr 6
char keys[ROWS][COLS] = { //mapowanie klawiatury
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad klawiatura = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //inicjalizacja klawiatury
String receivedData = "";
#include <Servo.h>
Servo servo1;
Servo servo2;
unsigned long Time ;
int xA;
int yA;
int zA;
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 53;
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
servo1.attach(9);
servo2.attach(10);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println("Testing");
myFile.close(); // close the file
Serial.println("Done");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
}
void loop() {
Time = millis();
servo1.write(0);
servo2.write(0);
if (millis() >= 0 && millis() <= 5000) {
servo1.write(0);
servo2.write(0);
delay(10);
}
if (millis() > 5000 && millis() <= 10000) {
servo1.write(90);
servo2.write(90);
delay(10);
}
if (millis() > 10000) {
servo1.write(0);
servo2.write(0);
delay(10);
}
myFile = SD.open("test.txt", FILE_WRITE);
xA = analogRead(A0);
yA = analogRead(A1);
zA = analogRead(A2);
Serial.print(xA);
Serial.print(",");
Serial.print(yA);
Serial.print(",");
Serial.print(zA);
Serial.println();
// if the file opened okay, write to it:
if (myFile) {
myFile.print(xA);
myFile.print(",");
myFile.print(yA);
myFile.print(",");
myFile.print(zA);
myFile.println();
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
delay(200);
}
ADXL335_SD_DualServoTiming.ino (2.42 KB)