Saltatu nabigazioa

Sketcha programatzea

Sketch: programazioa

Koadro honetan ikus dezakezu liburutegia kargatu ondoren Arduino MKR Wifi 1010 plakara bidaliko dugun programa:

    

/*   
  Sketch generated by the Arduino IoT Cloud Thing "p04"
  https://create.arduino.cc/cloud/things/81ef512f-ddd6-403c-bd2b-7046e03f0818 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float sonidomuestra;
  float voltios;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

const int sensorPIN = A0;

// Soinu-sentsorearen banda-zabalera deklaratzen dugu
const int sampleWindow = 50; // Ancho ventana en mS (50 mS = 20Hz)

#include "thingProperties.h"

void setup() {
  
// Irteerak konfiguratu
   pinMode(LED_BUILTIN, OUTPUT); // Usaremos LED_BUILTIN como señal cualdo la medida supera un valor de tension de 2 V
  
// Hasieratu seriea eta itxaron ataka ireki arte:

   Serial.begin(9600);

// Atzerapen honek serieko monitore baten zain egoteko aukera ematen du blokeatu gabe, ez bada aurkitu

   delay(1500); 

// thingProperties-en definitua.h

  initProperties();

// Konektatu Arduino IoT Cloud-era

 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}


void loop() {
  ArduinoCloud.update();

// Hemen zure kodea

  unsigned long startMillis= millis();

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;
 
// Bildu leihoan zehar

   unsigned int sample;
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(sensorPIN);
      if (sample < 1024)
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // Actualizar máximo
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // Actualizar mínimo
         }
      }
   }
 
   unsigned int peakToPeak = signalMax - signalMin;  // Soinuaren anplitudea
   double volts = (peakToPeak * 5.0) / 1024;  // Bihurtu tentsiora
   
    //Serial.println(volts);
   //Serial.println(sample);
  
  voltios = volts;

  sonidomuestra=sample;

// Emaitzak "serie monitore" bidez ere ikus ditzakegu
  
        Serial.print("Señal = ");
        Serial.print(sample);
        Serial.println(" ");
       
        Serial.print("Voltios = ");
        Serial.print(volts);
        Serial.println(" V");
        Serial.println(" ");


     if(voltios >= 2 ){      
                
                digitalWrite(LED_BUILTIN,  HIGH); 
                delay(400);
                Serial.println("LED_BUILTIN Encendido");  } 

   else
   
   {Serial.println("LED_BUILTIN NO Encendido");}  

   Serial.println(" ------ ");

}

Adi!

Gogoratu programa plakara bidali behar dela, horretarako goiko aldean dagoen gezi itxurako botoia erabiliz.

Programa Arduino plakara bidaltzeko botoia
Mercedes Jimeno Badiola (Jabetza publikoa)

Creado con eXeLearning (Ventana nueva)