Close

Odpověď na: Uchování hodnoty proměnné

Úvodní stránka Fórum Vaše projekty Arduino Uchování hodnoty proměnné Odpověď na: Uchování hodnoty proměnné

#9326
posjirka
Účastník

omlouvat se vubec nemusis. nikdo z nas nezna vsechno a kazdy nejak zacinal.
Kdyz vkladas kod pouzivej parove znacky „code“ (tlacitko CODE) pred a za kodem. uchova si formatovani. trochu jsem ti to prekopal:
– zrusil duplicitni vypocty
– odstranil zobrazovani obrazovek jen pri stisku tlacitka (funkce zobraz() )
– presunul promennou tlacitko
– naformatoval podle se abych se v tom vyznal.
– …

vsechny zmeny mam pod revizi “ #1 “ takze ji muzes v pohode dohledat
Vyzkousej to a uvidis. ja nema jak to vyzkouset a stavet se mi to vazne nechce 🙂 :


// #1 - BY JP 11/2016
// uprava na prepinani obrazovky pomocí tlacitek IR ovladace
// uprava na zobrazovani aktualnich dat (v puvodni verzi zustali hodnoty z doby prd sepnutim tlacitka)

/*—–( Import needed libraries )—–*/
#include <LiquidCrystal.h>
#include <SD.h>
#include <Wire.h>
#include „RTClib.h“
#include <interval.h>
#include <IRremoteInt.h>
#include „IRremote.h“

/*—–( Declare Constants )—–*/
int receiver = 2; // pin 1 of IR receiver to Arduino digital pin 11
int teplVenk = 1; //Cidlo Commet
int vlhkVenk = 0; //Cidlo Commet
int topTepl = 3; //Pt100 topení teplá
int topStud = 2; //Pt100 topení studena

int tlacitko = 0; // #1 - ID cislo tlacitka

/*—–( Declare objects )—–*/
IRrecv irrecv(receiver); // create instance of ‚irrecv‘
decode_results results; // create instance of ‚decode_results‘
File logfile;
Interval blink;
LiquidCrystal lcd(3, 4, 5, 6, 7, 8); // initialize the library with the numbers of the interface pins
RTC_DS1307 RTC; // define the Real Time Clock object

/*—–( Declare Variables )—–*/
const int chipSelect = 10;
const long blinkinterval = 100; // interval at which to blink (milliseconds)

void setup(){ 
  /*—-( SETUP: RUNS ONCE )—-*/
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  lcd.begin(16,2);
  Wire.begin();
  RTC.begin();

  //—————————————-
  if (! RTC.isrunning()) {
    Serial.println(„RTC is NOT running!“); // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  //——————————-
  Serial.print(„Initializing SD card…“);
  pinMode(SS, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(„Card failed, or not present“);
    // don’t do anything more:
    while (1) ;
  }
  
  Serial.println(„card initialized.“);
  // create a new file
  char filename[] = „METEO_00.CSV“;
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + ‚0‘;
    filename[7] = i%10 + ‚0‘;
    if (! SD.exists(filename)) {
      // only open a new file if it doesn’t exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }

  Serial.print(„Logging to: „);
  Serial.println(filename);
  logfile.println(„Datum, TeplotaVenku, VlhkostVenku, TopeniTepla, TopeniStudena“);
  
  
  tlacitko = 0; // #1 - nastaveni vychozi hodnoty
}/*–(end setup )—*/

void loop(){
  /*—-( LOOP: RUNS CONSTANTLY )—-*/

  if (irrecv.decode(&results)) {
    // have we received an IR signal?
    // Serial.println(results.value, HEX); UN Comment to see raw values
    translateIR();
    irrecv.resume(); // receive the next value
  }
  //——————————————————–
  int SCteplVenk = analogRead(teplVenk);
  SCteplVenk = map(SCteplVenk, 0, 1023, -20, 50);
  
  int SCvlhkVenk = analogRead(vlhkVenk);
  SCvlhkVenk = map(SCvlhkVenk, 0, 1023, 0, 100);
  
  int SCtopTepl = analogRead(topTepl);
  SCtopTepl = map(SCtopTepl, 0, 1023, 10, 120);
  
  int SCtopStud = analogRead(topStud);
  SCtopStud = map(SCtopStud, 0, 1023, 10, 120);
  
  String dataString = „“;

  DateTime now = RTC.now();
  //———————————————————–
  if (blink.expired()) {
    blink.set(blinkinterval); // set new interval period
    
    logfile.print(now.year(), DEC);
    logfile.print(„/“);
    logfile.print(now.month(), DEC);
    logfile.print(„/“);
    logfile.print(now.day(), DEC);
    logfile.print(„,“);
    logfile.print(now.hour(), DEC);
    logfile.print(„:“);
    logfile.print(now.minute(), DEC);
    logfile.print(„,“);
    //———————————————————–
    logfile.print(SCteplVenk);
    logfile.print(„,“);
    logfile.print(SCvlhkVenk);
    logfile.print(„,“);
    logfile.print(SCtopTepl);
    logfile.print(„,“);
    logfile.print(SCtopStud);
    logfile.print(„,“);
    // delay(10000); //čekej 10minut
    
    //————————————————————
    
    logfile.println(dataString);
    logfile.flush();
  }
  //———————————————————–

  zobraz(); // #1 - vykresleni obrazovky
  
}/* –(end main loop )– */

/*—–( Declare User-written Functions )—–*/
void translateIR(){ 
  // takes action based on IR code received
/* #1 duplicitni funkce
  int SCteplVenk = analogRead(teplVenk);
  SCteplVenk = map(SCteplVenk, 0, 1023, -20, 50);
  
  int SCvlhkVenk = analogRead(vlhkVenk);
  SCvlhkVenk = map(SCvlhkVenk, 0, 1023, 0, 100);
  
  int SCtopTepl = analogRead(topTepl);
  SCtopTepl = map(SCtopTepl, 0, 1023, 10, 120);
  
  int SCtopStud = analogRead(topStud);
  SCtopStud = map(SCtopStud, 0, 1023, 10, 120);
  // describing KEYES Remote IR codes
*/

//  int tlacitko = 0; // #1 - zruseni 
   switch(results.value){
    case 0x141: 
      tlacitko = 1;
      /* #1 - zruseni 
      Serial.println(“ zmacknuto tlacitko 1 „);
      lcd.setCursor(0, 0); // set the cursor to (0,0):
      lcd.print(SCteplVenk);
      lcd.setCursor(0, 1);
      lcd.print(SCvlhkVenk);
      lcd.setCursor(3, 0);
      lcd.print(„C – Teplota“);
      lcd.setCursor(3, 1);
      lcd.print(„% – Vlhkost“);
      */
      break;
    case 0x942: 
      tlacitko = 2;
      /* #1
      Serial.println(“ zmacknuto tlacitko 2 „);
      lcd.setCursor(0, 0);
      lcd.print(SCtopTepl);
      lcd.setCursor(0, 1);
      lcd.print(SCtopStud);
      lcd.setCursor(3, 0);
      lcd.print(„C – Kotel HOT“);
      lcd.setCursor(3, 1);
      lcd.print(„C – Kotel COLD“);
      */
      break;
    case 0x143: 
      tlacitko = 3;
      /* #1
      Serial.println(“ zmacknuto tlacitko 3 „);
      */
      break;
    case 0x944: 
      tlacitko = 4;
      /* #1
      Serial.println(“ zmacknuto tlacitko 4 „);
      */
      break;
    case 0x145: 
      tlacitko = 5;
      /* #1
      Serial.println(“ zmacknuto tlacitko 5 „);
      */
      break;
    case 0x946: 
      tlacitko = 6;
      /* #1
      Serial.println(“ zmacknuto tlacitko 6 „);
      */
      break;
    case 0x147: 
      tlacitko = 7;
      /* #1
      Serial.println(“ zmacknuto tlacitko 7 „);
      */
      break;
    case 0x948: 
      tlacitko = 8;
      /* #1
      Serial.println(“ zmacknuto tlacitko 8 „);
      */
      break;
    case 0x149: 
      tlacitko = 9;
      /*  #1
      Serial.println(“ zmacknuto tlacitko 9 „);
      */
      break;
    case 0x940: 
      tlacitko = 0;
      /* #1
      Serial.println(“ zmacknuto tlacitko 0 „);
      */
      break;
    default:
      /* #1
      Serial.println(“ jine tlacitko „);
      */
  }// End Case
} //END translateIR

void zobraz(){  // #1 - funkce vykresleni obrazovky
   switch(tlacitko){
    case 1: 
      Serial.println(“ zmacknuto tlacitko 1 „);
      lcd.setCursor(0, 0); // set the cursor to (0,0):
      lcd.print(SCteplVenk);
      lcd.setCursor(0, 1);
      lcd.print(SCvlhkVenk);
      lcd.setCursor(3, 0);
      lcd.print(„C – Teplota“);
      lcd.setCursor(3, 1);
      lcd.print(„% – Vlhkost“);
      break;
    case 2: 
      Serial.println(“ zmacknuto tlacitko 2 „);
      lcd.setCursor(0, 0);
      lcd.print(SCtopTepl);
      lcd.setCursor(0, 1);
      lcd.print(SCtopStud);
      lcd.setCursor(3, 0);
      lcd.print(„C – Kotel HOT“);
      lcd.setCursor(3, 1);
      lcd.print(„C – Kotel COLD“);
      break;
    case 3: 
      Serial.println(“ zmacknuto tlacitko 3 „);
      break;
    case 4: 
      Serial.println(“ zmacknuto tlacitko 4 „);
      break;
    case 5: 
      Serial.println(“ zmacknuto tlacitko 5 „);
      break;
    case 6: 
      Serial.println(“ zmacknuto tlacitko 6 „);
      break;
    case 7: 
      Serial.println(“ zmacknuto tlacitko 7 „);
      break;
    case 8: 
      Serial.println(“ zmacknuto tlacitko 8 „);
      break;
    case 9: 
      Serial.println(“ zmacknuto tlacitko 9 „);
      break;
    case 0: 
      Serial.println(“ zmacknuto tlacitko 0 „);
      break;
    default:
      Serial.println(“ jine tlacitko „);
  }
}