Go Down

Topic: Saving to Adafruit SPI FRAM causes full system malfunction (Read 583 times) previous topic - next topic

caylec

I will preface this by saying that this is the first time I have ever tried to code anything in my life and have learned while working on this plus with some guidance from a friend who is way better at this than I am. The one issue currently stopping me from finishing my project is the ability to save more than one Byte to FRAM.

---------------------------------------------------------
The following is in Adafruit_FRAM_SPI.h:
  void write8(uint32_t addr, uint8_t value);
  void write(uint32_t addr, uint8_t *values, size_t count);
  uint8_t read8(uint32_t addr);
  void read(uint32_t addr, uint8_t *values, size_t count);
---------------------------------------------------------

How should I be laying out the fram.write to save a long to start at address 5 for "i"?

Side note, if anyone is familiar with the DFPlayer Mini, can it be set to play a track in its entirety without being interrupted without using a Delay()?


#include <SPIMemory.h>
#include <Adafruit_FRAM_SPI.h>
#include <LiquidCrystal.h>  // access LCD Control file
#include "Arduino.h" // access command file
#include <SPI.h>
#define CHIP_SIZE64
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(18,9); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

const int rs = 14, en = 15, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //LCD pin structure
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //LCD pin Structure
const int pinLaser = 6; //defines laser pin
const int pinReceiver = 7; //defines laser receiver pin
int pushButton = 8; // defines pinout for button
long i; // defines number currently in bin
long t; // defines the total
int screen_page = 1; // Defines current screen page
int screen1; // Screen number 1
int screen2; // Screen number 2
int offset;
size_t var=4;

int SPIFram;
uint8_t FRAM_CS = 10;
uint8_t FRAM_SCK= 13;
uint8_t FRAM_MISO = 12;
uint8_t FRAM_MOSI = 11;
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS);
uint16_t          addr = 0;
uint8_t           addrSizeInBytes = 4;
uint32_t          memSize;
boolean fastRead= false;

long buttonTimer = 0; // defines the button timer and the starting value of the button timer
long longPressTime = 2500; // defines what the long button press value is
int emptyBin;
int sound;
int soundPlay;
int insultOrClip;

boolean buttonActive = false; //Either/or value for the buttons current state
boolean longPressActive = false; // Either/or value for the long press of the button
boolean buttonState = false; // Sets Button State for toggling screen
boolean lastButtonState = false; // Sets formula for last button state to be compared to buttonState
boolean laserState = HIGH;
boolean lastLaserState = HIGH;


void setup() {
  fram.begin();
  pinMode(pinLaser, OUTPUT);
  pinMode(pinReceiver, INPUT);
  pinMode (pushButton, INPUT);
  Serial.begin(9600);
    mySoftwareSerial.begin(9600);
    myDFPlayer.begin(mySoftwareSerial);
      myDFPlayer.volume(28);  //Set volume value. From 0 to 30
  Serial.println("Laser Module Test");
  digitalWrite(pinLaser, HIGH);
  digitalRead(pushButton == HIGH);
  i = fram.read8 (0x5);
   randomSeed (i);
 emptyBin = fram.read8 (0x20);
 sound = fram.read8 (0x32);
 soundPlay = fram.read8 (0x33);
        lcd.begin (16, 2);
}

void loop() {
  int receiverHigh = digitalRead(pinReceiver);
  long buttonTimer = millis();
  buttonState = digitalRead(pushButton);

  if (screen_page == 1) {
    lcd.setCursor(0, 0);
    lcd.print ("Number in bin") ;
    lcd.setCursor (0, 1);
    lcd.print(i, DEC);
  }
  if (screen_page == 2) {
    lcd.setCursor(0, 0);
    lcd.print ("Total number") ;
    lcd.setCursor (0, 1);
    lcd.print(t, DEC);
  }

  while (digitalRead (pushButton) == HIGH) {
    if (buttonActive == false) {
      buttonActive = true;
      buttonTimer = millis ();
    }
    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false) && (i > 0)) {
      myDFPlayer.playFolder(1,2);  //Play the second mp3
      delay (1000);
      longPressActive = true;
      i = 0;
    fram.writeEnable(true);
    fram.write8(0x5,i);
    fram.writeEnable(false);
            lcd.clear ();
      screen_page = 1;

      if ((longPressActive == true) && (buttonActive == true)) {
        buttonActive = false;
        longPressActive = false;
      }

    } else((buttonState != lastButtonState) && (longPressActive == false) && (buttonActive == true)); {
      delay (150);
      if (buttonState == true){
      lcd.clear () ;
      (screen_page = screen_page + 1);
      if (screen_page > 2) {
        lcd.clear ();
        screen_page = 1;
        delay (300);
      }
      }
    }
  }



  if (digitalRead (pushButton) == HIGH)
  {
    Serial.println("BUTTON!");
  } else {
    Serial.println("No BUTTON");
  }

  if (receiverHigh == HIGH)
  { lastLaserState = HIGH;
  laserState = HIGH;
    Serial.println("Detected!");
  } else {
    laserState = LOW;
    if ((laserState !=lastLaserState)&&(receiverHigh==LOW)){
    Serial.println("No laser");
     myDFPlayer.playFolder(1,1);
     delay (400);
    i = (i + 1);
    t = (t + 1);
    sound = (sound + 1);
    fram.writeEnable(true);
    fram.write8(0x5,i);
    fram.writeEnable(false);
    fram.writeEnable(true);
    fram.write (1010,*&t,4);
    fram.writeEnable(false);
    fram.writeEnable(true);
    fram.write8(0x32,sound);
    fram.writeEnable(false);
    delay(100);
    laserState = LOW;
    lastLaserState = LOW;
    }
  }
 
}

wildbill

I expect this will do it:
Code: [Select]

fram.write(5,(uint8_t*)&i,sizeof i);

caylec

Hey,

Sorry, I totally wrote the wrong thing. I should have asked what should I put to read it with?

fram.read(5,(uint8_t*)&i,sizeof i);

That did work...

THANK YOU SO MUCH!!

We had been trying different variations such as:
fram.read(5,(uint8_t*)*&i,sizeof i);
fram.read(5,(uint8_t*)&i,sizeof 4); // and defining the size of value

plus various other ones.

So, Thanks again!

Go Up