循环问题(arduino)

时间:2014-02-13 10:37:50

标签: c arduino

我正在尝试使用和arduino在学校创建一个小摊位, 该项目是使用条形码扫描仪扫描物品输入价格并签发收据。条形码扫描器连接到串口,键盘和打印机通过软件串口连接。我正在使用3.5英寸的电视作为显示器。条形码的前3位数字可以识别物品。我想在草图中的某处包含项目名称并匹配并在字符串以其编号开头时打印它们。 我已经创建了一个草图,其中包含来自其他论坛用户的大量帮助,但是日期正在重复打印,并且没有太多其他事情发生。如何仅将日期打印一次。以及如何在打印前缓冲串行和软件序列中的数据。

//#include <TVout.h>
//#include <video_gen.h>

//ARDUINO 1.0 COMPATIBLE ONLY!
//ARDUINO 1.0 COMPATIBLE ONLY!
#include <Time.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>
//#include <LiquidCrystal.h>
#include <Thermal.h>
#include <Wire.h>
//TVout TV;
  int printer_RX_Pin = 3;
  int printer_TX_Pin = 4;
  int incomingByte = 0;
  Thermal printer(printer_RX_Pin, printer_TX_Pin, 19200);

  String readString;    // a string to hold incoming data
   //SoftwareSerial mySerial(10, 11);
  void setup(){
    //TV.begin(PAL,120,96);
    // TV.select_font(font6x8);
    Serial.begin(9600);
    //mySerial.begin(9600);
    setSyncProvider(RTC.get);  
    // if (timeStatus() != timeSet )  
    // Serial.println("Unable to sync with the RTC");
    // else
    //  Serial.println("RTC has set the system time"); 
    //tv.print(scan item)  ???
    //ITEM SCANNED GETS STORED INTO string.Readstring
    //tv print (enter donation  Amount)
    //INPUT FROM KEYPAD GETS STORED INTO  BUFFER1 (KEYPAD IS HOOKED UP TO SOFTWARE SERIAL          PINS)
    //THEN PRINTER PRINTS TITLE  (MAYBE BETTER TO WAIT FOR ALL INPUT BEFORE PRINTING)

    printer.justify('C'); 

    //sets text justification (right, left, center) accepts 'L', 'C', 'R'
    printer.setSize('L'); // set type size, accepts 'S', 'M', 'L'
    printer.println("School Feast "); //print line
    printer.setSize('M'); // set type size, accepts 'S', 'M', 'L'
    printer.println("My Stall"); //print line 
    printer.println("My Town");
    printer.feed(); //advance one line
    printer.println(" My Country");
    printer.feed(); //advance one line 
  }

  void serialRead() {

     while (Serial.available()) {
        delay(10);  
        if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;};
       }
  }
  void loop() {
    // THEN TIME GETS PRINTED 
     tmElements_t tm;

     if (RTC.read(tm)) {
       Serial.print(" ");
       print2digits(tm.Hour);
       Serial.write(':');
       print2digits(tm.Minute);
       Serial.write(':');
       print2digits(tm.Second);
       Serial.print("        ");
       Serial.print(tm.Day);
       Serial.write('/');
       Serial.print(tm.Month);
       Serial.write('/');
       Serial.print(tmYearToCalendar(tm.Year));
       Serial.println();
       printer.print(" ");

       print2digits(tm.Hour);
       printer.write(':');
       print2digits(tm.Minute);
       printer.write(':');
       print2digits(tm.Second);
       printer.print("        ");
       printer.print(tm.Day);
       printer.write('/');
       printer.print(tm.Month);
       printer.write('/');
       printer.print(tmYearToCalendar(tm.Year));
       printer.println();
     } else {
        if (RTC.chipPresent()) {
           Serial.println("The DS1307 is stopped.  Please run the SetTime");
           Serial.println("example to initialize the time and begin running.");
           Serial.println();
        } else {
           Serial.println("DS1307 read error!  Please check the circuitry.");
           Serial.println();
        }
       delay(9000);
  }
   delay(1000);
}

void print2digits(int number) {
   if (number >= 0 && number < 10) {
     Serial.write('0');
   }
   Serial.print(number);
   if (Serial.available() > 0) {
       readString = "";
       serialRead();
       String Item = readString.substring(0,3);
       //THEN ITEM PRINT( IDENTIFY ITEM BY FIRST 3 CHARACTERS OF STRING, HOW??)
       Serial.print(Item);
       printer.print(Item);
       Serial.println();{
       //THEN PRINT DONATION AMOUNT(INPUT FROM SOFTWARE SERIAL)
      //printer.print(software.serial buffer)
      //printer. print("GBP"); 
      //clear display
      //tv.clear

    }

   }
}

0 个答案:

没有答案