Giter Club home page Giter Club logo

Comments (2)

superpeg00 avatar superpeg00 commented on August 29, 2024

In order to make things more clear this is the code, the error is in the last if-else cycle; on the serial monitor i recive "error opening Datalog.txt"

#include <CAN.h>
#include <SPI.h>
#include <SD.h>

void setup() {
//setup CAN

Serial.begin(9600);
while (!Serial);

//setup parte CAN

Serial.println("CAN Receiver");

// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
//while (1);
}
CAN.filter(0x518);

//setup SD
Serial.begin(9600);
while (!Serial) {
;
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(10)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");

}

void loop() {

int packetSize = CAN.parsePacket(), i=0;
float Vlamb=0, lamb=0;
Vlamb=analogRead(A0);

float pack[8];

//Serial.print("packet with id 0x");
//Serial.print(CAN.packetId(), HEX);
//Serial.print(" and length ");
//Serial.println(packetSize);
//Serial.println();

//salvo i valori del pacchetto ricevuto nella matrice pack
      while (CAN.available()) {
    //Serial.print(CAN.read(), HEX);
    pack[i]=CAN.read();
    i=i+1;    
  }
Serial.println(); 
//stampo sul seriale i valori contenuti nel pacchetto ricevuto
for(i=0;i<8;i++) {
  Serial.print(pack[i]);
} 
Serial.println();

float rpm = ((pack[1] * 256.0) + pack[0]) / 4.0; //creo una variabile rpm che contiene rpm

Serial.print("Engine RPM = ");
Serial.println(rpm);
Serial.print("lamb = ");
lamb=Vlamb/3;
Serial.println(lamb);

// inizializzo stringa per raccogliere i dati

String dataString = "";

//popolo la stringa

int sensor = analogRead(A0);
dataString += String(sensor);

File dataFile = SD.open("datalog.txt", FILE_WRITE);

// controllo sul file
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}

else {
Serial.println("error opening datalog.txt"); //here is it the error
}

delay(500);
}

from candrive.

adamtheone avatar adamtheone commented on August 29, 2024

Hello!
I'd suggest you to take a deep dive into SPI communication. You might need to change one or two things in the lower layers, but your setup (parallel MCP2515 and SDCard) is completely doable. You can just choose another pin as the SPI CS pin. That pin does nothing special, but goes low before the transmission, and goes high right after it. You can even control it yourself from the main logic. One thing to keep in mind: storing data to the SDCard will take some time, and meanwhile, you'll not be able to receive messages from the MCP2515 (because Arduino only has 1 SPI peripheral if I'm not mistaken).

from candrive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.