Copying file bytes from SD Card to FRAM - arduino

I'm trying to copy a file from an SD card to an adafruit FRAM module. I'm wondering if I'm going about it the right way. I'm trying to read the file one byte at a time and then write that byte to a specific location on the Fram module.
I've been trying that approach using the sketch below and haven't been successful. I'm wondering if I'm approaching it the right way, and if so, where have I gone wrong with my sketch. Thanks.
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
uint16_t framAddr = 0;
void setup() {
Serial.begin(9600);
// setup SD-card
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println(" failed!");
while(true);
}
Serial.println(" done.");
}
void loop() {
uint16_t count = 0;
File myFile = SD.open("test.txt");
if (!myFile) {
// if the file didn't open, print an error and stop
Serial.println("error opening");
while (true);
}
const int S = 1;
byte buffer[S];
while (myFile.available()) {
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
Serial.print("0x"); Serial.print(count, HEX); Serial.print(": ");
Serial.println(buffer[count]);
//write fram (address,value)
fram.write8(count,buffer[count]);
}
myFile.close();
while (true) ;
}

I don't familiar with this FRAM, but you don't promote your address.
So the device write to the same address all the time and rewrite the memory.
Hope i help.
yoav

Related

Could not find a valid MPU6050 sensor

I am using the below code with arduino-uno, but often getting "Could not find a valid MPU6050 sensor
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while (!mpu.begin()) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop() {
}
My Arduino is working fine,
So, I checked MPU6050 using below code,
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Got Output as expected
Scanning...
I2C device found at address 0x68 !
done
From the above output I hope GPU6050 is working
How can I get values from GPU6050?
Your code is working as expected, it tells you in setup() when it can't connect to the device, until it can.
So when it stops printing the message, it is connected.
Now in loop() you should write your code to negotiate with the device.
Here's an excellent place to start with.

Arduino not saving to SD correctly

I've built simple project with my daughter that takes two temp readings and writes the to a file on a Micro SD card.
The code is fairly simple (if not a bit messy) and is creating the file, but only ever seems to write the same row of data twice (from the setup()) and then never writes again, although doesn't seem to produce an error.
The code is:
#include <Wire.h> //Required for Realtime Clock
#include "RTClib.h" //Required for Realtime Clock
#include <SPI.h> //Required for SD card reader
#include <SD.h> //Required for SD card reader
#include <dht11.h> //Required for Temp/humidity reader
#define DHTPIN 4 //Pin for temp sensor
#define LEDPIN 7 //Pin for led (status)
#define DHTTYPE DHT11 //define which temp sensor we are using (DHT11)
dht11 DHT11; //Create instance of DHT11 class to read sensor values
File myFile;
String filename;
uint32_t start_min; //Store time the current loop started so we can work out how long we've been running for
RTC_DS3231 RTC; //Create instance of real Time clock class
void setup() {
Serial.begin(9600); //start serial monitor for output
Wire.begin();
//begin the rtc, let us know if it fails
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
abort();
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
//set RTC time and date to the time the app was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
//Get the current RTC time and sore it as a unix time
DateTime now = RTC.now();
start_min = now.unixtime();
//Setup Filename
String filename = String(start_min);
int start = filename.length() - 8;
int end = filename.length();
filename = filename.substring(2,10) + ".csv";
Serial.println(filename);
//Write Header row
myFile = SD.open(filename, FILE_WRITE);
if(myFile) {
myFile.println("timestamp|humidity|temperature1|temperature2");
myFile.flush();
myFile.close();
delay(1000);
}
else {
Serial.println("Error opening file");
}
}
void loop() {
//Get current RTC date and time
DateTime now = RTC.now();
//Convert to unix time
uint32_t _now = now.unixtime();
// work out if x mins have passed
if((_now - start_min) > (4*60)) {
//Turn LED on
digitalWrite(LEDPIN,HIGH);
Serial.print("Looped - ");
Serial.print(now.hour());
Serial.print(":");
Serial.println(now.minute());
int chk = DHT11.read(DHTPIN);
myFile = SD.open(filename, FILE_WRITE);
if(myFile) {
String output = String(now.getTimeStr());
output += "|";
output += String((float)DHT11.humidity,2);
output += "|";
output += String((float)DHT11.temperature, 2);
output += "|";
output += String(RTC.getTemperature());
Serial.println(output);
myFile.println(output);
myFile.close();
Serial.println("Write complete");
}
else {
Serial.println("Error opening file");
}
//reset start time
start_min = now.unixtime();
}
delay(5000);
//Turn LED off
digitalWrite(LEDPIN,0);
//wait 90 secs
delay(5000);
}
I'm using an Arduino Uno, a DHT11 for temp/humidity and a DS3231 RTC (also grabbing temp there to).
Once compiled and uploaded. this is the output:
Sketch uses 16798 bytes (52%) of program storage space. Maximum is 32256 bytes.
Global variables use 1290 bytes (62%) of dynamic memory, leaving 758 bytes for local variables. Maximum is 2048 bytes.
any help appreciate

SPI Micro SD Card - error opening textfile - why isn't it working?

First of all sorry for my bad english and my programming skills... im still a beginner. I have a problem implementing the example code into my project. The example code for Datalogging on my SD-Card works. So there are no wiring faults.. Implementing this working code in my project, the arduino cant find the text data and i dont know why. can anybody help me ?
im working with an arduino nano V3. and a SPI Card Reader.
Here is what happening in the serial monitor:
Initializing SD card...card initialized.
error opening datalog.txt
Here is my Code - sorry for the used German words... but i think they wont disturb.
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // SD KARTE
int pinDHT22 = 2; // Kombisensor
SimpleDHT22 dht22(pinDHT22);
float temperature = 0;
float humidity = 0;
volatile float windgeschwindigkeit = 0;
unsigned long previousMillis = 0;
volatile int Impulscounter = 0; // Impulszähler für Windgeschwindigkeit
unsigned long windmillis = 0;
int a = 0;
File Datenlog;
void wind()
{
Impulscounter = Impulscounter + 1;
if( Impulscounter == 1)
{
windmillis = millis();
}
}
void setup()
{
pinMode(3, INPUT);
lcd.begin();
lcd.backlight();
Serial.begin(9600);
attachInterrupt(1, wind, RISING);
while (!Serial) { // wait for serial port to connect. Needed for native USB port only
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
Datenlog = SD.open("test.txt", FILE_WRITE);
if (Datenlog){
Datenlog.print("Tag "); // ... und die Textdatei anschließend befüllt werden.
Datenlog.print("Datum ");
Datenlog.print("Uhrzeit ");
Datenlog.print("Aussentemperatur ");
Datenlog.print("Aussenfeuchtigkeit ");
Datenlog.print("Windgeschwindigkeit ");
Datenlog.print("Gehaeusetemperatur ");
Datenlog.close();
Serial.print ( "it worked");
}
else {
Serial.println("error opening datalog.txt");
}
}

iBeacon name not displayed in ESP32 BLE scanner

I am working on a project that involves a BLE scanner using ESP32. I use the installed BLE scanner sample code in Arduino IDE to program the ESP32 but the device is unable to scan for the names of iBeacon just like any normal android BLE scanner application. I also tried to modify the code to specifically extract the names of iBeacons only but I still end up with the same result. Any suggestions are welcomed. Here is my modified code:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
//Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
Serial.print("Name :");
Serial.println(advertisedDevice.getName().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}

Read/Write EEPROM Arduino

I have a new ATmega328P CH340G Arduino Uno R3 board.
When I input a two-digit number (like 29), after power off and power on, the board shows only one digit (only 9). I want to show two digits.
enter image description here
Can you help me?
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <EEPROM.h>
int addr = 5;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
lcd.init();
Serial.begin(9600);
// initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.write(EEPROM.read(addr));
}
void loop() {
if (Serial.available()) {
while (Serial.available() > 0) {
char myValue = Serial.read();
EEPROM.write(addr,myValue);
lcd.write(myValue);
}
}
}
You are always writing to the same addr (i.e. 5) so you are most likely overwriting the previous character. Try incrementing your address after a write like this:
EEPROM.write(addr++, myValue);
(notice the ++ to increment the address)

Resources