About Arduino and SD Cards

Asked 2 years ago, Updated 2 years ago, 110 views

Currently, the sample.txt in the SD card has a number 1,3,5,6,17... for each line, which is substituted by val as the counter value.
When val=TimeDivCounter, I would like to run inside the switch and rotate the motor, but there are some problems with the code below, and I don't know how to solve them, so I would appreciate it if you could let me know.

What do you want to do
I want to get a value from the SD card one line at a time and replace it with val to rotate the motor.

Question
The interrupt is now running at 1ms, but will it be in time to read from the SD card?

Current state code

https://writening.net/page?VJ47Xp

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

int APHASE=2;
int AENBL = 3;
int BPHASE = 6;
int BENBL = 7;
volatile int count = 0;
volatile int RotCounter = 0;
volatile int TimeDivCounter = 0;
volatile int database = 0;
volatile intval;

US>Read to ISR (TIMER1_COMPA_vect) {//1ms(0.001s)
    // 18 degree rotation processing
    If(TimeDivCounter==val){//== followed by SD value
        switch(RotCounter){
            case0:
                digitalWrite (APHASE, LOW);
                digitalWrite (AENBL, HIGH);
                digitalWrite (BPHASE, LOW);
                digitalWrite (BENBL, HIGH);
                break;
            case1:
                digitalWrite (APHASE, HIGH);
                digitalWrite (AENBL, HIGH);
                digitalWrite (BPHASE, LOW);
                digitalWrite (BENBL, HIGH);
                break;
            case2:
                digitalWrite (APHASE, HIGH);
                digitalWrite (AENBL, HIGH);
                digitalWrite (BPHASE, HIGH);
                digitalWrite (BENBL, HIGH);
                break;
            case3:
                digitalWrite (APHASE, LOW);
                digitalWrite (AENBL, HIGH);
                digitalWrite (BPHASE, HIGH);
                digitalWrite (BENBL, HIGH);
                break;
        }
        RotCounter++;

        if(RotCounter>=4){
            RotCounter = 0;
        }
        TimeDivCounter = 0;
    } else {
        TimeDivCounter++;
    }
}

void setup() {
    pinMode (APHASE, OUTPUT);
    pinMode (AENBL, OUTPUT);
    pinMode (BPHASE, OUTPUT);
    pinMode (BENBL, OUTPUT);
    digitalWrite (AENBL, HIGH);
    digitalWrite (BENBL, HIGH);

    Serial.begin (9600);
    while(!Serial){
        ;
    }
    Serial.println("SD Card Initialization";

    if(!SD.begin(4)){
        Serial.println("Initialization Failed";
        while(1);
    }
    Serial.println("Initialization Complete";

    // Open file, take data every 1s
    if(dataccount==1000){
        File dataFile=SD.open("sample.txt");
        if(dataFile){
            while(dataFile.available()){
                val=dataFile.parseInt();
                Serial.println(val);
            }
            dataFile.close();
        } else {
            Serial.println(F("error opening sample.txt"));
        }

        TCCR1A = 0b00000010;
        TCCR1B = 0b00000011;
        TIMSK1 = 0b00000010;
        OCR1A = 249;
    }

    void loop() {
    }

c arduino

2022-09-30 19:27

1 Answers

The variable dataccount is

at the beginning of the program.
 volatile int database = 0;

and the initial value of 0 only, but no other value is substituted for the dataccount, so the value remains 0.

Therefore, the IF statement condition (dataccount==1000) in loop will never be met and SD files will never be opened.

// Open the file, take data every 1s
if(dataccount==1000){
    File dataFile=SD.open("sample.txt");
    if(dataFile){

I think this is the reason why it doesn't work as expected.

Try increasing the value of the counter (variable datount) by interrupting every 1ms and modifying the program to read from SD when the value of the counter reaches 1000.


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.