Go Down

Topic: SOLVED two questios about Serial.print (Read 241 times) previous topic - next topic

matelot

Oct 20, 2016, 10:18 pm Last Edit: Oct 20, 2016, 11:39 pm by matelot
I am altering a program to run an RFID.
The program has many  Serial.print   instructions
They are all in the format    Serial.print(F("string"))
The learning section on this site says

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

here
about twenty lines from the top.
Can someone tell me more information about this or point me to somewhere which explains the reasoning ?

I have been trying to find where in the program the information to the monitor shows a "7" at the start of the first line?
I have posted just  down to the bottom of the setup, much more and I exceeded the 9000 character limit.
fairly high up in the setup is the following line that looks like the first thing printed

Serial.println(F("Access Control v3.3"));   // For debugging purposes

this is the first thing to appear when I click on the monitor button but it is preceded by a 7 ?  Why?


Code: [Select]
/*
  Arduino RFID Access Control

  Security !

  Copyright (C) 2015 Omer Siar Baysal

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include <EEPROM.h>     // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h>        // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices

/*
 Instead of a Relay maybe you want to use a servo
 Servos can lock and unlock door locks too
 There are examples out there.
 */

// #include <Servo.h>

/*
 For visualizing whats going on hardware
 we need some leds and
 to control door lock a relay and a wipe button
 (or some other hardware)
 Used common anode led,digitalWriting HIGH turns OFF led
 Mind that if you are going to use common cathode led or
 just seperate leds, simply comment out #define COMMON_ANODE,
 */

//#define COMMON_ANODE

#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif

#define redLed 7 // Set Led Pins
#define greenLed 6
#define blueLed 5

#define relay 4 // Set Relay Pin
#define wipeB 3 // Button pin for WipeMode

boolean match = false;          // initialize card match to false
boolean programMode = false; // initialize programming mode to false

int successRead; // Variable integer to keep if we have Successful Read from Reader

byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM

/*
 We need to define MFRC522's pins and create instance
 Pin layout should be as follows (on Arduino Uno):
 MOSI: Pin 11 / ICSP-4
 MISO: Pin 12 / ICSP-1
 SCK : Pin 13 / ICSP-3
 SS : Pin 10 (Configurable)
 RST : Pin 9 (Configurable)
 look MFRC522 Library for
 other Arduinos' pin configuration
 */

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {




 
   // Setting Auto Read Mode - EM4102 Decoded Mode - No password
   // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
   Serial.write(0xFF);  //Header
   Serial.write(0x01);  //Reserved
   Serial.write(0x09);  //Length (Command + Data)
   Serial.write(0x87);  //Command (0x87 sets auto mode behavior
   Serial.write(0x01);  //Data 1: Enable Auto-Read
   Serial.write(0x03);  //Data 2: Mode - Parity decoded - Manchester RF/64
   Serial.write(0x02);  //Data 3: Total number of block to be read (2)
   Serial.write((byte)0x00);  //Data 4: No password expected
   Serial.write(0x10);  //Data 5: Password byte 1
   Serial.write(0x20);  //Data 6: Password byte 2
   Serial.write(0x30);  //Data 7: Password byte 3
   Serial.write(0x40);  //Data 8: Password byte 4
   Serial.write(0x37);  //Checksum
  
   delay(500);





  
  //Arduino Pin Configuration
  
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
  pinMode(relay, OUTPUT);
  
  //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
  
  digitalWrite(relay, HIGH); // Make sure door is locked
  digitalWrite(redLed, LED_OFF); // Make sure led is off
  digitalWrite(greenLed, LED_OFF); // Make sure led is off
  digitalWrite(blueLed, LED_OFF); // Make sure led is off
  

  //Protocol Configuration
  Serial.begin(9600); // Initialize serial communications with PC
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  
  //If you set Antenna Gain to Max it will increase reading distance
  //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
  
  Serial.println(F("Access Control v3.3"));   // For debugging purposes
  ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details

  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
    digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
    Serial.println(F("Wipe Button Pressed"));
    Serial.println(F("You have 5 seconds to Cancel"));
    Serial.println(F("This will be remove all records and cannot be undone"));
    delay(5000);                        // Give user enough time to cancel operation
    if (digitalRead(wipeB) == LOW) {    // If button still be pressed, wipe EEPROM
      Serial.println(F("Starting Wiping EEPROM"));
      for (int x = 0; x < EEPROM.length(); x = x + 1) {    //Loop end of EEPROM address
        if (EEPROM.read(x) == 0) {              //If EEPROM address 0
          // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
        }
        else {
          EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
        }
      }
      Serial.println(F("EEPROM Successfully Wiped"));
      
      digitalWrite(redLed, LED_OFF); // visualize successful wipe
      delay(200);
      digitalWrite(redLed, LED_ON);
      delay(200);
      digitalWrite(redLed, LED_OFF);
      delay(200);
      digitalWrite(redLed, LED_ON);
      delay(200);
      digitalWrite(redLed, LED_OFF);
      
    }
    else {
      Serial.println(F("Wiping Cancelled"));
      digitalWrite(redLed, LED_OFF);
    }
  }
  // Check if master card defined, if not let user choose a master card
  // This also useful to just redefine Master Card
  // You can keep other EEPROM records just write other than 143 to EEPROM address 1
  // EEPROM address 1 should hold magical number which is '143'
  if (EEPROM.read(1) != 143) {  
    Serial.println(F("No Master Card Defined"));
    Serial.println(F("Scan A PICC to Define as Master Card"));
    do {
      successRead = getID();            // sets successRead to 1 when we get read from reader otherwise 0
      digitalWrite(blueLed, LED_ON);    // Visualize Master Card need to be defined
      delay(200);
      digitalWrite(blueLed, LED_OFF);
      delay(200);
      
    }
    while (!successRead);                  // Program will not go further while you not get a successful read
    for ( int j = 0; j < 4; j++ ) {        // Loop 4 times
      EEPROM.write( 2 + j, readCard[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
    }
    EEPROM.write(1, 143);                  // Write to EEPROM we defined Master Card.
    Serial.println(F("Master Card Defined"));
  }
  Serial.println(F("-------------------"));
  Serial.println(F("Master Card's UID"));
  for ( int i = 0; i < 4; i++ ) {          // Read Master Card's UID from EEPROM
    masterCard[i] = EEPROM.read(2 + i);    // Write it to masterCard
    Serial.print(masterCard[i], HEX);
  }
  Serial.println("");
  Serial.println(F("-------------------"));
  Serial.println(F("Everything Ready"));
  Serial.println(F("Waiting PICCs to be scanned"));
  cycleLeds();    // Everything ready lets give user some feedback by cycling leds
}



AWOL

#1
Oct 20, 2016, 10:21 pm Last Edit: Oct 20, 2016, 10:28 pm by AWOL
You've got a load of "Serial.write"s before you call "Serial.begin" - why?

Quote
Can someone tell me more information about this or point me to somewhere which explains the reasoning ?
Strings would normally be copied into RAM before main() is run.
If the strings are constant, this is a waste, so the F() macro ensures they remain in flash memory, where they clearly belong.

Code: [Select]
Serial.write(0x37);  //ChecksumThat's where your '7' comes from
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

sterretje

Your last serial.write in the beginning of setup will write a '7'; check the ascii table (e.g. asciitable.com).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

matelot

the serialWrites are to setup the addresses for the Mifare library to use to read an rfid.

I commented out a few of the lines and the symbol in the monitor altered to the ascii code of the last line uncommented, so that answers both my questions thank you.

Jiggy-Ninja

#4
Oct 21, 2016, 12:04 am Last Edit: Oct 21, 2016, 12:05 am by Jiggy-Ninja
the serialWrites are to setup the addresses for the Mifare library to use to read an rfid.
That doesn't answer AWOL's question of why you are doing that before calling begin(). It looks like you're using the module's SPI interface anyway, so why are you trying to send commands to it with the UART (Serial)?
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

Go Up