text:
need help in code to send sms to multiple contacts using arduino, gsm800, push button

When button 1 is pressed, the message should be sent to 4 members. But we are receiving the message for one member and after some delay for the other members. But receiving to all members of contacts given in the code (Here numbers are not mentioned)
#include <LiquidCrystal.h>

int ButtonValue1=0;
int ButtonValue2=0;
int ButtonValue3=0;
int ButtonValue4=0;

int Button1=8;
int Button2=9;
int Button3=10;
int Button4=11;
int buz=12;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void okcheck()
{
  unsigned char rcr;
  do{
    rcr = Serial.read();
  }while(rcr != 'K');
}

void setup()
{
  Serial.begin(9600);

  pinMode(buz,OUTPUT);
  pinMode(Button1,INPUT);
  pinMode(Button2,INPUT);
  pinMode(Button3,INPUT);
  pinMode(Button4,INPUT);

  digitalWrite(12, HIGH);

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("INNOVATIVE CLG");
  lcd.setCursor(0, 1);
  lcd.print("BUS INFO SYSTEM");

  delay(1000);
}

void loop()
{
  ButtonValue1=digitalRead(Button1);
  ButtonValue2=digitalRead(Button2);
  ButtonValue3=digitalRead(Button3);
  ButtonValue4=digitalRead(Button4);

  if(ButtonValue1!=0)
  {
    lcd.clear();
    lcd.print("BUS STARTED...");
    Serial.write("AT\r\n");         okcheck();
    Serial.write("ATE0\r\n");         okcheck();
    Serial.write("AT+CMGF=1\r\n");         okcheck();
    Serial.write("AT+CNMI=1,2,0,0\r\n");         okcheck();
    Serial.write("AT+CSMP=17,167,0,0\r\n");         okcheck();
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(100);
    Serial.write("GUD MNG.. BUS STARTED FROM STARTING POINT\r");
    delay(100);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(100);
    Serial.write("GUD MNG.. BUS STARTED STARTING POINT\r");
    delay(100);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(100);
    Serial.write("GUD MNG.. BUS STARTED STARTING POINT\r");
    delay(100);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(100);
    Serial.write("GUD MNG.. BUS STARTED STARTING POINT\r");
    delay(100);
    Serial.write(0x1A);
    delay(1000);delay(1000);
  }
  if(ButtonValue2!=0)
  {
    lcd.clear();
    lcd.print("BUS IS @ 1st stage");
    Serial.write("AT\r\n");         okcheck();
    Serial.write("ATE0\r\n");         okcheck();
    Serial.write("AT+CMGF=1\r\n");         okcheck();
    Serial.write("AT+CNMI=1,2,0,0\r\n");         okcheck();
    Serial.write("AT+CSMP=17,167,0,0\r\n");         okcheck();
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(1000);
    Serial.write("BUS IS @ 1st stage\r");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(1000);
    Serial.write("BUS IS @ 1st stage\r");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(1000);
    Serial.write("BUS IS @ 1st stage\r");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);delay(1000);
  }
  if(ButtonValue3!=0)
  {
    lcd.clear();
    lcd.print("BUS IS @ 2nd stage");
    Serial.write("AT\r\n");         okcheck();
    Serial.write("ATE0\r\n");         okcheck();
    Serial.write("AT+CMGF=1\r\n");         okcheck();
    Serial.write("AT+CNMI=1,2,0,0\r\n");         okcheck();
    Serial.write("AT+CSMP=17,167,0,0\r\n");         okcheck();
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(1000);
    Serial.write("BUS IS @ 2nd stage\r");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);delay(1000);
    Serial.write("AT+CMGS=\"+91...........\"\r");
    delay(1000);
    Serial.write("BUS IS @ 2nd stage\r");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);delay(1000);
  }
  if(ButtonValue4!=0)
  {
    lcd.clear();
    lcd.print("BUS IS @ COLLEGE");
    digitalWrite(buz,LOW);
    delay(1000);
    digitalWrite(buz,HIGH);
  }
}

I suggest you write a sendSMS(const char *number, const char *message) function...

You need to refactor you code, It has become just too complicated for this kind of straight linear code! Your code must be organized better, Don't Repeat Yourself (DRY); just by doing this you may find your error(s).
First lets fix your variables:
#include <LiquidCrystal.h>
#define NUMBUTTS 4
int ButtonValues[NUMBUTTS]= {0,0,0,0};
int Buttons[NUMBUTTS]= {8,9,10,11};
int buz=12;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

and add some new ones
String numbers[NUMBUTTS] =
{
    "+91234567890",
    "+91234567890",
    "+91234567890",
    "+91234567890",
}

String msgs[NUMBUTTS-1] =
{
   "GUD MNG.. BUS STARTED FROM STARTING POINT\r",
   "BUS IS @ 1st stage\r",
   "BUS IS @ 2nd stage\r",
}

next lets clean up your function:
void okcheck()
{
    unsigned char rcr = 0;
    while(rcr != 'K') // rcr can't be 'K' when it starts as 0
        rcr = Serial.read();
}

Setup needs to be changed now that we changed your vars:
void setup()
{
    Serial.begin(9600);

    pinMode(buz,OUTPUT);
    digitalWrite(buz, HIGH);

    for(int i=0; i<NUMBUTTS; i++)
        pinMode(Buttons[i],INPUT);

    lcd.begin(16, 2);
    lcd.clear();
    lcd.print("INNOVATIVE CLG");
    lcd.setCursor(0, 1);
    lcd.print("BUS INFO SYSTEM");

    delay(1000);
}

now we need to extract duplicated code and put it in a function:
// common starting code.
void preamble()
{
    Serial.write("AT\r\n");
    okcheck();

    Serial.write("ATE0\r\n");
    okcheck();

    Serial.write("AT+CMGF=1\r\n");
    okcheck();

    Serial.write("AT+CNMI=1,2,0,0\r\n");
    okcheck();

    Serial.write("AT+CSMP=17,167,0,0\r\n");
    okcheck();
}

// common message code
void send_sms(String& msg)
{
    String prefix = "AT+CMGS=\"";
    preamble();

    for(int i=0;i<NUMBUTTS;i++)
    {
        Serial.write(prefix+numbers[i]+"\"\r");
        delay(100);

        Serial.write(msg);
        delay(100);

        Serial.write(0x1A);
        delay(2000)
    }
}

now we need to fix up loop():
void loop()
{
    for(int i=0; i<NUMBUTTS; i++)
    {
        ButtonValues[i]=digitalRead(Buttons[i]);
    }

    for(int i=0; i<NUMBUTTS-1; i++)
    {
        if(ButtonValues[i]!=0)
        {
            lcd.clear();
            lcd.print(msgs[i]);
            send_sms(msgs[i]);
        }
    }

    //special case for button4
    if(ButtonValue[3]!=0)
    {
         lcd.clear();
         lcd.print("BUS IS @ COLLEGE");
         digitalWrite(buz,LOW);
         delay(1000);
         digitalWrite(buz,HIGH);
    }
}

Now your program logic is separate from the code that sends the messages, and you can test each in isolation from each other. You can test that send_sms() actually sends SMSs. Then you can see if your logic does what you think it does; by either replacing send_sms() with a test stub, or just by inspection.
With clean code it should be easier to spot your problem. The code looks like it should work, so I would guess that the modem itself may be causing the delays, maybe it takes a while to send a message and you are queuing them up and it takes a few seconds to get through each one, its possible that not all get send because one or more gets dropped while it is busy sending another. Hard to know without looking at the documentation for your module.
Your diagram looks like it is hooked up wrong, you have TX-> RX, but MOSI -> TX; Serial.read() reads from RX not MOSI, so i don't know how checkok() doesn't just loop forever, since you left the arduino RX floating.

The code has occurred error while compiling. There is no declaration of ButtonValue(3) in the code, but it is used in the special case for button 4.

@PavanKumar ButtonValue_s_ is an array and ButtonValues[3] is definitely in the code; there is no guarantee that i didn't make a typo or error you might have to fix.


text:
GPS Logger problem
I have some problems with my GPS Logger. U use Arduino Uno, NEO - 6M, SD card.
Problem is in function void loop. I enter inside, but stuck on the "while loop", don't even get inside this loop. Here is code I use:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SPI.h>
#include <SD.h>

#define SD_CS 4
#define FILE_BASE_NAME "Trac"

TinyGPS gps;
SoftwareSerial serial(10, 11);

File myFile;

const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";

void setup(){
  Serial.begin(9600);
  serial.begin(9600);

  Serial.println("SD card initialization...");
  if(!SD.begin(SD_CS))
  {
    Serial.println("Failed! Check card");
    //while(1);
  }
  Serial.println("Initialization done");

  while (SD.exists(fileName)){
    if (fileName[BASE_NAME_SIZE + 1] != '9')
    {
      fileName[BASE_NAME_SIZE + 1]++;
    }
    else if (fileName[BASE_NAME_SIZE] != '9')
    {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    }
    else{
      Serial.println("Can't create file name");
      //return;
    }
  }

  myFile = SD.open(fileName, FILE_WRITE);
  if (!myFile) {
    Serial.println("open failed");
    //return;
  }
  Serial.print("opened: ");
  Serial.println(fileName);
}

void loop(){
  while (serial.available())
  {
    int c = serial.read();
    if (gps.encode(c))
    {
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);
      Serial.print("Latitude:");Serial.println(lat);
      Serial.print("Longitude:");Serial.println(lon);
      myFile = SD.open(fileName, FILE_WRITE);
      myFile.print(lat);
      myFile.print(", ");
      myFile.print(lon);
      myFile.println("");
      myFile.close();
    }
  }
}

If I use this code, it gives me the lat ad lon:
#include <SoftwareSerial.h>
#include <TinyGPS.h>

TinyGPS gps;
SoftwareSerial nss(10, 11);

void setup(){
  nss.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  while (nss.available())
  {
    int c = nss.read();
    if (gps.encode(c))
    {
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);

      Serial.print("Szerokosc:");Serial.println(lat);
      Serial.print("Dlugosc:");Serial.println(lon);

    }
  }
}

you need to describe the observed results .... you made no mention if anything actually prints in the serial monitor

Yes sorry, the observed results:
-code with GPS only is print location on serial monitor, so GPS works fine
-code with GPS and SD card only show inforation about file create and stop

SD uses SPI. On the Uno, SPI is on pins 10-13. You are also using pins 10 and 11 for software serial. That won't work. You need to use pins other than 10 and 11 for software serial.

Owww that was so simple, and stupid mistake for me. You saved my life! Thanks so much.
First I changed the pins of serial to 8 and 9, it's not work, but when I change to 5 and 6 it works perfectly. Once again thanks! :)

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes


text:
ESP8266 not accessing PHP file
I'm trying to control NodeMCU via Android application and I can control NodeMCU easily but when I added the URL to hit the PHP code to enter data in MYSQL, neither NodeMCU is controllable nor NodeMCU is hitting the PHP code. I'm confused with content type: text/html - I'm not sure where to set this piece. Is there any mistake I'm doing or anything I'm missing?
Following is the ESP8266 code on Arduino IDE:
#include <ESP8266WiFi.h>

const char* ssid = "ssid";
const char* password = "password";
const char* host = "aoatech.ml";

WiFiServer server(3000);
int light1 = 13;
int light2 = 15;
int fan = 12;
int socket = 14;
String what;
int which;
String mac;

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(socket, OUTPUT);
  digitalWrite(light1, 0);
  digitalWrite(light2, 0);
  digitalWrite(fan, 0);
  digitalWrite(socket, 0);
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.print("Connecting to ");
  Serial.println(host);
  //  const int httpPort = 3000;
  //  if (!client.connect(host, httpPort)) {
  //    Serial.println("Connection failed!");
  //    return;
  //  }
  mac = WiFi.macAddress();
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  int val;
  if (req.indexOf("/light1/1") != -1){
    val = 1;
    what = "Light_1";
    which = light1;
  }
  else if (req.indexOf("/light1/0") != -1){
    val = 0;
    what = "Light_1";
    which = light1;
  }
  else if (req.indexOf("/light2/1") != -1){
    val = 1;
    what = "Light_2";
    which = light2;
  }
  else if (req.indexOf("/light2/0") != -1){
    val = 0;
    what = "Light_2";
    which = light2;
  }
  else if (req.indexOf("/fan/1") != -1){
    val = 1;
    what = "Fan";
    which = fan;
  }
  else if (req.indexOf("/fan/0") != -1){
    val = 0;
    what = "Fan";
    which = fan;
  }
  else if (req.indexOf("/socket/1") != -1){
    val = 1;
    what = "Socket";
    which = socket;
  }
  else if (req.indexOf("/socket/0") != -1){
    val = 0;
    what = "Socket";
    which = socket;
  }
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  digitalWrite(which, val);

  String url = "http://aoatech.ml/hitthis.php?room=Bed%20Room%201";
  url += "&state=";
  url += val;
  url += "&button=";
  url += what;
  url += "&mac=";
  url += mac;

  Serial.print("Requesting URL: ");
  Serial.println(url);

  client.flush();
  //  client.println("HTTP/1.1 200 OK");
  //  client.println("Content-Type: text/html");
      client.print(String("GET ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  client.println("");
  client.print(what + " is now: ");

  if(val == 0) {
    client.print("Off");
  } else {
    client.print("On");
  }
  Serial.println("Client disonnected");
}

Don't cross post. You already asked this on SO.

i need urgent answer for this post no one is responding.

Doesn't matter. This isn't the place to get urgent answers.

what does hit the php code mean?

I mean here my esp is not accessing the url so that it is not accessing my php file on server.

You mixed WiFiServer and client request examples by replacing servers HTTP response text with a HTTP request text. A client object returned by WiFiServer's available function is a counterpart of requesting client (for example web browser).
See WebClient example for a client connecting to some server.

Your PHP server has a certain content type it can receive to process your requests from the ESP8266. The two most generic content-types used for php are either application/x-www-form-urlencoded or multipart/form-data. Try to play with those two and see if that solves it for you.

you don't see that the code in question is a mess?

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
Measure revolutions per second with reed switch
I'm trying to measure speed of a fly wheel with a reed switch with one magnet (1 pulse per full rotation). I have an 100nf cap connected to switch to help with debounce.

#include <Arduino.h>

int speedPin = 2; // switch is connected to pin 2
int ledPin = 13;

int debounceTime = 7;

long speedPulseTime = 0;
int speedPulseCount = 0;
int speed = 0;
long speedLastSample = 0;

long serialLastReport = 0;
int serialReportFreq = 40;

void speedPulse()
{
  long time = millis();
  if ((speedPulseTime + debounceTime) < time)
  {
    speedPulseCount++;
    speedPulseTime = time;
  }
}

void setup()
{
  pinMode(ledPin, OUTPUT);

  pinMode(speedPin, INPUT_PULLUP);     // Set the switch pin as input
  attachInterrupt(digitalPinToInterrupt(speedPin), speedPulse, HIGH);

  Serial.begin(115200);
  Serial.println("Ready...");
}

void loop()
{
  long time = micros();
  if (speedPulseCount >= 4 || (speedLastSample + 1500000) < time)
  {
    detachInterrupt(speedPin);
    int tempSpeed = 1000000 / ((time - speedLastSample) / speedPulseCount);
    if (tempSpeed < 0)
    {
      speed = 0;
    }
    else
    {
      speed = tempSpeed;
    }

    //reset sample
    speedLastSample = time;
    speedPulseCount = 0;
    attachInterrupt(digitalPinToInterrupt(speedPin), speedPulse, HIGH);
  }

  if ((serialLastReport + serialReportFreq) < millis())
  {
    Serial.print(millis());
    Serial.print(",");
    Serial.println(speed);
    serialLastReport = millis();
  }
}

Here the code I have so far. The problem I have it jumps around a little bit. What could I change to ensure more accurate readings?

Can you state more accurately (by editing your post) what "jumps around a little bit"?  As written, one could assume it was your code that jumped around.

It sounds like occasional "misses". Possible causes include:

the magnet passing far enough from the reed switch to "only just" trigger it [narrow the gap.];
the magnet's passage being too short for the reed switch/capacitor to respond [use a wider magnet or move closer to the center of rotation];
Too much capacitance [reduce it or debounce in software];

Update:

Would it be better to debounce in software and what would be the best way to do it.

If you have an oscilloscope available, you could look at the raw switch signal (no capacitor) to find out how long it bounces. I'm guessing that it will be around 10ms for such a small switch. If the entire [make/break/make/break....] sequence takes less than 50% (being conservative) of the wheel rotation time (1/max_RPS), you can probably count the first pulse and ignore the switch for (0.5/max_RPS), or 1/2 of the fastest rotation. You appear to be doing something like this already. If you want to be a little bit more rigorous, you might do:
see a HIGH;
ignore switch for debounce_time;
if still HIGH,
   count++;

Also, don't forget that variables shared between your Interrupt function and the main-line code need to be:

declared as volatile, to tell the compiler not to expect that this variable has the value it had last time the mainline code read it; and
Turn interrupts off briefly while reading or writing such a variable (if it is multi-byte) in your mainline, to prevent it being changed by the interrupt function between byte-reads/writes by the mainline.

Would it be better to debounce in software and what would be the best way to do it. I already have a little debounce in software, would this do?


text:
How to manage two alarms with one interrupt?
I have a datalogger using the RTC DS3231 as an interrupt generator. I am able to have the interrupts work but here is my problem. I would like to take 2 separate measures from 2 different sensors (one for humidity, temperature and the other for accelerations), one every 30 min et one every hour, the thing is that because I use the same pin (only one pin from the DS3231) for both of these alarms I cannot use a simple counter in my interrupt function otherwise one measure will not be taken when both of the alarms will trigger...
For example I just did a quick test with one alarm each min when seconds are 00 and an other alarm each minute when seconds are 20. The problem as I said is that I cannot increment counters for each since I use the same interrupt function (because of the single pin).
Here is my loop function and interrupt handling function :

void loop() {
  delay(1000);
  sleepNow();
  if (alarm == true){
    handleAlarm(); // reset the possibility to trigger alarm
    if (counter_dtphl == COUNT_DTPHL){
      take_measure_DTPHL();
      counter_dtphl = 0;
    }
    else if (counter_accel == COUNT_ACCEL){
      take_measure_ACCEL();
      counter_accel = 0;
    }
  }
}

void wakeUpNow() {
  Serial.println("In wake up now");
  delay(100);
  alarm = true;
  //Serial.println(counter);
  sleep_disable();//Disable sleep mode
  byte sec = get_date(SEC);
  Serial.println(sec);
  if (sec == 0){
  counter_dtphl +=1;
  }
  else if (sec == 30){
  counter_accel +=1;
  }
  detachInterrupt(digitalPinToInterrupt(interruptPin)); //Removes the interrupt from pin 2;
}

void handleAlarm() {
  alarm = false;
  rtcObject.LatchAlarmsTriggeredFlags();
}

COUNT_ACCEL and COUNT_DTPHL are constant having the value of minutes corresponding to the trigger for the measure.
Has anybody an idea how to manage this situation ?
For the acceleration I need to save the data for a period of 30 sec. One solution I tried is to increment the counter according to the time in second that corresponds each minute but I cannot put it in the wakeUpNow function since only fast processing should be done there (it is the function executed by the interrupt).
Thank you for your time !

what else does the sketch? why do you need RTC as timer? you can do something every 30 min with millis()

why would you need two interrupts for an action that occurs every 30 minutes?

Hello, I need a RTC as timer because the data logger will be an embedded system and the consumption is a key factor for this project. Thus, I cannot let it run with a simple millis() because I am powering down the card all the time and wake it up with alarm interrupt of DS3231.

Consider it is good practice to leave as little code in an interrupt as possible.  In this case, as the time in question is much greater then how often the loop() function is called, setting a single flag indicating an interrupt occurred would be sufficient.
Each time in your loop() function test for this flag.  If TRUE, immediately set it to FALSE so as not forget to do so later.  Next, while still in the scope of the IF statement, assuming the RTC DS3231 has been configured to create an interrupt every 30 minutes, take your "30 minute sample".  Next, while still in the scope of the IF statement, increment a counter.  Make sure this counter is defined as a global variable in order to persist between calls to the loop() function.  If this counter has reached the value of 2, clear this counter to zero and take your "60 minute sample".
Consider the following pseudo code:
// Global variable defined outside of functions to track state & time.
bool state_interrupts;
int counter_interrupts;

void setup()
{
  // Initialize RTC to generate an interrupt once every 30 minutes.
  <RTC initialization code.>
  // Call attach interrupt to identify interrupt function name "wakeUpNow".
  attachInterrupt(...)
  // Initialize state & counter;
  interrupts_state = false;
  interrupts_counter = 0;
}

void loop()
{
  // Check if interrupt occurred.
  if(interrupts_state)
  {
    interrupts_state = false;
    interrupts_counter++;
    // Sample 30 minute sensor.
    <Sample 30 minute sensor code.>
    if(interrupts_counter >= 2)
    {
      interrupts_counter = 0;
      // Sample 60 minute sensor.
      <Sample 60 minute sensor code.>
    }
  }
}

void wakeUpNow()
{
    interrupts_state = true;
}

Hi and thank you for your answer. I already tried what you say but it doesn't work for my need. I need 2 different measures from 2 different sensors. One every 30 min et the other every hour. The thing is that there will be a conflict each hour between the calls since 30 min is half of an hour. So I cannot use a counter that way

@JamesONeil, are you sure you are not over thinking this?  Or is there a problem constraint that you have not told us about?  Yes, 2 different measurements from 2 sensors.  The interrupt DOES'NT SAMPLE ANYTHING!!! The interrupt only sets a flag indicating 30 minutes has passed.  YOUR CODE in the Loop function needs to keep track of the number of interrupts. Such that every time flag-set it samples the 30 minute sensor and every other time it samples the 60 minute sensor as well. REPEAT, the INTERRUPT DOES NOT SAMPLE ANY SENSORS!!! It's good practice to do as little as possible in interrupts!

@JamesONeil, since you made the effort to edit / improve your question, I added pseudo code to my answer to make the proposed solution easier to understand.

Hi ! Sorry for late answer I managed to have it worked the way I wanted :) Thank you for your pseudo code it is a good way also to solve my problem !

"Initialize RTC to generate an interrupt once every 30 minutes." doesn't look like it is an option from the A2M2 bit's table in  https://datasheets.maximintegrated.com/en/ds/DS3231.pdf  -- It looks like you can only trigger every minute or when the minutes match a particular number.  If you choose '30' you'll miss '00' & vice versa.  You could trigger each minute and count minutes interruptState = (++elapsedMinutes % 30 == 0);, or read the minutes and act accordingly.

The typical solution to this type of problem is:

In the ISR set a flag to indicate an RTC interrupt has occurred.
In the loop check the flag and if the set:
a) Read the alarm flags from the RTC,
b) Check the alarms, return value from LatchAlarmsTriggeredFlags(), and do the appropriate action.

What you should not do is write a lot of code in the ISR. Especially code that is also interrupt driven or depend on interrupts such as Wire, Serial, millis(), delay(), etc.
Cheers!

After the interrupt read the time from the RTC and then do the correct thing based on the result.

Try this:
bool x = true; This is before setup
bool done = false;
x = !x;  Do this in the interrupt function.
done = true;
this is in your loop!
if done == true{    // Determine if you have been here before
if X !true do both functions
else
do the 30 second one
I use the !X as it is faster.
done = false;}
This should give you an idea, I did not actually code this but i use code similar to this all the time. My goal is to do no more then 1 or two instructions in the interrupt service routine (ISR).


text:
How to use C Libraries for Arduino code
I have a code in Visual studio that I want to implement in Arduino. But
there is a problem. Many libraries usable in Visual Studio aren't usable
in the Arduino IDE. How can I use them in my Arduino code? To be
precise, the libraries I want to use are
#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include <math.h>
#include <ctime>

I know I have <iostream> available in Arduino. <math.h> is also
available I think along with the <string> library. The main problem is
to how to use #include<queue> and its functions such as
priority_queue() and other functions of iosteam like .pop()? Any
help would be highly appreciated.

Weird, I can remember using the same ATMega328P with another compiler, where it did work.
Though you can check if an external library fits your needs: http://playground.arduino.cc/Code/QueueList
But I would understand if you prefer a solution rather than a work-around?

The Arduino doesn't have an operating system, so therefore doesn't have a C standard library.

@mypal125: The Arduino does have a standard C library (avr-libc for the AVR based boards). What it lacks is the C++ STL, but see Nick Gammon's answer on how to add it.

@EdgarBonet: TIL!

Some of the libraries you mention (iomanip, iostream, queue, string) are part of the STL - Standard Template Library. Whilst it does not ship with the IDE download, you can install it yourself. See The Standard Template Library (STL) for AVR with C++ streams - a port to the Arduino by Andy Brown.
I have some implementation notes here.
Be aware that a lot of Arduinos don't have much RAM, so you need to use libraries like this with caution.
Library download: AVR standard template library, version 1.1.1 - 197.29 kB

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
How to count the number of times a changing variable reaches a peak value
I am recording a varying distance with an Ultrasonic sensor and Arduino but my problem is I want to display the number of times this variable reaches peak (at an instance the value will be increasing and the next instance decreasing in that order) on an LCD.
So far this is my code:
//Libraries
#include <Ultrasonic.h>
#include <LiquidCrystal.h>

//Setup connection with LCD and HC-SR04
LiquidCrystal lcd(12, 10, 5, 4, 3, 2);
Ultrasonic ultrasonic(A0,A1);

void setup() {
  //Lcd init
  lcd.begin(16, 2); //16 rows, 2 columns
}

void loop()
{
  lcd.clear();
  lcd.print("  Ardumotive ;)  "); // You can change this message.
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");
  lcd.print(ultrasonic.Ranging(CM));
  lcd.print("cm");

  delay(300); // 1sec delay
}

So far this just looks like a copy-paste code from an example for the Ultrasonic sensor. You need to show us that you've thought about how you're going to achieve your goal.

The counting portion of what you want to achieve is simple - you just increment a variable.  The hard part is knowing when to count.
You need to remember what your value has been doing in the past in order to know if you have had a peak or not.  Also you have to define exactly what a peak is.
The simplest method (I'm not going to spoon feed you code, just help you understand the concepts) is to set a couple of thresholds, and maintain a flag.
You decide upon a value "above which the value enters the peak zone", and another "below which the value leaves the peak zone".  They could be the same value, but don't need to be.
You then have a flag which remembers if you are in the peak zone or not.
If you're not currently in the peak zone and the new value is above the peak zone entry threshold then you have entered the peak zone.  Set the flag to remember that fact.
If you are currently in the peak zone and the new value is below the peak zone leaving threshold then you have just left the peak zone.  Clear the flag to remember the fact that you're not in the peak zone and increment your peak counter now you have left the peak.

It can be very good to have the leaving threshold lower than the entering threshold so that the value has to drop significantly out of the peak zone before it will register another peak.  This is known as hysteresis and is great for getting rid of the noise you may get if the value is hovering around the peak zone entry threshold.

If ((current distance - previous distance) < 0)
Max = previous distance;
Current distance = previous distance;

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
Call a class method from inside the same class
If i have file cSpeedOfSound.h:
#ifndef cSpeedOfSound_h
#define cSpeedOfSound_h
#include "Arduino.h"
#include "math.h"
class cSpeedOfSound{
    public:
        cSpeedOfSound(float *i,float *C);
    private:
        float *C;
        float *a;
        float *i;
    };
#endif

and file cSpeedOfSound.cpp:
#include "cSpeedOfSound.h"
cSpeedOfSound::cSpeedOfSound(float *i,float *C){
    //some code...
    *C =roundto(*C,2);}
float cSpeedOfSound::roundto(float x,float dp){
return (round(x*pow(10., dp)) / pow(10., dp));}

where i want to run function roundto inside class, but it doesnt seems to work. So what im making wrong and how to do it properly?
Ps: Im begginer to arduino (want it to learn robotics + C language) and know python (learnd for about 1+ year), so pls do not hate me if i made crucial mistakes.

In this case you could simply write roundto() as a plain function. Declare it as static to make it visible only from inside cSpeedOfSound.cpp. That's the C way of encapsulation.

You need to include roundto() in your class declaration, e.g. something like this:
class cSpeedOfSound {
public:
    cSpeedOfSound(float *i,float *C);

private:
    float roundto(float x, float dp);

    float *C;
    float *a;
    float *i;
};

In C++, class structures are fixed. You can define the body of a function almost anywhere, but all member functions/data must be specified in the original declaration.

As a side note, it's worth pointing out that you have a naming conflict in your code. You have member variables called i and C, and you have totally separate function parameters called i and C. The compiler won't complain about this (the function parameters will simply mask the member variables), but it may not do what you expect.


text:
Locally declared variable takes up global variable space in dynamic memory/SRAM
I'm trying to make my Arduino Uno control the air conditioner by recording the raw IR signal of several of the AC remotes temperatures using AnalysisIR. However these IR codes are quite long (array with 343 items).
Declaring more than one IR code in a variable shows me the error:
"Global variables use 2064 bytes (100%) of dynamic memory, leaving -16 bytes for local variables. Maximum is 2048 bytes."
However, as seen in my code, I have declared the IR codes locally in a separate function to send them to the air conditioner. I don't understand why i get the error saying the global variables are taking up too much dynamic memory.
Isn't the space taken up by the local variable supposed to be freed up from the SRAM once the functions runs because I have declared the variable locally?
Thanks in advance.
Code:
#include <IRremote.h>

IRsend irsend;

int khz = 38;

void send23()
{

  unsigned int irSignal[]= {9048, 4496, 620, 1636, 620, 1636, 620, 520, 620, 520, 620, 524, 624, 524, 620, 532, 620, 1648, 620, 508, 620, 1640, 620, 1644, 620, 520, 620, 528, 616, 528, 620, 532, 624, 516, 620, 1636, 620, 516, 616, 520, 620, 520, 620, 524, 620, 528, 616, 536, 620, 520, 620, 508, 620, 1640, 620, 520, 620, 520, 620, 1648, 620, 1656, 620, 1656, 616, 524, 620, 508, 620, 516, 616, 520, 620, 520, 624, 520, 620, 528, 620, 532, 620, 520, 620, 508, 624, 508, 624, 516, 616, 520, 624, 524, 620, 524, 620, 532, 620, 504, 620, 7956, 616, 512, 620, 1640, 616, 520, 620, 520, 620, 524, 620, 528, 620, 528, 624, 1644, 620, 1636, 616, 1640, 624, 1640, 620, 1648, 616, 528, 620, 1652, 620, 532, 620, 520, 620, 512, 620, 512, 620, 516, 620, 524, 620, 524, 616, 528, 620, 532, 620, 520, 620, 508, 620, 512, 624, 512, 620, 520, 620, 520, 620, 528, 620, 532, 620, 520, 620, 508, 620, 516, 620, 516, 620, 520, 620, 524, 620, 524, 624, 528, 624, 516, 620, 508, 620, 512, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 620, 520, 624, 504, 624, 512, 620, 516, 624, 516, 620, 524, 620, 524, 624, 528, 620, 520, 624, 504, 624, 1636, 624, 1640, 624, 1640, 624, 1648, 620, 524, 624, 1656, 624, 1624, 624, 7952, 616, 512, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 624, 520, 624, 504, 624, 1636, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 628, 516, 624, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 620, 528, 628, 512, 624, 508, 624, 508, 624, 512, 620, 520, 620, 524, 624, 520, 624, 528, 624, 516, 620, 508, 624, 508, 624, 512, 624, 1640, 624, 520, 624, 524, 624, 528, 624, 512, 628, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 624, 516, 620, 508, 624, 1632, 628, 508, 628, 1640, 624, 520, 624, 524, 620, 528, 624, 500, 608};
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void send22()
{

  unsigned int irSignal[]=  {8988, 4548, 572, 1688, 572, 1688, 600, 532, 572, 568, 572, 572, 576, 572, 572, 580, 572, 1724, 544, 556, 576, 1684, 600, 1660, 608, 532, 604, 540, 608, 540, 600, 548, 608, 536, 604, 1652, 608, 520, 616, 524, 608, 532, 612, 532, 612, 532, 620, 532, 616, 524, 620, 512, 616, 1644, 624, 508, 628, 516, 624, 520, 628, 1676, 596, 1648, 632, 508, 632, 500, 632, 500, 632, 504, 636, 504, 636, 512, 628, 516, 632, 524, 628, 508, 636, 496, 632, 504, 628, 508, 632, 508, 632, 512, 632, 516, 632, 516, 632, 496, 628, 7948, 628, 500, 632, 1656, 600, 508, 628, 508, 632, 512, 632, 516, 632, 524, 628, 1636, 632, 496, 632, 504, 632, 504, 632, 508, 632, 1672, 600, 1672, 600, 520, 632, 508, 632, 500, 652, 480, 632, 504, 632, 512, 628, 512, 632, 516, 632, 520, 632, 508, 632, 496, 632, 504, 632, 504, 632, 508, 632, 516, 628, 520, 628, 520, 656, 484, 632, 500, 656, 476, 656, 480, 632, 508, 632, 516, 632, 512, 656, 496, 632, 508, 632, 500, 652, 480, 632, 504, 656, 484, 656, 488, 632, 516, 632, 520, 632, 508, 632, 500, 656, 476, 632, 508, 656, 484, 656, 488, 656, 492, 632, 520, 656, 484, 656, 1632, 624, 476, 632, 508, 656, 484, 656, 1620, 652, 488, 656, 1656, 600, 1648, 604, 7948, 656, 472, 656, 476, 660, 480, 632, 508, 656, 488, 652, 496, 656, 492, 656, 484, 632, 500, 656, 1608, 628, 504, 656, 484, 656, 488, 656, 492, 656, 496, 656, 484, 632, 496, 656, 480, 632, 504, 656, 484, 656, 488, 632, 516, 656, 496, 628, 508, 660, 472, 656, 480, 656, 480, 652, 488, 656, 488, 656, 492, 652, 496, 660, 484, 656, 472, 632, 504, 652, 484, 652, 1644, 624, 488, 656, 492, 656, 492, 632, 512, 628, 500, 664, 468, 656, 480, 656, 488, 652, 488, 656, 492, 656, 492, 632, 512, 652, 476, 656, 1632, 624, 484, 652, 1644, 620, 492, 656, 488, 628, 524, 652, 472, 608};
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void setup()
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  delay(2000);
  send23();
  delay(2000);
  send22();
}

void loop() {

}

EDIT 05/07:
Here is my new code with the use of PROGMEM.
As you can see, i have initialized the progmem arrays globally at the top of the program. When i do this, my SRAM is not full anymore but sendraw in the functions does not work anymore.
Weirdly however, when i declare the progmem variable INSIDE the send23() function (IN COMMENTS), the sendraw works! But the variable takes up space in the SRAM again and it says that Global variables have used 100% of dynamic memory.
Why does this happen and what can i do to work around it?
PROGMEM updated code:
#include <IRremote.h>

const unsigned int irSignal23[] PROGMEM = {9048, 4496, 620, 1636, 620, 1636, 620, 520, 620, 520, 620, 524, 624, 524, 620, 532, 620, 1648, 620, 508, 620, 1640, 620, 1644, 620, 520, 620, 528, 616, 528, 620, 532, 624, 516, 620, 1636, 620, 516, 616, 520, 620, 520, 620, 524, 620, 528, 616, 536, 620, 520, 620, 508, 620, 1640, 620, 520, 620, 520, 620, 1648, 620, 1656, 620, 1656, 616, 524, 620, 508, 620, 516, 616, 520, 620, 520, 624, 520, 620, 528, 620, 532, 620, 520, 620, 508, 624, 508, 624, 516, 616, 520, 624, 524, 620, 524, 620, 532, 620, 504, 620, 7956, 616, 512, 620, 1640, 616, 520, 620, 520, 620, 524, 620, 528, 620, 528, 624, 1644, 620, 1636, 616, 1640, 624, 1640, 620, 1648, 616, 528, 620, 1652, 620, 532, 620, 520, 620, 512, 620, 512, 620, 516, 620, 524, 620, 524, 616, 528, 620, 532, 620, 520, 620, 508, 620, 512, 624, 512, 620, 520, 620, 520, 620, 528, 620, 532, 620, 520, 620, 508, 620, 516, 620, 516, 620, 520, 620, 524, 620, 524, 624, 528, 624, 516, 620, 508, 620, 512, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 620, 520, 624, 504, 624, 512, 620, 516, 624, 516, 620, 524, 620, 524, 624, 528, 620, 520, 624, 504, 624, 1636, 624, 1640, 624, 1640, 624, 1648, 620, 524, 624, 1656, 624, 1624, 624, 7952, 616, 512, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 624, 520, 624, 504, 624, 1636, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 628, 516, 624, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 620, 528, 628, 512, 624, 508, 624, 508, 624, 512, 620, 520, 620, 524, 624, 520, 624, 528, 624, 516, 620, 508, 624, 508, 624, 512, 624, 1640, 624, 520, 624, 524, 624, 528, 624, 512, 628, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 624, 516, 620, 508, 624, 1632, 628, 508, 628, 1640, 624, 520, 624, 524, 620, 528, 624, 500, 608};
const unsigned int irSignal22[] PROGMEM = {8988, 4548, 572, 1688, 572, 1688, 600, 532, 572, 568, 572, 572, 576, 572, 572, 580, 572, 1724, 544, 556, 576, 1684, 600, 1660, 608, 532, 604, 540, 608, 540, 600, 548, 608, 536, 604, 1652, 608, 520, 616, 524, 608, 532, 612, 532, 612, 532, 620, 532, 616, 524, 620, 512, 616, 1644, 624, 508, 628, 516, 624, 520, 628, 1676, 596, 1648, 632, 508, 632, 500, 632, 500, 632, 504, 636, 504, 636, 512, 628, 516, 632, 524, 628, 508, 636, 496, 632, 504, 628, 508, 632, 508, 632, 512, 632, 516, 632, 516, 632, 496, 628, 7948, 628, 500, 632, 1656, 600, 508, 628, 508, 632, 512, 632, 516, 632, 524, 628, 1636, 632, 496, 632, 504, 632, 504, 632, 508, 632, 1672, 600, 1672, 600, 520, 632, 508, 632, 500, 652, 480, 632, 504, 632, 512, 628, 512, 632, 516, 632, 520, 632, 508, 632, 496, 632, 504, 632, 504, 632, 508, 632, 516, 628, 520, 628, 520, 656, 484, 632, 500, 656, 476, 656, 480, 632, 508, 632, 516, 632, 512, 656, 496, 632, 508, 632, 500, 652, 480, 632, 504, 656, 484, 656, 488, 632, 516, 632, 520, 632, 508, 632, 500, 656, 476, 632, 508, 656, 484, 656, 488, 656, 492, 632, 520, 656, 484, 656, 1632, 624, 476, 632, 508, 656, 484, 656, 1620, 652, 488, 656, 1656, 600, 1648, 604, 7948, 656, 472, 656, 476, 660, 480, 632, 508, 656, 488, 652, 496, 656, 492, 656, 484, 632, 500, 656, 1608, 628, 504, 656, 484, 656, 488, 656, 492, 656, 496, 656, 484, 632, 496, 656, 480, 632, 504, 656, 484, 656, 488, 632, 516, 656, 496, 628, 508, 660, 472, 656, 480, 656, 480, 652, 488, 656, 488, 656, 492, 652, 496, 660, 484, 656, 472, 632, 504, 652, 484, 652, 1644, 624, 488, 656, 492, 656, 492, 632, 512, 628, 500, 664, 468, 656, 480, 656, 488, 652, 488, 656, 492, 656, 492, 632, 512, 652, 476, 656, 1632, 624, 484, 652, 1644, 620, 492, 656, 488, 628, 524, 652, 472, 608};
const unsigned int irSignal21[] PROGMEM = {9044, 4496, 624, 1628, 628, 1592, 668, 508, 628, 512, 628, 516, 628, 520, 628, 524, 628, 1636, 632, 500, 628, 1632, 624, 1636, 628, 512, 628, 520, 628, 516, 628, 524, 628, 512, 628, 1628, 628, 512, 620, 512, 624, 516, 628, 516, 628, 520, 628, 524, 628, 512, 628, 500, 632, 1628, 628, 512, 628, 512, 628, 1644, 628, 520, 628, 1648, 628, 512, 628, 504, 624, 508, 628, 508, 632, 508, 628, 516, 632, 516, 628, 524, 628, 512, 628, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 500, 624, 7948, 628, 504, 628, 1628, 628, 512, 624, 516, 624, 516, 628, 520, 628, 524, 628, 1636, 628, 504, 624, 1632, 628, 508, 628, 512, 628, 1640, 628, 1648, 628, 520, 628, 512, 628, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 516, 628, 524, 628, 512, 628, 500, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 500, 632, 504, 628, 508, 628, 512, 624, 520, 624, 520, 628, 524, 624, 516, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 504, 628, 504, 628, 508, 624, 516, 628, 512, 628, 520, 628, 524, 624, 516, 628, 1624, 628, 1632, 628, 508, 628, 512, 628, 516, 628, 1644, 628, 1648, 628, 1620, 628, 7944, 628, 500, 628, 504, 628, 512, 624, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 504, 624, 1636, 624, 508, 632, 508, 628, 516, 628, 520, 628, 520, 632, 512, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 516, 628, 500, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 512, 628, 504, 624, 508, 628, 508, 628, 1640, 624, 516, 628, 520, 628, 524, 624, 516, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 512, 628, 504, 628, 1632, 624, 512, 628, 1640, 624, 516, 628, 520, 628, 524, 628, 492, 612};

IRsend irsend;

int khz = 38;

void send23()
{

//  const unsigned int irSignal23[] PROGMEM = {9048, 4496, 620, 1636, 620, 1636, 620, 520, 620, 520, 620, 524, 624, 524, 620, 532, 620, 1648, 620, 508, 620, 1640, 620, 1644, 620, 520, 620, 528, 616, 528, 620, 532, 624, 516, 620, 1636, 620, 516, 616, 520, 620, 520, 620, 524, 620, 528, 616, 536, 620, 520, 620, 508, 620, 1640, 620, 520, 620, 520, 620, 1648, 620, 1656, 620, 1656, 616, 524, 620, 508, 620, 516, 616, 520, 620, 520, 624, 520, 620, 528, 620, 532, 620, 520, 620, 508, 624, 508, 624, 516, 616, 520, 624, 524, 620, 524, 620, 532, 620, 504, 620, 7956, 616, 512, 620, 1640, 616, 520, 620, 520, 620, 524, 620, 528, 620, 528, 624, 1644, 620, 1636, 616, 1640, 624, 1640, 620, 1648, 616, 528, 620, 1652, 620, 532, 620, 520, 620, 512, 620, 512, 620, 516, 620, 524, 620, 524, 616, 528, 620, 532, 620, 520, 620, 508, 620, 512, 624, 512, 620, 520, 620, 520, 620, 528, 620, 532, 620, 520, 620, 508, 620, 516, 620, 516, 620, 520, 620, 524, 620, 524, 624, 528, 624, 516, 620, 508, 620, 512, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 620, 520, 624, 504, 624, 512, 620, 516, 624, 516, 620, 524, 620, 524, 624, 528, 620, 520, 624, 504, 624, 1636, 624, 1640, 624, 1640, 624, 1648, 620, 524, 624, 1656, 624, 1624, 624, 7952, 616, 512, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 624, 520, 624, 504, 624, 1636, 624, 512, 624, 516, 624, 520, 624, 524, 624, 524, 628, 516, 624, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 620, 528, 628, 512, 624, 508, 624, 508, 624, 512, 620, 520, 620, 524, 624, 520, 624, 528, 624, 516, 620, 508, 624, 508, 624, 512, 624, 1640, 624, 520, 624, 524, 624, 528, 624, 512, 628, 504, 624, 508, 624, 512, 624, 516, 624, 520, 624, 524, 624, 528, 624, 516, 620, 508, 624, 1632, 628, 508, 628, 1640, 624, 520, 624, 524, 620, 528, 624, 500, 608};
  irsend.sendRaw(irSignal23, sizeof(irSignal23) / sizeof(irSignal23[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void send22()
{
//  const unsigned int irSignal22[] PROGMEM = {8988, 4548, 572, 1688, 572, 1688, 600, 532, 572, 568, 572, 572, 576, 572, 572, 580, 572, 1724, 544, 556, 576, 1684, 600, 1660, 608, 532, 604, 540, 608, 540, 600, 548, 608, 536, 604, 1652, 608, 520, 616, 524, 608, 532, 612, 532, 612, 532, 620, 532, 616, 524, 620, 512, 616, 1644, 624, 508, 628, 516, 624, 520, 628, 1676, 596, 1648, 632, 508, 632, 500, 632, 500, 632, 504, 636, 504, 636, 512, 628, 516, 632, 524, 628, 508, 636, 496, 632, 504, 628, 508, 632, 508, 632, 512, 632, 516, 632, 516, 632, 496, 628, 7948, 628, 500, 632, 1656, 600, 508, 628, 508, 632, 512, 632, 516, 632, 524, 628, 1636, 632, 496, 632, 504, 632, 504, 632, 508, 632, 1672, 600, 1672, 600, 520, 632, 508, 632, 500, 652, 480, 632, 504, 632, 512, 628, 512, 632, 516, 632, 520, 632, 508, 632, 496, 632, 504, 632, 504, 632, 508, 632, 516, 628, 520, 628, 520, 656, 484, 632, 500, 656, 476, 656, 480, 632, 508, 632, 516, 632, 512, 656, 496, 632, 508, 632, 500, 652, 480, 632, 504, 656, 484, 656, 488, 632, 516, 632, 520, 632, 508, 632, 500, 656, 476, 632, 508, 656, 484, 656, 488, 656, 492, 632, 520, 656, 484, 656, 1632, 624, 476, 632, 508, 656, 484, 656, 1620, 652, 488, 656, 1656, 600, 1648, 604, 7948, 656, 472, 656, 476, 660, 480, 632, 508, 656, 488, 652, 496, 656, 492, 656, 484, 632, 500, 656, 1608, 628, 504, 656, 484, 656, 488, 656, 492, 656, 496, 656, 484, 632, 496, 656, 480, 632, 504, 656, 484, 656, 488, 632, 516, 656, 496, 628, 508, 660, 472, 656, 480, 656, 480, 652, 488, 656, 488, 656, 492, 652, 496, 660, 484, 656, 472, 632, 504, 652, 484, 652, 1644, 624, 488, 656, 492, 656, 492, 632, 512, 628, 500, 664, 468, 656, 480, 656, 488, 652, 488, 656, 492, 656, 492, 632, 512, 652, 476, 656, 1632, 624, 484, 652, 1644, 620, 492, 656, 488, 628, 524, 652, 472, 608};
  irsend.sendRaw(irSignal22, sizeof(irSignal22) / sizeof(irSignal22[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void send21()
{
//  const unsigned int irSignal21[] PROGMEM = {9044, 4496, 624, 1628, 628, 1592, 668, 508, 628, 512, 628, 516, 628, 520, 628, 524, 628, 1636, 632, 500, 628, 1632, 624, 1636, 628, 512, 628, 520, 628, 516, 628, 524, 628, 512, 628, 1628, 628, 512, 620, 512, 624, 516, 628, 516, 628, 520, 628, 524, 628, 512, 628, 500, 632, 1628, 628, 512, 628, 512, 628, 1644, 628, 520, 628, 1648, 628, 512, 628, 504, 624, 508, 628, 508, 632, 508, 628, 516, 632, 516, 628, 524, 628, 512, 628, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 500, 624, 7948, 628, 504, 628, 1628, 628, 512, 624, 516, 624, 516, 628, 520, 628, 524, 628, 1636, 628, 504, 624, 1632, 628, 508, 628, 512, 628, 1640, 628, 1648, 628, 520, 628, 512, 628, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 516, 628, 524, 628, 512, 628, 500, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 500, 632, 504, 628, 508, 628, 512, 624, 520, 624, 520, 628, 524, 624, 516, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 504, 628, 504, 628, 508, 624, 516, 628, 512, 628, 520, 628, 524, 624, 516, 628, 1624, 628, 1632, 628, 508, 628, 512, 628, 516, 628, 1644, 628, 1648, 628, 1620, 628, 7944, 628, 500, 628, 504, 628, 512, 624, 512, 628, 516, 628, 520, 628, 520, 628, 512, 628, 504, 624, 1636, 624, 508, 632, 508, 628, 516, 628, 520, 628, 520, 632, 512, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 516, 628, 500, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 512, 628, 504, 624, 508, 628, 508, 628, 1640, 624, 516, 628, 520, 628, 524, 624, 516, 624, 504, 628, 504, 628, 508, 628, 512, 628, 516, 628, 520, 628, 524, 624, 512, 628, 504, 628, 1632, 624, 512, 628, 1640, 624, 516, 628, 520, 628, 524, 628, 492, 612};
  irsend.sendRaw(irSignal21, sizeof(irSignal21) / sizeof(irSignal21[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  send23();
  Serial.println("done");
  delay(4000);
  send22();
  Serial.println("done");
  delay(4000);
  send21();
  Serial.println("done");
}

void loop() {
}

The compiler can probably not be fooled that easy. I don't know how the compiler deals with it, perhaps someone else knows the answer. Do you really need integers? or is is a few different timings that could be put into a byte? I think you need an arduino board with more memory. Arrays of constant data belong in progmem: https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

use PROGMEM https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

I tried using PROGMEM as well. However, if the const PROGMEM variable is declared inside the 'loop' function, it still takes up space in the SRAM. If the const PROGMEM variable is declared outside any function (globally), it no longer takes up space in the SRAM but the irRemote "sendRaw function no longer works."

make the PROGMEM array global and copy it to RAM in the function

Can you point me on how to copy the global PROGMEM array into my RAM during the functions execution?

memArr[i] = pgm_read_word_near(progmemArr[i]);

Isn't the space taken up by the local variable supposed to be freed up
  from the SRAM once the functions runs because I have declared the
  variable locally?

This is correct. The local arrays you have take up RAM only while the
corresponding function is executing. They do not consume any static RAM
(i.e. .data and .bss, what the Arduino IDE improperly calls “Global
variables”).
The problem is not the arrays themselves, but rather the data used to
initialize them. As an experiment, you can try to change the array
definitions as follows:
unsigned int irSignal[343];

You will see the memory consumption reported by the IDE drop by
4×343 bytes. The local arrays are still there. They use RAM (from
the stack) when the functions execute. Only you have removed the
matching initializers.
I disassembled a slightly modified copy of your program just to see how
the compiler handles it. I will not post the assembly code here, as I
guess not many people here are fluent in AVR assembly. Instead, I will
attempt to translate the assembly back to C in the most straightforward
way. The compiler interpreted the function send23() as follows:
static const unsigned int anonymous_array_1[343] = {9048, ...};

void send23()
{
    unsigned int irSignal[343];
    for (int i = 0; i < 343; i++)
        irSignal[i] = anonymous_array_1[i];
    irsend.sendRaw(irSignal, 343, khz);
}

What is taking up static RAM is not the local arrays, it's the global
anonymous arrays that hold the initialization data. You may notice that
making the arrays local has actually worsened the problem: while
send23() executes, you have two copies of the data in RAM.
The solution, as Jot and Juraj said in comments, is to put the arrays in
PROGMEM. If it doesn't work, then you have a bug. You should fix the
bug instead of trying to avoid PROGMEM.

Yes I have tried the progmem approach. Please see my edit on the post. I am guessing what i need to do now is to figure out how to copy the progmem variable into ram when the function runs? Perhaps that is why the send raw function is not working.

This is now very different from your original question. You should ask it as a new question.

You must declare the array in global space or static, and make sure the function you pass the buffer pointer to knows it is in PROGMEM.
void send22()
{

  static unsigned int irSignal[] PROGMEM=  {8988, 4548, 572, 1688, 572, 1688, 600, 532, 572, 568, 572, 572, 576, 572, 572, 580, 572, 1724, 544, 556, 576, 1684, 600, 1660, 608, 532, 604, 540, 608, 540, 600, 548, 608, 536, 604, 1652, 608, 520, 616, 524, 608, 532, 612, 532, 612, 532, 620, 532, 616, 524, 620, 512, 616, 1644, 624, 508, 628, 516, 624, 520, 628, 1676, 596, 1648, 632, 508, 632, 500, 632, 500, 632, 504, 636, 504, 636, 512, 628, 516, 632, 524, 628, 508, 636, 496, 632, 504, 628, 508, 632, 508, 632, 512, 632, 516, 632, 516, 632, 496, 628, 7948, 628, 500, 632, 1656, 600, 508, 628, 508, 632, 512, 632, 516, 632, 524, 628, 1636, 632, 496, 632, 504, 632, 504, 632, 508, 632, 1672, 600, 1672, 600, 520, 632, 508, 632, 500, 652, 480, 632, 504, 632, 512, 628, 512, 632, 516, 632, 520, 632, 508, 632, 496, 632, 504, 632, 504, 632, 508, 632, 516, 628, 520, 628, 520, 656, 484, 632, 500, 656, 476, 656, 480, 632, 508, 632, 516, 632, 512, 656, 496, 632, 508, 632, 500, 652, 480, 632, 504, 656, 484, 656, 488, 632, 516, 632, 520, 632, 508, 632, 500, 656, 476, 632, 508, 656, 484, 656, 488, 656, 492, 632, 520, 656, 484, 656, 1632, 624, 476, 632, 508, 656, 484, 656, 1620, 652, 488, 656, 1656, 600, 1648, 604, 7948, 656, 472, 656, 476, 660, 480, 632, 508, 656, 488, 652, 496, 656, 492, 656, 484, 632, 500, 656, 1608, 628, 504, 656, 484, 656, 488, 656, 492, 656, 496, 656, 484, 632, 496, 656, 480, 632, 504, 656, 484, 656, 488, 632, 516, 656, 496, 628, 508, 660, 472, 656, 480, 656, 480, 652, 488, 656, 488, 656, 492, 652, 496, 660, 484, 656, 472, 632, 504, 652, 484, 652, 1644, 624, 488, 656, 492, 656, 492, 632, 512, 628, 500, 664, 468, 656, 480, 656, 488, 652, 488, 656, 492, 656, 492, 632, 512, 652, 476, 656, 1632, 624, 484, 652, 1644, 620, 492, 656, 488, 628, 524, 652, 472, 608};
  irsend.sendRaw_P(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz);
  // _P postfix is often used to mark functions taking in PROGMEM pointers
}

You can adjust the library to allow it to take PROGMEM buffers, with a quick search for the implementation of IRsend::sendRaw it gave me:
void  IRsend::sendRaw (const unsigned int buf[],  unsigned int len,  unsigned int hz)
{
    // Set IR carrier frequency
    enableIROut(hz);

    for (unsigned int i = 0;  i < len;  i++) {
        if (i & 1)  space(buf[i]) ;
        else        mark (buf[i]) ;
    }

    space(0);  // Always end with the LED off
}

To make it work with progmem you can create:
void  IRsend::sendRaw_P (const unsigned int buf[],  unsigned int len,  unsigned int hz)
{
    // Set IR carrier frequency
    enableIROut(hz);

    for (unsigned int i = 0;  i < len;  i++) {
        unsigned int word = pgm_read_word_near(buf + i);
        if (i & 1)  space(word) ;
        else        mark (word) ;
    }

    space(0);  // Always end with the LED off
}

How should i proceed to edit the existing IRremote library?


text:
How to calculate illuminance from the output of a TCS34725 color sensor?
I am using Arduino UNO with the sensor with the following programm.
#include <Wire.h>

// TCS34725 I2C address is 0x29(41)
#define Addr 0x29

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise Serial Communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Wait Time register
  Wire.write(0x83);
  // Set wait time = 2.4 ms
  Wire.write(0xFF);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Atime register
  Wire.write(0x81);
  // Atime = 700 ms, max count = 65536
  Wire.write(0x00);
  // Stop I2C Transmission on the device
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x8F);
  // AGAIN = 1x
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select enable register
  Wire.write(0x80);
  // Power ON, RGBC enable, wait time disable
  Wire.write(0x03);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(800);
}

void loop()
{
  unsigned int data[8];

  // Start I2C Transmission on the device
  Wire.beginTransmission(Addr);
  // Select data register
  Wire.write(0x94);
  // Stop I2C Transmission on the device
  Wire.endTransmission();

  // Request 8 byte of data from the device
  Wire.requestFrom(Addr, 8);

  // Read 8 bytes of data
  // cData lsb, cData msb, red lsb, red msb, green lsb, green msb, blue lsb, blue msb
  if (Wire.available() == 8)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
    data[6] = Wire.read();
    data[7] = Wire.read();
  }

  // Convert the data
  float cData = (data[1] * 256.0) + data[0];
  float red = (data[3] * 256.0) + data[2];
  float green = (data[5] * 256.0) + data[4];
  float blue = (data[7] * 256.0) + data[6];

  // Calculate the final lux
  float luminance = (-0.32466 * red) + (1.57837 * green) + (-0.73191 * blue);

  // Output data to serial monitor
  Serial.print("Red color Luminance   : ");
  Serial.print(red);
  Serial.println(" lux");
  Serial.print("Green color Luminance : ");
  Serial.print(green);
  Serial.println(" lux");
  Serial.print("Blue color Luminance  : ");
  Serial.print(blue);
  Serial.println(" lux");
  Serial.print("IR Luminance          : ");
  Serial.print(cData);
  Serial.println(" lux");
  Serial.print("Ambient Light Luminance : ");
  Serial.print(luminance);
  Serial.println(" lux");
  delay(500);
}

Is the output really in lux or is it counts? If it's counts, how can I calculate the illuminance in lux?

The corresponding Adafruit tutorial states it is in Lux, but you are not using their library. Have you read the datasheet?

Are you using a laser or spectral lamp to shine monochromatic light (i.e. light with a well defined wavelength) onto your sensor? If not, what do you mean by “calculate wavelength”?

Hello Chirsl I am not going to use the sensor on arduino for my final application.This if for the initial test weather the sensor is working or not.But i need to know the calculation of lux or intensity from counts.

Hi Edger,Need to calculate lux or intensity form the counts

@user59381: Then talking about wavelength is a nonsense. And it's “illuminance”, not “intensity”. And it's “Edgar”, not “Edger”. I edited your question to fix all that.

The output of this program is a very wild guess at what the illuminance
may be. I would definitely no trust it. It uses the same formula as the
function Adafruit_TCS34725::calculateLux(), from the
Adafruit TCS34725 driver. Googling for the numeric
coefficients, I found that the likely source of this formula is the
application note Calculating Color Temperature and Illuminance using
the TAOS TCS3414CS Digital Color Sensor. That application note
seems serious, but it addresses a different model of sensor from the
one you have. It also gives a formula that does not depend on the gain
nor the integration time (maybe that's not adjustable on the TCS3414CS),
which makes absolutely no sense if you have a TCS34725. I found an
issue filed against the Adafruit driver that shows the formulas
used in the library are grossly wrong when compared against a
high-quality lux- and color-meter.
I suggest you try implementing the illuminance formula given in the
application note DN40 – Lux and CCT Calculations using ams Color
Sensors. This documents gives a generic formula with specific
coefficient matching different color sensor models, and it does give
coefficients specific for the TCS3472 family. Adafruit did use this
document to implement a color temperature calculation (function
Adafruit_TCS34725::calculateColorTemperature_dn40()), but they failed
to use it for the illuminance calculation.

float cData = (data[1] * 256.0) + data[0];I am having confussion on the above line. Is the line is for storing the data's in one variable or for coverting the data into RGB value.

float cData = (data[1] * 256.0) + data[0];I am having confussion on the above line. Is the line is for storing the data's in one variable or for coverting the data into RGB value

@user59381: It's for combining two bytes (data[0] and data[1]) into a single number.

OK.Thank you so much

In the chip's datasheet on page 8 in the chapter "RGBC Operation" (emphasis by me):

The RGBC engine contains RGBC gain control (AGAIN) and four
  integrating analog-to-digital converters (ADC)for the RGBC
  photodiodes. The RGBC integration time (ATIME) impacts both the
  resolution and the sensitivityof  the  RGBC  reading.  Integration  of
  all  four  channels  occurs  simultaneously  and  upon  completion  of
  theconversion cycle, the results are transferred to the color data
  registers. This data is also referred to as channelcount.

So, if you read the values from the data registers, you get simple count values, which were determined by the ADCs connected to the channels. To get a value in lux, you need to calculate that yourself.
Adafruit has a library for their breakout board with this chip. In their tutorial it seems, that the library outputs a value in lux. Maybe you can also use this library, or you can check, how they are converting the values from the sensor to lux values.

my end application is different.I need to know the calculation of lux or intensity of light from the counts

float cData = (data[1] * 256.0) + data[0];I am having confussion  on the above line. Is the line is for storing the data's in one variable or for coverting the data into RGB value.


text:
attachInterrupt HIGH
I'm trying to understand the implementation of attachInterrupt. For LOW, CHANGE, RISING and FALLING it seems simple enough; it sets some chip-specific registers and stores the given callback in intFunc, then invokes the callback from inside its own thin wrapper callback.
But the documentation says that on the Arduino Due, Zero and MKR1000, it can also accept HIGH. I can't find any reference to this in the code, and I can see that HIGH (= 0x01) and CHANGE (= 1) would be indistinguishable after preprocessing, so I can only assume a macro is involved somewhere, but I can't see one in the source.
How does the library implement this? Where is the code?

CHANGE is defined as 2 in the SAM core.

@EdgarBonet ah, thank you. That explains it. And I see that the SAM version also checks which value is given inside attachInterrupt, unlike the AVR version which just passes it though. I'd got stuck on the AVR version since github's search seems incapable of seeing the SAM core.

For a reason I don't know, they recently moved that core to a different repository.


text:
NodeMCU does not boot up when GPIO pin is high
I have a NodeMCU board connected to a door and there is a micro switch that makes one GPIO pin (GPIO15 - D8 in this specific case) go high when the door opens.
My problem is if I lose power while the door is opened, the GPIO15 is fed high at the next boot when power is restored. And it makes the board go to a not responding state. I have to boot it again while the door is closed (pin is low) to bring it back to work. I experienced this while flashing too. I cannot flash while this pin is high (while the door is open).
How can I fix this?

Certain pins of the ESP8266 perform special functions at bootup. GPIO15, when HIGH, makes the ESP8266 boot from an SD card.
You can read more about the special functions of the pins here.
The solution? Don't use GPIO 0, 2 or 15 for things that can affect the boot sequence.


text:
Getting Bluetooth addresses of connected devices via Arduino Uno?
I want to find all the Bluetooth addresses of connected devices using  an HC-05/HC-06 Bluetooth module and an Arduino. I can't find any code related to this. Basically I need the addresses to put in an array. Is it possible to do this with an HC-05 or 06?

This question isn't really about Arduino, and the goal expressed in your responses to answers of having someone supply the entire unspecified solution isn't really compatible with the specific-question model of the Stack Exchange sites.  Your city has several hackerspaces and similar creative communities, perhaps you would find it interesting to visit one of their open days and get a sense of how people create things like this, for themselves or as work projects.  Alternatively, to get help on this and related sites, questions need to be about a specific, singular on-topic goal.

The HC-05 has an AT command, AT+BIND?, that will tell you which slave device it is connected with currently. Since you can connect to one device only at the same time, your array is going to be a very short one.
AT+INQ will provide you with a list of nearby devices, to which it can connect.

can you please send me the entire code as i m new to arduino. It will be very helpful. Thank you.

@OliviaJohnson Anyone can send you the entire code and do the job for you, if you accept to hire them for this task. StackExchange is not a site for that: you need to demonstrate what you have tried so far.

You just have to send the above Bluetooth command via serial port to your Bluetooth module using print command.
For example yourserial.print("AT+BIND?"); OR yourserial.print("AT+INQ"); if you are using SoftwareSerial. Otherwise just use Serial.print("AT+BIND?"); OR Serial.print("AT+INQ"); when using Rx/Tx pins. That will do the job.

thanks. but can u also tell me which sensor should i use for this project

and also please provide the entire code


text:
Arduino Uno Sensor DHT11 Failed to read from DHT sensor
I get this error every time. I used the DHT tester file from this library. Which gives me an error.
And then I realized that in my code Arduino recognizes only the readTemperature function, not readHumidity, but it doesn't give any compile error. Also, when I read the sensor, this is the output:

Humidity Sensor Test
  Temp: 0C, Humidity: 0%
  Temp: 0C, Humidity: 0%
  Temp: 0C, Humidity: 0%

My code is here:
//DHT11 Sensor:
#include "DHT.h"
#define DHTPIN 5 // which digital pin we're connected to
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

//I2C LCD:
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>

// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
  Serial.begin(9600);
  delay(300);
  lcd.begin(16,2);
  Serial.println("Humidity Sensor Test");
  dht.begin();
  delay(300);
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  int t = dht.readTemperature();

  // set the cursor to (0,0):
  lcd.setCursor(0, 0);
  // print from 0 to 9:

  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print("C");

  // set the cursor to (16,1):
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");

  Serial.print("Temp: ");
  Serial.print(t);
  Serial.print("C, Humidity: ");
  Serial.print(h);
  Serial.println("%");
  delay(2500);
}

Also I have the 10k resistor between data and Vcc pins. Data is connected to digital pin 5. Has anyone had this problem before?

There are several versions of the DHT11 library for Arduino out there. This is the one that worked for me: https://github.com/adidax/dht11. Here is a simple sketch to test the sensor with the output going to the serial monitor.
// 5k ohm pull up resistor is used on the data line.
// Sensor operates at 5VDC.
// Data line connects to Arduino Uno pin 3.

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 3

// Sensor calibration. Start with 0 for each.
const int HumidityCorrection = -10;          // -10 worked with my sensor.
const int CelsiusTemperatureCorrection = 2;  // 2 worked with my sensor.

const float FahrenheitTemperatureCorrection = CelsiusTemperatureCorrection * 1.8;

// Update the serial display every 60 seconds.
const float UpdateSerialDisplay = 60000;

// Celsius to Fahrenheit conversion.
double Fahrenheit(double celsius){
  return 1.8 * celsius + 32;
}

void setup(){
  Serial.begin(9600);
  // Minimum wait for good data recommended is 1 second.
  delay(2000);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
}

void loop(){
  Serial.println("");
  int chk = DHT11.read(DHT11PIN);
  switch(chk){
    case DHTLIB_OK:
      //Serial.println("OK");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.println("Read sensor: Checksum error");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.println("Read sensor: Time out error");
      break;
    default:
      Serial.println("Read sensor: Unknown error");
      break;
  }

  Serial.print("Humidity: ");
  Serial.print("\t");
  Serial.print(DHT11.humidity + HumidityCorrection);
  Serial.println(" %");

  Serial.print("Temperature: ");
  Serial.print("\t");
  Serial.print(DHT11.temperature + CelsiusTemperatureCorrection);
  Serial.println(" C");

  Serial.print("Temperature: ");
  Serial.print("\t");
  Serial.print(round(Fahrenheit(DHT11.temperature) + FahrenheitTemperatureCorrection));
  Serial.println(" F");

  delay(UpdateSerialDisplay);
}

Once you get this working you can add the LCD code to it. Here is a link to the DHT 11 Humidity & Temperature Sensor manual / data sheet: http://www.micropik.com/PDF/dht11.pdf


text:
MPU6050 doesn't work
I am trying to do a self-balancing bot as a school project, but I got stuck with the MPU6050.
I tried using the code by Jeff Rowberg (MPU_6050_raw) but it's reading only zeros.
I tried the MPU_6050_DMP and got stuck at "initializing DMP..." and no reading is shown.
I tried other code, but same thing happens, either reading zeros or getting stuck at "initializing DMP..".
And how can I make sure that the chip isn't broken?

The MPU-6050 is a 3.3V chip. When you are using a 5V Arduino board, you should take care that you don't put current into the SDA and SCL pins of the MPU-6050.
Start with a i2c scanner.
This one: Arduino Playground I2C scanner
Or this one: Nick Gammon I2C scanner
If the I2C scanner halts, then there is a hardware problem with the I2C bus. It might be a shortcut of SDA or SCL to GND, or the MPU-6050 module is not powered.
If the I2C scanner is working, then you know the address (0x68 or 0x69). With that address you can try the Short example sketch to read data from the chip.
If that is working, then you can try the Jeff Rowberg library.

Thanks for replying.
the i2c scanner worked well here is what i got.
    I2C scanner. Scanning ...
    Found address: 104 (0x68)
    Done.
    Found 1 device(s).

but i still get zeros when i try the short examples.
    AcX = 0 | AcY = 0 | AcZ = 0 | Tmp = 36.53 | GyX = 0 | GyY = 0 | GyZ = 0
    AcX = 0 | AcY = 0 | AcZ = 0 | Tmp = 36.53 | GyX = 0 | GyY = 0 | GyZ = 0
    AcX = 0 | AcY = 0 | AcZ = 0 | Tmp = 36.53 | GyX = 0 | GyY = 0 | GyZ = 0

sorry i forgot to mention tham iam using arduino uno board

The temperature is not zero. It seems as if the accelerometer and gyro are not turned on. There are a number of possibilities: a broken sensor, or a bad I2C bus, or wrong voltages, or it is not the MPU-6050. Could you try the long example sketch http://playground.arduino.cc/Main/MPU-6050#sketch , that sketch checks for bus errors. Could you check with a magnifier if it is really a MPU-6050 ? It is a 3.3V sensor and the Arduino Uno is a 5V board. The MPU-6050 might be broken because of the wrong voltages. My suggestion is to buy a new MPU-6050 (or the newer MPU-9250) and a I2C level shifter.

thank you for being patient with me. here is what i got form the code   MPU-6ًInvenSense MPU-6050
June 2012
WHO_AM_I : 68, error = 0
PWR_MGMT_1 : 0, error = 0
MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 0, 0, 0
temperature: 36.506 degrees Celsius
gyro x,y,z : 0, 0, 0,
i use the 3.3 volt pin in the arduino i dont know what you mean that the uno is 5v board. and i consider buying the mpu 9250 .and iam sorry to ask another question. if you know how to test it at the shop and what to do to prevent it from getting burned like the last one. thanks again for your patience

error = 0 means there is no error, but the accelerator and gyro values are still zero. That is weird, and I have not heard of it before. Let's assume it is broken. The Arduino Uno has a ATmega328P microcontroller running at 5V. The MPU-6050 should have 3.3V to power it and also 3.3V signals at the I2C bus (SDA and SCL). You don't have to test it at the shop, it should be fine. It is safer and better to use a I2C level shifter to connect the Arduino Uno to a 3.3V sensor, or else use 4k7 pullup resistors to 3.3V: http://playground.arduino.cc/Main/I2CBi-directionalLevelShifter


text:
RFID+Fingerprint sensor code
I'm trying to create a security door system that uses an RFID and fingerprint sensor (Adafruit) to allow/deny access to a door. I have the fingerprint sensor and RFID hooked up to an LED, but the code isn't working very well. I want the LED to light only and only when both fingerprints and RFID are verified. Here is my preliminary code.
//HEADERFILES
#include <SPI.h> //rfid
#include <MFRC522.h>//rfid
#include <Adafruit_Fingerprint.h> //FINGERPRINT
#include <SoftwareSerial.h> //FINGERPRINT
#include<stdint.h> //FINGERPRINT

//rfid settings
#define RST_PIN         9          // rfid pin 9 RST_pin
#define SS_PIN          10         // rfid pin 10 SS_pin
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
String read_rfid;
String ok_rfid_1 = "cbb427b2"; //white card
String ok_rfid_2 = "0ebb14f39"; //blue key chain

//fingerprint settings
int getFingerprintIDez();
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (YELLOW wire)
SoftwareSerial mySerial(2, 3);
//activates serial communication on pin 2 & 3
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

//output garage door pin
int door = 7; //motor to door

void dump_byte_array(byte * buffer , byte bufferSize) { //why so important
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

void setup() {
  //RFID setup
  Serial.begin(9600);
  // Initialize serial communications with the PC
  while (!Serial);
  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  finger.begin(57600);
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 RFID
  pinMode (door, OUTPUT);
  //FINGERPRINT setup
  {
    while (!Serial);  // For Yun/Leo/Micro/Zero/...
    if (finger.verifyPassword()) {
      Serial.println("Found fingerprint sensor!");
    } else {
      Serial.println("Did not find fingerprint sensor :(");
      while (1);
    }
    Serial.println("Waiting for valid finger...");
  }

}

// OK success!

p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
  Serial.println("Image converted");
  break;
case FINGERPRINT_IMAGEMESS:
  Serial.println("Image too messy");
  return p;
case FINGERPRINT_PACKETRECIEVEERR:
  Serial.println("Communication error");
  return p;
case FINGERPRINT_FEATUREFAIL:
  Serial.println("Could not find fingerprint features");
  return p;
case FINGERPRINT_INVALIDIMAGE:
  Serial.println("Could not find fingerprint features");
  return p;
default:
  Serial.println("Unknown error");
  return p;
}
}

//DOOR/OUTPUT PIN PROPERTIES CHANGE TO REFLECT HOW FAST AND HOW LONG MOTOR     PULLS DOOR USING DIGITALWRITE; BREAK..   //USING LED FOR NOW

void open_door() {
  digitalWrite (door, HIGH);
  delay (2000);
  digitalWrite (door, LOW);
}

void loop() {
  // RFID LOOP SETTINGS                                                   `
  if ( ! mfrc522.PICC_IsNewCardPresent()) {     // Look for new cards
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {       // Select one of the c ards
    return;
  }
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);
  // FINGERPRINT LOOP SETTINGS

  getFingerprintID();  // start fingerprint sensor
  delay(100);

  uint8_t getFingerprintID() {
    uint8_t p = finger.getImage();
    switch (p) {
      case FINGERPRINT_OK:
        Serial.println("Image taken");
        break;
      case FINGERPRINT_NOFINGER:
        Serial.println("No finger detected");
        return p;
      case FINGERPRINT_PACKETRECIEVEERR:
        Serial.println("Communication error");
        return p;
      case FINGERPRINT_IMAGEFAIL:
        Serial.println("Imaging error");
        return p;
      default:
        Serial.println("Unknown error");
        return p;
    }

    p = finger.fingerFastSearch();
    if (p == FINGERPRINT_OK && read_rfid == ok_rfid_1) {
      Serial.println(ok_rfid_1);
      open_door();
    }

  }

And this is the error I am getting
Arduino: 1.8.1 (Windows 10), Board: "Arduino/Genuino Uno"

fingerprint:127: error: expected unqualified-id before '{' token

 {

 ^

exit status 1
expected unqualified-id before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I am new to this so please bear with me and explain in childish manner if possible.

Welcome to Arduino SE.  Please take the tour at https://arduino.stackexchange.com/Tour.    And btw, when you put in code, just make sure you have one blank line at the top then enter the code with 4 spaces at the beginning of each line.  It is much more readable that way.

Looks like your switch statement is outside a function declaration.

Can't say I have read what you posted here or what you're trying to do, however nonetheless I have fixed the errors in your post. This will compile no problem. Good luck with your project.
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
int getFingerprintIDez();
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

//HEADERFILES
#include <SPI.h> //rfid
#include <MFRC522.h>//rfid
#include<stdint.h> //FINGERPRINT

//RFID settings
#define RST_PIN         9          // RFID pin 9 RST_pin
#define SS_PIN          10         // RFID pin 10 SS_pin
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
String read_rfid;
String ok_rfid_1 = "cbb427b2"; //white card
String ok_rfid_2 = "0ebb14f39"; //blue key chain

int door = 7; //motor to door

void dump_byte_array(byte * buffer , byte bufferSize) {
  //why so important
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

void setup() {
  //RFID setup
  finger.begin(57600);
  Serial.begin(9600);
  // Initialize serial communications with the PC
  while (!Serial);
  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 RFID
  pinMode (door, OUTPUT);
  //FINGERPRINT setup
  {
    while (!Serial);  // For Yun/Leo/Micro/Zero/...
    if (finger.verifyPassword()) {
      Serial.println("Found fingerprint sensor!");
    } else {
      Serial.println("Did not find fingerprint sensor :(");
      while (1);
    }
    Serial.println("Waiting for valid finger...");
  }
}

// OK success!

void open_door() {
  digitalWrite (door, HIGH);
  delay (2000);
  digitalWrite (door, LOW);
}

void loop() {
  // RFID LOOP SETTINGS
  if (! mfrc522.PICC_IsNewCardPresent()) {
    // Look for new cards
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    // Select one of the cards
    return;
  }
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);
  // FINGERPRINT LOOP SETTINGS
  getFingerprintID();  // start fingerprint sensor
  delay(100);
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!
  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
}

default:
  Serial.println("Unknown error");
  return p;
}
} <-- Why?

Proper indentation shows that you have an extraneous closing curly brace.


text:
src folder in sketch structure and include library from subfolder
My Arduino/PlatformIO libraries folder
libraries
└── cleaner_robot
    └── TB6612_Dual
        ├── tb6612_dual.h
        └── tb6612_dual.cpp

I used this code to innclude it:
#include <TB6612_Dual/tb6612_dual.h>

But the compilation has errors like "undefinded variables, undefinded reference,...etc"
This is my sketch folder
Cleaner_Robot
└── src
    └── TB6612_Dual
        ├── tb6612_dual.cpp
        └── tb6612_dual.h

If i use this code, all is ok
#include "src/libs/TB6612_Dual/tb6612_dual.h"

So the question is how to include if header file is in a sub folder of a folder in libraries folder of Arduino/PlatformIO ?, i has searched for many things, but cant help.
I also read about src folder allowed in Arduino, but where can find an official announcement/manual for this structure ?, cant find much about this
Thanks  :)

You're using PlatformIO? Maybe you should ask on a PlatformIO forum?

https://www.arduino.cc/en/guide/libraries#toc5 -|- https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification

I could include header files in subfolder after including root header file.
#include <TB6612_Dual.h>
#include <TB6612_Dual/tb6612_dual.h>

TB6612_Dual
└── src
    ├── TB6612_Dual.h << Add this empty file.
    └── TB6612_Dual
        ├── tb6612_dual.cpp
        └── tb6612_dual.h

Examples:

utils_asukiaaa/examples/string/string.ino
utils_asukiaaa/src/utils_asukiaaa/string.h
utils_asukiaaa/src/utils_asukiaaa/string.cpp
utils_asukiaaa/src/utils_asukiaaa.h


text:
Linux SSH command for Arduino Console
I have a Leonardo with a Yun shield, and I would like to be able to control the arduino with a windows form app, but thats besides the point for now at least. Currently, I am using putty and the commands Telnet Localhost 6571. However, I would like to avoid doing that and I was wondering if anybody new how to communicate with the arduino from port 22 at the yun's IP, rather than connecting to the yun and then to the remote local machine. That or I am missing something entirely!
This is the code I am using to try and access the console port of the Leonardo:
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //connect to arduino
            ConnectionInfo ConnNfo = new ConnectionInfo("10.1.1.211", 22, "root", new AuthenticationMethod[] { new PasswordAuthenticationMethod("root", "123456789") });
            using (var sshclient = new SshClient(ConnNfo))
            {
                sshclient.Connect();
                //connect to console port of arduino
                TcpClient tcp = new TcpClient("localhost", 5671);
                sshclient.Disconnect();
            }
            Console.ReadKey();
        }
    }
}

Thank you

The console port is only listening on 127.0.0.1 (for security), so only the Yun itself can connect to the console.
You can weaken security and open the port up to the entire local network by editing the file /usr/lib/python2.7/bridge/console.py and change the value 127.0.0.1 to 0.0.0.0.
Any machine on your local network will then be able to connect directly to port 6571.

Thanks for the help, how would I connect to that port via my pc without using Telnet, I edited the file as per your instructions.

You can use putty in telnet mode to connect direct to the port.

Alternatively you can write your own software in whatever language you prefer to open a TCP connection directly and use it as if it were a serial port.


text:
how to apply a timer to an analogWrite command for Arduino
I have 2 sketches that control a fan using Arduino UNO, that I try to combine.
The 1st is a timer, that turns a pin ON and OFF for a determined period of time.   The pin signal drives a MOSFET that in turn switches a small fan ON and OFF.
Since I need to control the speed of the motor, I would rather use the PWM control, however I am not sure how I can apply the timer since analogWrite command refers to a pin and value.
Thanks
timer code:
const byte relayPin = 6; // relay module on pin 6
const byte monitorPin = 13; // builtin LED output
const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
unsigned long currentMillis, previousMillis = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // relay OFF
  pinMode(monitorPin, OUTPUT);
  digitalWrite(monitorPin, LOW); // LED off
}

void loop() {
  // put your main code here, to run repeatedly:
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval_1)
  {
    digitalWrite(relayPin, LOW); // relay ON
    digitalWrite(monitorPin, LOW); // LED on
  }
  if (currentMillis - previousMillis >= (interval_1 + interval_2))
  {
    digitalWrite(relayPin, HIGH); // relay OFF
    digitalWrite(monitorPin, HIGH); // LED off
    previousMillis = currentMillis; // reset
  }

PWM code:
int PWMControl= 6;
int PWM_Input = A0;

int PWM_Value = 0 ;

void setup() {
  pinMode(PWMControl, OUTPUT);
  pinMode(PWM_Input, INPUT);
}

void loop()
{
  PWM_Value = analogRead(PWM_Input);
  PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
  analogWrite(PWMControl, PWM_Value);
}

Thanks for the hand with editing the code

I don't really understand, what your problem is. have you tried to just pu the analogwrite into the timer code?

chris1 - I did and it did not work; just replacing digitalWrite with analogWrite doesn't do the trick: the fan runs continually whereas the LED follows the timing

Then please provide your combined code and describe how it does not work. What are you expecting to see? And what do you actually see?

@chrisl, I described that in my post just preceding yours

Then show us your combined code, since that is the problematic code

If I understand correctly, this is what you want to do:

Fan + led off for 15s
Fan on (speed determined by A0) + led on for 15s
Repeat

clearly defining your goal in english can help with coding, so I hope I guessed right.
here's my suggestion for your code
 const byte controlPin = 6; // MOSFET on pin 6
 const byte monitorPin = 13; // builtin LED output
 const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
 unsigned long currentMillis, previousMillis = 0;
 int PWM_Input = A0;
 int PWM_Value = 0 ;

 // setting pin direction and default state
 void setup() {
   pinMode(controlPin, OUTPUT);
   digitalWrite(controlPin, HIGH); // MOSFET OFF
   pinMode(monitorPin, OUTPUT);
   digitalWrite(monitorPin, LOW); // LED off
 }

 // run fan for a while, then stop fan for a while
 void loop() {

   currentMillis = millis();

   //fan on, led on, both signals active low
   if (currentMillis - previousMillis >= interval_1){
       PWM_Value = analogRead(PWM_Input);
       PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
       analogWrite(PWMControl, PWM_Value);
       digitalWrite(monitorPin, LOW);
   }

   //fan off, led off, reset timing
   if (currentMillis - previousMillis >= (interval_1 + interval_2)){
      digitalWrite(controlPin, HIGH); // MOSFET OFF
      digitalWrite(monitorPin, HIGH); // LED off
      previousMillis = currentMillis; // reset
   }
 }

There are still issues I can with this code:

You did not tell us what type of MOSFET you are using, so I can't tell if it is actually active low
I believe that the LED on most arduino boards (pin 13) is active high, so that's probably wrong
Don't forget comments. I find comments more useful for a block of code than for a single line, but you can do both if you want ;)
I find your code convoluted regarding millis logic, for me it would be more logical to have something along the lines of:
 current = millis()-previous
 if current < interval1
    fan off
 elseif current < inteval1+interval2
    fan on
 else
    previous = millis()

It would be more readable for a human, the result on the arduino would be similar.

you've got it right; I use a BUZ11 N-channel MOSFET to control the fan.   The code as it is now, works fine: the LED is on only when the fan is on.   I tried replacing the digital Write with the analogWrite as suggested but it didn't work so that's why I posted the code here.   It's obvious I'm not an experienced programmer...

@kwhunter have you tried to use the Serial monitor to make sure your initial program does what you think it does? It's extremely useful to debug arduino code. A few thing to monitor: does the fan turn on/off when you think your code turns it on/off. You also should monitor the PWM_Value before and after map, to make sure you are feeding analogWrite with the proper parameter. These would be the first things I look at.

No I did not, application not critical; for the record, I use the fan to move around the air in the Humidor for the temperature and humidity to even out.   Current setting is 1 minute ON and 15 minutes off.

Thinking back at my note#4, the resulting program would actually be different, since while the fan is on, it would immediately react to a change on A0, which is arguably better. your initial program with the on-off structure would have been easily implemented with delay() - also if you plan to run this program for a very long time, you will have an unexpected behavior when millis() overflows.

I made some edits to your program since the fan was ON all the time and when timer 2 kicked in it changed speed since A0 controlled it; now it works as it should, thanks.   Aren't the millis() reset each cycle? if not what can I do to prevent the overflow?

no, millis() returns the number of milliseconds since you powered-up the board, it keeps counting until it reaches its max value, then goes back to zero (after ~50 days according to arduino documentation: https://www.arduino.cc/reference/en/language/functions/time/millis/). you can guard against it with an if statement, or some clever math, there are examples you can easily find, this is NOT an unusual thing. also, if that's been helpful to you, consider up-voting and select the answer. If you think I should change something specific in the code I gave in my answer, I can do that too.

sorry MAB, I cannot vote since I do not have enough privileges.   The code is Ok, I didn't know you can turn on an output with an analogWrite and turn it off with a digitalWrite... Someone wrote another code for me, but it is a more advanced use of commands and therefore not as easy to add the PWM function, in my opinion.   I will google for the millis overflow; if I cannot handle, I will just turn the board off every week...

functionally, digitalWrite(LOW) is equivalent to analogWrite(0), digitalWrite(HIGH) is equivalent to analogWrite(255) if the pin is PWM capable. I can only encourage you to use https://www.arduino.cc/reference/en/ more, it has a lot of useful information.

Don't think of what you have as a "timer".  That's not what it really is.  What you have is a loop function that repeats itself thousands of times per second and an if statement that asks "is it time to do this yet".  So if you want to use analogWrite instead of digitalWrite when it is time to do that thing then use analogWrite instead.  It doesn't change your timing setup one bit.  Just replace the digitalWrite calls with analogWrite calls.  If you want to read your pot at the same time then put that in there as well.  Basically everything inside the block of code controlled by that if statement is going to happen every time that interval passes no matter what it is that you put there.  If you want something to print every interval then you could put a print statement in there.  If you want it to call some custom function every interval then call that function in there.  There's nothing special about that way of timing that only works with digitalWrite.


text:
Does a normal Ultra-Sonic sensor (HC SR04) sense through a Football Net like obstacle?
I am planning to to use ultrasonic sensors mounted on a drone for obstacle avoidance. I am not sure if the sensors would be capable of detecting a Net Like obstacle in its path.

Most likely it will not sense the net

There is always a chance of picking up the net for a split second, but most likely, you will never accurately detect the net with this type of sensor. There is too little to detect.


text:
Adafruit Trinket M0 persistent storage?
I am using an Adafruit Trinket M0 to program a small game and would like to save the game state every so often. I am programming it through the Arduino IDE as the chip does not have enough space to run my game with circuitpython (too many libraries). Does the Trinket M0 have any form of persistent storage that I can use, or will I need to use a different device?
I couldn't find EEPROM in the documentation, but I wasn't able to find a definitive answer whether or not it has any form of long term storage. Knowing the board is based on the Attiny85 I would expect it to have 512 bytes or so, but the arduino library is giving me problems when I try to use it as well, so there is no way for me to trial and error this.

Add a i2c eeprom to store data. There is a 'emulation' eeprom that writes to flash memory. I don't know the current status of that library. I don't know if that works with the trinket and I don't know if that has been added to the arduino eeprom library. https://github.com/cmaglie/FlashStorage The adafruit trinket m0 is not based on a attiny chip. Please add this link to your question: https://www.adafruit.com/product/3500

The M0 boards use a SAMD chip. It has no EEPROM. To store data without additional hardware like external EEPROM or SD card, you can use the flash memory of the SAMD. But it has a drawback of limited count of writes to flash memory.
An EEPROM emulation library exits. It uses the flash memory, so the drawback of limited writes, applies. I used it, it works. The library is almost official. The author is Arduino's coworker.