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() {
}
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.
As a foundation for embedded systems
setup()
is equivalent)loop()
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
setup()
So what we need to do is
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 oneSo I would like to suggest an improvement.
© 2024 OneMinuteCode. All rights reserved.