I want to control the rotation of the motor based on the values read from the file.

Asked 2 years ago, Updated 2 years ago, 87 views

The file on the SD card contains values one line at a time, and I would like to rotate the motor by substituting the value for val in the interrupt function.
The file contains 1500, 2500, 500... and one line at a time, rotating the motor every 1.5s, 2.5s, 0.5s... when replaced by val.

However, the last number of 1500 in the file goes into val and rotates every 1.5s.

I would like to be able to turn the read code of the file written in setup once I put the value in val line by line.
Please let me know if there are any improvements in the sketch.
Thank you for your cooperation.

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

int APHASE=2;
int AENBL = 3;
int BPHASE = 6;
int BENBL = 7;
volatile int count = 0; // Counter for LED
volatile int RotCounter = 0; // Current rotation angle (0, 1, 2, 3)
volatile int TimeDivCounter = 0; // For interrupt counters
volatile int database = 0; // Counter for data
volatile intval;

US>Read to ISR (TIMER1_COMPA_vect) {//1ms(0.001s)
    // 18-degree rotation treatment
    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";

    File dataFile=SD.open("data.txt");
    if(dataFile){
        char buffer [65];

        while(dataFile.available()){
            int length = dataFile.available();

            if(length>64){
                length = 64;
            }

            dataFile.read (buffer, length);
            buffer [length] = '\0';
            Serial.write (buffer, length);
            val=atoi(buffer);
        }
        dataFile.close();
    } else {
        Serial.println(F("error opening data.txt"));
    }

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

void loop() {
}

c++ c arduino

2022-09-30 16:04

2 Answers

Since the setup function runs only once at the beginning, it is inappropriate to write "every time you value val one line at a time" in the setup function.

You can move the code "put a value in val and turn the motor for that amount of time" to the loop function that runs repeatedly.


2022-09-30 16:04

As a foundation for embedded systems

  • After only one initialization process (applied setup() is equivalent)
  • It never ends around the main loop loop()
  • Interrupt processing must be completed as soon as possible before it will fail.ISR()

The presentation program does not follow these rules, so it does not work as expected, and only the policy of how to fix it is presented

  • I want to use the contents of the file during the interrupt process
  • but reading files in interrupts is out of the question in terms of processing time
  • Preloading the contents of the file and using the data in memory within the interrupt
  • Function for pre-processing is setup()
  • By the way, memory is finite

So what we need to do is

  • Create a table that holds the number of seconds (the elements will be finite)
  • setup() reads the values and stores them in the table (requires the number of elements read: you need to develop specifications for what to do if there is a lot of file content)
  • ISR() uses the values of the table one by one
  • You'll need a separate indicator of how far you've used it (separate from the number of elements you've read)
  • You need to develop specifications to determine what to do when you run out of loads

So I would like to suggest an improvement.


2022-09-30 16:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.