// Echo in C and C++ with pushbutton rest // Recreated By Faisal // 22-March-2020 #include #include const int rxPin = 0; const int txPin = 1; const int led = 3; // Make sure that is the program is running const int button = 7; //Disabled for dynamic memory int PBval = 0; // reading pushbutton pin status const int debounce = 300;// debounce for pushbutton const int max_buffer = 25; // Buffer index size boolean newData = false; // Check new data // Prepare Serila with Tx, Rx Pins SoftwareSerial mySerial(rxPin, txPin); // RX, TX void setup() { pinMode (led, OUTPUT); pinMode (button, INPUT); PINA |= (1 << PINA7); // 1 to use internal pull-up resitor to pin A7, 0 to use external // define pin modes for tx, rx: pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); // Initialize Serial mySerial.begin(115200); delay(3000); // To see the message when open the monitor via FTDI cable mySerial.println(""); digitalWrite(led, LOW); } void loop() { // Static variables persist beyond the function call, // preserving their data between function calls. // Unlike local variables that get created and // destroyed every time a function is called static char chr; static char buffer[max_buffer] = {0}; static int index; PBval = digitalRead (button); if (PBval == LOW) { delay(debounce); digitalWrite(led, HIGH); mySerial.println("Clear Buffer"); index = 0; while (index <= 25) { buffer[index] = 0; delay(8.5); ++index; } index = 0; digitalWrite(led, LOW); } // Check if serial is ready, with new data: if (mySerial.available() > 0) { // read the incoming byte: delay(50); chr = mySerial.read(); newData = true; } // Print out the new data: if (newData == true) { buffer[index] = chr; delay(8.5); // say what you got: mySerial.println(buffer); ++index; newData = false; } // Reset the buffer for manage the memory if (index == 25) { index = 0; while (index <= 25) { buffer[index] = 0; delay(8.5); ++index; } index = 0; } }