I have a text file of 1's and 0's stored on a MicroSD card.
When I read in one byte representing a 1 (hex49) or a 0 (hex 48)
I want to output a high or low using digitalWrite(). Basically bit
banging one I/O pin.
Any ideas?
I have a text file of 1's and 0's stored on a MicroSD card.
When I read in one byte representing a 1 (hex49) or a 0 (hex 48)
I want to output a high or low using digitalWrite(). Basically bit
banging one I/O pin.
Any ideas?
Can you read and print those characters as you drag them off the SD file?
Post that code, or what you have done so far with reading and reacting to what you have read.
a7
Hello zuelatak
Try, check and mod this sketch to your needs:
char TextFile[] {"0101010101010101010"};
void setup()
{
pinMode (LED_BUILTIN,OUTPUT);
for (unsigned int n=0; n<sizeof(TextFile); n++)
{
digitalWrite(LED_BUILTIN,TextFile[n]&1);
delay(1000); // for testing purpose only
}
}
void loop()
{
}
Have a nice day and enjoy coding in C++.
Thanks, I'll give it a try
Hi paulpaulson, I modified your code so that I could drive a 595 shift register and it works great, but I can't figure out why or how to eliminate the extra clock pulse that I'm getting at the end. It clocks in the last "0" then it outputs another clock before latch goes high. Thanks
char TextFile[]{ "01010101010101010101010101010101010101010101010101010101010101010" };
int dataPin = 25;
int clockPin = 17;
int latchPin = 16;
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, HIGH);
for (unsigned int n = 0; n < sizeof(TextFile); n++) {
digitalWrite(latchPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, TextFile[n] & 1);
digitalWrite(clockPin, HIGH);
}
digitalWrite(clockPin, LOW);
digitalWrite(latchPin, HIGH);
}
void loop() {
}
delete the "last" 0 from the textfile.
and take a view here addtional:
never mind
if I add a -1 the problem is solved
for (unsigned int n = 0; n < sizeof(TextFile)-1; n++) {
Thanks for the advice but I need to read in 65 "1''s and 0's" from a text file and output them via a output pin. Using ShiftOut() which does 8 bits at a time would require me to modify my file.
The file represents a Piano Roll that has 65 hole positions across the roll.
I've added a MicroSD initialization program with the shifting program but I'm not sure how to modify the digitalWrite(dataPin, TextFile[n] & 1); with File.read() so that I read in an Ascii 1 or 0 then output it as a high or low 65 times, latch the data, and repeat until end of file. Here is what I have so far.`
#include <SdFat.h>
SdFat sd;
SdFile File;
int dataPin = 25;
int clockPin = 17;
int latchPin = 16;
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, HIGH);
sd.begin(5, SD_SCK_MHZ(32)); //MicroSD card Chip Select Pin 5, set SPI to 32 Mhz (actually 25Mhz)
File.open("test.txt", O_READ ); //Open File to Read
while (File.available()) {
for (unsigned int n = 0; n < 65; n++) { // start of Shifting Data Out
digitalWrite(latchPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, TextFile[n] & 1); //File.read(); ????
digitalWrite(clockPin, HIGH);
}
digitalWrite(clockPin, LOW);
digitalWrite(latchPin, HIGH);
}
File.close();
}
void loop() {
}
Hello zuelatak
I guess the text file string is empty and I´m not familar how the sketch reads the text file from the SD card into the text file string.
I tried this and it gave an error.
digitalWrite(dataPin, char myFile.read());
I"ll keep on playing with it
Hello zuelatak
Make simply a secound playground to check how to read a text file from the SD-card to a string variable .
ok thanks.
If this helps with ideas this is the code that worked in Python but it ran too slow on the ESP32 so I'm trying to convert it over to C++
file = open("song.txt","r")
def shift_update(input,data,clock):
for line in input:
GPIO.wait_for_edge(latchIn, GPIO.RISING)
for i in range(64, -1, -1):
GPIO.output(clockOut,0)
GPIO.output(dataOut, int(line[i]))
GPIO.output(clockOut,1)
shift_update(file.readlines(),dataOut,clockOut)
Hello zuelatak
Take a view here to gain the knowledge:
ok thanks, I"ll check it out
ok I got it to work but the output results are not correct.
I changed/added these two lines
char letter = myFile.read();
digitalWrite(dataPin, (letter & 1));
The test.txt file contains this
The output looks like this
I get three bit steams, file only has two
also the first steam is correct 100000etc. 2nd one is wrong 101000000etc. third shouldn't happen.
The buffer letter is the problem I think.
I read this:
there is a Serial buffer, containing room for a fixed number of characters.
the bytes available in that buffer is decremented as chars are removed by receiver.
as characters are removed, they are destroyed (read()) . if more characters are coming from the sender, the sender will send another byte.
Not sure how to fix these two problems
Any Ideas?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.