i have got these two codes off the internet, one to load images from an sd card and another to use the barcode scanning module. This barcode scanning module displays the number from the barcode but not one i could use to load a file. So i had a friend help me convert this to something i could use. however it doesnt seem to work. could anyone please help?
/*
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins. For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_RST 9 // Reset line for TFT (or see below...)
#define TFT_DC 8 // Data/command line for TFT
#define SD_CS 4 // Chip select line for SD card
//Use this reset pin for the shield!
//#define TFT_RST 0 // you can also connect this to the Arduino reset!
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
int SCAN_ENTER = 0x5a; int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 3; // Clock is only output.
int dataPin = 2; // The data pin is bi-directional
// But at the moment we are only interested in receiving
int ledPin = 13; // When a SCAN_ENTER scancode is received the LED blink
int clockValue = 0; byte dataValue;
byte scanCodes[10] = {0x45,0x16,0x1e,0x26,0x25,0x2e,0x36,0x3d,0x3e,0x46}; char characters[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int quantityCodes = 10;
char buffer[64] = {}; // This saves the characters (for now only numbers)
int bufferPos = 0;
int bufferLength = 64;
char data[30];
String fileName;
int dataRead();
void setup() {
pinMode(dataPin, INPUT);
pinMode(clockPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.begin(9600);
// Use this initializer if you're using a 1.8" TFT
tft.initR(INITR_BLACKTAB);
// Use this initializer (uncomment) if you're using a 1.44" TFT
//tft.initR(INITR_144GREENTAB);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
return;
}
Serial.println("OK!");
// change the name here!
// wait 5 seconds
delay(5000);
}
void loop() {
dataValue = dataRead();
// If there is a break code, skip the next byte
if (dataValue == SCAN_BREAK) {
breakActive = 1;
}
// Translate the scan codes to numbers
// If there is a match, store it to the buffer
for (int i = 0; i < quantityCodes; i++) {
byte temp = scanCodes[i];
if(temp == dataValue){
if(!breakActive == 1){
buffer[bufferPos] = characters[i];
bufferPos++;
}
}
}
//Serial.print('*'); // Output an asterix for every byte
// Print the buffer if SCAN_ENTER is pressed.
if(dataValue == SCAN_ENTER){
// Serial.print("\nbuffer: ");
// Read the buffer
int i=0;
if (buffer[i] != 0) {
while(buffer[i] != 0) {
//Serial.print( buffer[i] );
data[i] = buffer[i];
buffer[i] = 0;
i++;
}
}
int stringSize = i;
for(i=0;i<stringSize;i++){
fileName += data[i];
}
Serial.print(fileName);
Serial.print("\n"); //debug
if(fileName == 92016401){
bmpDraw("92016401.bmp", 0, 0);
}
fileName = "";
// Serial.println(" [Enter]");
bufferPos = 0;
// Blink the LED
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
}
// Reset the SCAN_BREAK state if the byte was a normal one
if(dataValue != SCAN_BREAK){
breakActive = 0;
}
dataValue = 0;
}
}
sorry i couldnt fit all the code in. here is the rest.
int dataRead() {
byte val = 0;
// Skip start state and start bit
while (digitalRead(clockPin)); // Wait for LOW.
// clock is high when idle
while (!digitalRead(clockPin)); // Wait for HIGH.
while (digitalRead(clockPin)); // Wait for LOW.
for (int offset = 0; offset < 8; offset++) {
while (digitalRead(clockPin)); // Wait for LOW
val |= digitalRead(dataPin) << offset; // Add to byte
while (!digitalRead(clockPin)); // Wait for HIGH
}
// Skipping parity and stop bits down here.
while (digitalRead(clockPin)); // Wait for LOW.
while (!digitalRead(clockPin)); // Wait for HIGH.
while (digitalRead(clockPin)); // Wait for LOW.
while (!digitalRead(clockPin)); // Wait for HIGH.
return val;
// uncomment these lines to draw bitmaps in different locations/rotations!
/*
tft.fillScreen(ST7735_BLACK); // Clear display
for(uint8_t i=0; i<4; i++) // Draw 4 parrots
bmpDraw("parrot.bmp", tft.width() / 4 * i, tft.height() / 4 * i);
delay(1000);
tft.setRotation(tft.getRotation() + 1); // Inc rotation 90 degrees
*/
}
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates. It's sped up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel). Increasing the buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster. 20 pixels seems a
// good balance.
#define BUFFPIXEL 20
void bmpDraw(char *filename, uint8_t x, uint8_t y) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= tft.width()) || (y >= tft.height())) return;
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print("File not found");
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: "); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x+w-1, y+h-1);
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.pushColor(tft.Color565(r,g,b));
} // end pixel
} // end scanline
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
}
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
the filename is the converted buffer which i could then use to open the files. the error codes i get are; /var/folders/7l/020czf5j0sl_mpbs3mz21jzc0000gn/T/arduino_ac751f277a116302c5a5fc6b0f885f85/Casualty_08_12_15.ino: In function 'void loop()': Casualty_08_12_15:144: error: invalid conversion from 'long int' to 'const char*' [-fpermissive]
if(fileName == 92016401){*
^* In file included from /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:223:0,
ok thankyou for your help so far, im very new to this arduino so sorry for being slow. i want to use the buffer (which is the barcode number scanned) to then open a file with the same name. how can i do this?
ok thankyou for your help so far, im very new to this arduino so sorry for being slow. i want to use the buffer (which is the barcode number scanned) to then open a file with the same name. how can i do this?
Start by showing some sample output from the barcode scanner. Something like:
I think most of the code will work. I just need to convert the buffer to something I can use? Then I can go something like If scannedno==920164 Bmpdraw(920164.bmp); Understand what I'm getting at? What do I need to convert the buffer to?
I just need to convert the buffer to something I can use?
Why do your posts consistently switch to italics?
If you are putting [ i ] in the body of the text, explain why you think you can use one byte of the data for anything.
If you want to use the contents of buffer, a char array that may, or may not, be NULL terminated, then there is NOT reason to "convert the buffer to something I can use". The buffer is ALREADY something you can use.
And, this is the last time I am responding to this, or any other thread of yours, until you provide the data I have asked for, more than once.
Except for the earlier mentioned issue (the number is NOT a string, so you can't compare), you have a flaw in your thinking Are you going to use 1000 if's if you have 1000 different barcodes?
ok so your saying i can use this to add the .bmp and then use that to open the relevant file. but my problem still remains that i cant use the buffer for this. so how can i use the buffer?
thanks again, to Paul. I'm sorry if I'm fustrating you but im very new to this and have little knowledge.
You will see that buffer is just a character array. The array size is more than large enough to allow you to add more characters to it. That is what strcat() is doing. It will change the data in buffer from "92016401" to "92016401.bmp".
Print stuff to see for yourself that that is what happens.
Then, call bmpDraw(buffer) and see what happens. No rocket science involved. Just a few dozen brain cells working in concert.