Advertisement
pleasedontcode

Scanner rev_124

Nov 7th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Scanner
  13.     - Source Code compiled for: Arduino Pro Mini 3.3V
  14.     - Source Code created on: 2023-11-07 14:26:05
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <SPI.h>
  21. #include <SdFat.h>
  22. #include <EasyButton.h>
  23.  
  24. /****** SYSTEM REQUIREMENT 1 *****/
  25. /* each time the button is pressed, read one bit from */
  26. /* sd card and change the state of led depending from */
  27. /* bit. */
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t pulsante_PushButton_PIN_D3 = 3;
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t led_LED_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF SPI PINS *****/
  40. const uint8_t memoria_SDCardModule_SPI_PIN_MOSI_D11 = 11;
  41. const uint8_t memoria_SDCardModule_SPI_PIN_MISO_D12 = 12;
  42. const uint8_t memoria_SDCardModule_SPI_PIN_SCLK_D13 = 13;
  43. const uint8_t memoria_SDCardModule_SPI_PIN_CS_D10 = 10;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. SdFat SD;
  47. EasyButton button(pulsante_PushButton_PIN_D3);
  48.  
  49. void onPressed()
  50. {
  51.   // Open the file on the SD card
  52.   SdFile file;
  53.   if (!file.open("data.txt", O_READ))
  54.   {
  55.     // Error handling code
  56.     return;
  57.   }
  58.  
  59.   // Read one byte from the file
  60.   uint8_t byteValue;
  61.   if (file.read(&byteValue, sizeof(byteValue)) != sizeof(byteValue))
  62.   {
  63.     // Error handling code
  64.     file.close();
  65.     return;
  66.   }
  67.  
  68.   // Close the file
  69.   file.close();
  70.  
  71.   // Extract the bit value from the byte
  72.   bool bitValue = (byteValue & (1 << 0)) != 0;
  73.  
  74.   // Change the state of the LED depending on the bit value
  75.   digitalWrite(led_LED_PIN_D2, bitValue);
  76. }
  77.  
  78. void setup(void)
  79. {
  80.   // put your setup code here, to run once:
  81.  
  82.   pinMode(pulsante_PushButton_PIN_D3, INPUT_PULLUP);
  83.  
  84.   pinMode(led_LED_PIN_D2, OUTPUT);
  85.  
  86.   pinMode(memoria_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
  87.   // start the SPI library:
  88.   SPI.begin();
  89.  
  90.   // Initialize SD card
  91.   if (!SD.begin(memoria_SDCardModule_SPI_PIN_CS_D10, SPI_FULL_SPEED))
  92.   {
  93.     // SD card initialization failed
  94.     while (1)
  95.     {
  96.       // Error handling code
  97.     }
  98.   }
  99.  
  100.   button.onPressed(onPressed);
  101. }
  102.  
  103. void loop(void)
  104. {
  105.   // put your main code here, to run repeatedly:
  106.   button.read();
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement