Advertisement
Alx09

pt bia

Mar 25th, 2022
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #define sensorPin A0
  3. #define mesMaxLenght 32
  4. #define nrMessages 10
  5.  
  6. const int redPin = 3;
  7. const int greenPin = 5;
  8. const int bluePin = 6;
  9. int id;
  10. int messagePoz;
  11. int sensorPin1 = 4;  
  12. int sensorValue1 = 0;  // variable to store the value coming from the sensor
  13. byte value;
  14. int lastSent=0;
  15. char message[mesMaxLenght];
  16. String mesaj;
  17. int eeAddress = 0;   //Location we want the data to be put.
  18. unsigned int numar_CAN;
  19. int voltageValue, temperatureRead;
  20.  
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.   pinMode(LED_BUILTIN, OUTPUT);
  25.   pinMode(redPin, OUTPUT);
  26.   pinMode(greenPin, OUTPUT);
  27.   pinMode(bluePin, OUTPUT);
  28.   EEPROM.get(mesMaxLenght * (nrMessages - 1), messagePoz); // luam pozitia la care am ajuns ultima data asta e o frmula matematica
  29.   eeAddress = messagePoz * mesMaxLenght ; // aflam adresa din eeprom la care trebuie sa prelucram
  30. }
  31.  
  32. void displayMes(){
  33.   int eeAddress = 0;
  34.  
  35.   while(eeAddress < mesMaxLenght * nrMessages){
  36.      EEPROM.get(eeAddress, message);
  37.      if(message[0] == '\0') return; // daca nu este salvat nimic la adresa parasim functia
  38.      Serial.println(message);
  39.      eeAddress += mesMaxLenght;
  40.   }
  41. }
  42.  
  43. void RGB(){
  44.   int red = Serial.parseInt();  
  45.     int green = Serial.parseInt();
  46.     int blue = Serial.parseInt();
  47.       analogWrite(redPin, red);
  48.       analogWrite(greenPin, green);
  49.       analogWrite(bluePin, blue);
  50.       Serial.print(red, HEX);
  51.       Serial.print(green, HEX);
  52.       Serial.println(blue, HEX);
  53. }
  54. void turnLed(){
  55.       while(Serial.available()  < 1) delay(1);// asteptams a se trimita date
  56.        int buffer = Serial.read();
  57.     while( buffer == 32) buffer = Serial.read(); // pentru a evita punctele albe
  58.      
  59.        if( buffer > 96) buffer -= 32; // aici e transformare din litera mica in mare ca sa puteti introduce si a, si A
  60.       if(buffer == 'A')
  61.              digitalWrite(LED_BUILTIN, HIGH);
  62.       else if(buffer == 'S')
  63.              digitalWrite(LED_BUILTIN, LOW);
  64.       else Serial.println("Valoare invalida");
  65.        while(Serial.available())buffer = Serial.read();// golim buffer
  66.    
  67.     }
  68.  
  69. void temp(){
  70.   while(1){
  71.      if(millis() >= lastSent +1000){
  72.     // Get the voltage reading from the LM35
  73.   int reading = analogRead(sensorPin);
  74.  
  75.   // Convert that reading into voltage
  76.   float voltage = reading * (5.0 / 1024.0);
  77.  
  78.   // Convert the voltage into the temperature in Celsius
  79.   float temperatureC = voltage * 100;
  80.  
  81.   // Print the temperature in Celsius
  82.   Serial.print("Temperature: ");
  83.   Serial.print(temperatureC);
  84.   Serial.print("\xC2\xB0"); // shows degree symbol
  85.   Serial.print("C  |  ");
  86.   lastSent = millis();
  87.   }
  88. }
  89. }
  90.  
  91.   void readEEPROM(){
  92.       Serial.println("Introduceti mesaj: ");
  93.       while(Serial.available()  < 1) delay(1); // asteptam introdcere de mesaj
  94.       mesaj = Serial.readString();
  95.       mesaj.toCharArray(message, mesMaxLenght);
  96.       EEPROM.put(eeAddress, message);
  97.       eeAddress += mesMaxLenght;
  98.       messagePoz++;
  99.       if(messagePoz == nrMessages)
  100.       {
  101.         messagePoz = 0;
  102.         eeAddress = 0;
  103.       }
  104.       EEPROM.put(mesMaxLenght * (nrMessages - 1), messagePoz);
  105.      Serial.println("Mesaj salvate in EEPROM: ");  
  106.      displayMes();
  107.   }
  108.  
  109. void loop() {
  110.  
  111.   if (Serial.available()){
  112.  
  113.      id = Serial.parseInt();
  114.     switch(id){
  115.       case 1: turnLed();
  116.              break;
  117.       case 2: RGB();
  118.              break;
  119.       case 3: temp();
  120.             break;
  121.        case 4:
  122.       while(1)
  123.       {
  124.     // read the value from the sensor:
  125.      sensorValue1 = digitalRead(sensorPin1);
  126.      Serial.println(sensorValue1);
  127.      
  128.       }
  129.       break;  
  130. case 5:
  131.      readEEPROM();
  132.      break;
  133. default:
  134.     Serial.println("Valoare invalida");
  135.       break;
  136.     }
  137.   }
  138.  delay(1000);
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement