body:
With reference to this [question][1] ::I have written a program by using 16-bit timer,

The program is as follows .

```
volatile uint32_t ovfCount = 0;

struct Pulse {
  uint32_t last_toggle;
  uint32_t width;
  bool stateHigh;
  uint32_t get_width() {
    noInterrupts();
    uint32_t width_copy = width;
    interrupts();
    return width_copy;
  }
};

Pulse ch1, ch2, ch3, ch4, ch5, ch6;

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

  pinMode(13, OUTPUT);

  /*
     Pin Change Interupt for
     measuring the pulse width
     from the receiver

  */

  DDRK = 0; // A8:A15 -> direction as Input
  PORTK = B00111111; // A8:A15 -> pullupResistor

  PCIFR = 0; // resetting the flags
  PCIFR  = _BV(PCIF2);
  PCMSK2 = B00111111; // activating from A8:21 :: 6
  PCICR = B00000100; // activating 2 nd PCINT

  /*
     16 bit timer setup for counting
     timer 3 is used
  */
  TCCR3B = 0x00;
  TCCR3A = 0x00; // wave generation is normal:: all zeros
  TCNT3 = 0x00;
  TIFR3 = 0x00; // resetting all flags
  TIMSK3 = 0x01; //timer overflow enable
  TCCR3B = 0x01; // no prescaling

}

ISR(TIMER3_OVF_vect) {
  ovfCount++;
}

ISR(PCINT2_vect) {
  uint8_t tcnt3 = TCNT3;
  uint32_t ovf_count = ovfCount;

  if ( bit_is_set(TIFR3, TOV3) && tcnt3 < 32768 ) {
    ovf_count ++;
  }

  uint32_t time = ovf_count << 16 | tcnt3;

  pinChangeFunction(&ch1, time, PK0);

}

void pinChangeFunction(Pulse *channel, uint32_t time, uint8_t pin) {
  bool input_is_high = bit_is_set(PINK, pin);
  if (input_is_high && !channel->stateHigh) {
    channel->stateHigh = true;
  } else if (! input_is_high && channel->stateHigh) {
    channel->width = time - channel->last_toggle;
    channel->stateHigh = false;
  }
  channel->last_toggle = time;
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(200);

  Serial.println(ch1.get_width() / 16e3, 5);

}

```

The output for the same is attached below,

```
999.42663
999.43060
999.42645
999.42614
999.42730
999.42889
1003.52416
999.41473
999.42681
999.42211
999.42077
999.42718
999.42016
1003.52447
999.41802
999.42480
999.42541
999.43603
999.42767
1003.52075
999.42199
999.42767
999.41876
999.42816
999.41143
999.41998
1003.52429
999.42730
999.42950
999.41149
999.42028
999.42700
1003.50775
999.43743
999.42858
999.42584
999.41381
999.42852
999.43347
1003.52490
999.41925
999.41540
999.42510
999.42895
999.42980
999.42883
1003.52526
999.43658
999.43707
999.42889
999.41284
999.42401
1003.52343
999.42901
999.42950
999.42822
999.43481
999.42718
999.41986
1003.52398
999.42303
999.41442
999.42498
999.42950
999.42547
1003.51074

```

When I observed the output , I am seeing 999 millis almost instead of 1000 as expected and  in-between  1003 is coming instead either of 999/1003.

Also if we look carefully it can be observed that ,999 is coming 6 times then one 1003 is coming and is going 2 times like that then 5 times 999 then 1003

so I want to know what mistake I am making in my program , and what is the reason behind these kind of behaviour .

  [1]: https://arduino.stackexchange.com/questions/81688

title: Arduino mega PinChangeInterrupt with 16 bit timer

Answers: 0


body: I have a project that I need to make a game(guitar hero like rhythm game) on unity for android and develop a custom controller for it that works on mobile game. I decide to use Arduino and hc05 module for communication via bluetooth. But every tutorial that I watched on internet that phone used as a controller, I want to make it reverse. I want to use Arduino as a gamepad on Adnroid. Is that possible?

title: Can I Control My Android Game with Arduino Gamepad

Answers: 0


body:
I only uploaded the library to check whether it has installed correctly. It's installed and i am having this error. ****Using library DHT-sensor-library-master at version 1.4.1 in folder: C:\Users\Mcniven\Documents\Arduino\libraries\DHT-sensor-library-master\DHT_U.h:36:29: fatal error: Adafruit_Sensor.h: No such file or directory**
 #include <Adafruit_Sensor.h>
exit status 1
Error compiling for board Arduino/Genuino Uno.**

title: DHT-sensor-library-master

Answers: 0


body:
I am trying to transfer `unsigned int` values between 2 Arduinos. When sending `61805` for the first 8-9 times, I am getting 61805.  But after then after these numbers: 61937 28013 61937.

Sender's code:

```c++
unsigned int s = 0;

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

void loop()
{
  s = 61805;
  Serial.write(highByte(s)) ;
  Serial.write(lowByte(s));
}
```

Receiver's code:
```
unsigned int upperByte, lowerByte, r=0,p=0;

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

void loop ()
{
  if (Serial.available() > 1)
  {
    upperByte = Serial.read();
    lowerByte = Serial.read();
  }

  r = (upperByte << 8) + lowerByte;
  Serial.println(r);
}
```

title: Getting different numbers after sending an unsigned int 8-9 times between Arduinos via Serial

Answers: 2

body:
There's no flow control on the Uno and Nano serial and you are sending fast enough that bytes are being dropped, and there's no way your protocol or lack of one demarcates separate messages (separate numbers).

This will become more clear if you write your numbers in hexadecimal:

Dec   | Hex
------|-------
61805 | 0xf16d
61937 | 0xf1f1
28013 | 0x6d6d
61937 | 0xf1f1

Adding a delay between messages can serve to separate the messages if the receiving side makes use of a timeout and can prevent the receiving side from being overwhelmed with data.

If you don't want to use time to slow down and separate your data, you can change what is sent instead.  One idea is to have the receiver actively request data when it's ready to receive some. The receiver should expect only entire messages between requests, and if anything gets garbled, it can just read whatever data has come in and throw it out before the next request.

Your messages can also be separated by some characters that aren't part of the messages themselves.  A common way to do that is just to encode your numbers as separate text lines, the line terminator `\n` serving as the separator between messages.

I have not tested these yet, but here are some very minimal modifications to your code that approach having a protocol with fewer of these problems:

Sender:
```c++
void setup () {
  Serial.begin(9600);
}

void loop() {
  // await message request
  while(Serial.read() != '.') {
  }

  //
  const unsigned int s = 61805;
  Serial.write(highByte(s)) ;
  Serial.write(lowByte(s));
}
```

Receiver:
```c++
void setup() {
  Serial.begin(9600);
}

void loop () {
  // request message
  Serial.write('.');

  // await two-byte response
  const unsigned long request_sent = millis();
  while (Serial.available() < 2 && millis() - request_sent < 1000) {
  }

  if (Serial.available() != 0) {
    if (Serial.available() == 2) {
      // two byte response receive
      const unsigned upperByte = Serial.read();
      const unsigned lowerByte = Serial.read();
      const unsigned r = (upperByte << 8) + lowerByte;
      Serial.println(r);
    }  else {
      // Something went wrong, too or too few bytes received;
      // throw them out and start over.
      delay(100);
      while (Serial.read() != -1) {}
    }
  }
}
```

It's a long way from doing it properly, but it should behave at least better than what you started with.  There's a lot to designing even simple protocols.  Typically there are start and end frame bytes, a checksum or CRC, a message number counter, etc.

body:
This line of the receiver's code:

```c++
Serial.println(r);
```

is transmitting 7 bytes (5 digits, CR and LF). At 9600/8N1, each byte
takes 1.04 ms, so the whole message is transmitted in 7.28 ms. In the
meantime, the sender is sending 2-byte messages at full speed (no pause
between the bytes). The receiver thus gets a new message every 2.08 ms.

So, this receiver is given a new message to relay every 2.08 ms, and it
needs 7.28 ms to relay each message. Something has to give...

The reason it works in the beginning is that the receiver has a buffer
where it stores the bytes that have been received, and another one for
the bytes that have been made ready for transmission. As long as there
is room in those buffers, everything works fine. The transmit buffer
fill-up first, as it gets 7 bytes to transmit every 2.08 ms. Once it's
full, `Serial.println()` becomes a _blocking_ function: it has to wait
for the buffer to have enough room. Your `loop()` then slows down to
match the speed of the transmission, which means you do not read the
receive buffer often enough. Then the receive buffer fills up, and you
start loosing bytes.


body:
I'm looking to transmit some data (CSV files) from a measurement instrument underground to a receiver on the surface. I've learned that in order to penetrate the limestone, low frequencies have to be used (around 80-130kHz). There are various methods to achieve this including ground-spike antennas that use the earth between two electrodes to help form a large antenna by injecting current into it, and magentic loop antenna pairs that essentially work as an inductor by transmitting and receiving the magnetic (H) component of the RF transmission rather than the low power E field component.

With that put aside, I've noticed that there aren't any existing RF modules that transmit at these frequencies. Because of that I've considered the posibility of directly synthesising a modulated signal using an Arduino (UNO or Nano). As far as I understand it I need a direct digital synthesis (DDS) IC to generate a reference frequency. From there I understand the Arduino can be used to calculate a waveform that simulates the modulation of that carrier in the mentioned frequency range. I've identified that on-off keying (OOK) would be a suitable modulation method due to its simplicity and resistance to noise.

This is where I need some help. How do I take a CSV file, use its contents to OOK modulate a carrier wave and then directly synthesise this waveform using Arduino? Are there libraries that already exist to enable this? If so, how would I use them? I don't have extensive programming knowledge so I'd appreciate any help I can get.

title: Directly synthesising an OOK modulated LF signal using a CSV file

Answers: 0


body: I have a term project: I need to develop a mobile game(Guitar Hero Like rhythm game) probably on Unity  and I need to develop a custom controller for this game that works on android device. There will be 3 buttons for notes and Bluetooth connections between android device and controller. My question is can I use ESP32 as a mobile game controller. If it's yes, which model that you suggest me?

title: Can ESP32 used for mobile game controller?

Answers: 0


body:
https://www.arduino.cc/reference/en/

**Language Reference**

Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.

**functions**

For controlling the Arduino board and performing computations.

**variables**

Arduino data types and constants.

**structure**

The elements of Arduino (C++) code.

I find it really confusing that they had to separate the structures but also call them functions in their descriptions. I kind of want to get a visualization or organization of the different language references present in Arduino, but this just confuses me. Are structures in the website also functions, and mainly syntax can be classified into functions and variables? Can someone present a organization of language references that doesn't involve terminologies being mixed up?

title: This website in Arduino lists language references like functions and structures. What makes structures there different from functions?

Answers: 1

body:
What the website names "Structures" are code building-blocks for the programs you write. The main part there are conditional (if-else) and loop instructions (for, while) as well as mathematical operators. You use these constructs _within_ your functions to control program flow or compute results. Functions, on the other hand, are sub-programs you can call to have certain tasks done. They might be pre-defined (such as the ones described on the language reference page) or written by yourself.

Now there are two exceptions to this rule, and maybe that's what's a bit confusing in the documentation: The functions `setup()` and `loop()`. These are functions, but make up the structure of your program. They are the main building blocks of an Arduino sketch, and that's probably why they're mentioned under "Structures".


body:
Pl someone answer my question. I have 4 pushbuttons namely add/reject/start & finish. I want to

1.Deactivate add/reject buttons at initial stage until start button pressed &
2.Once add/reject activated, again should deactivate once finish button pressed &
3.Again if start button pressed, add/reject should be released.

with following code I able to do 2 & 3. However I still lack at 1.pl help on this.

    const int bt_Select = 2; // #define BUTTON_PIN 2
    const int bt_Reject = 3;
    const int bt_Start = 7;
    const int bt_Finish = 8;

    boolean decision=false;

    int bt_Select_state=0;
    int bt_Reject_state=0;
    int bt_Start_state;
    int bt_Finish_state;

    int selected;
    int rejected;
    int started;
    int finished;

    void setup() {
      // put your setup code here, to run once:
        Serial.begin(9600);
        pinMode(bt_Select, INPUT_PULLUP);
        pinMode(bt_Reject, INPUT_PULLUP);
        pinMode(bt_Finish, INPUT_PULLUP);
        pinMode(bt_Start, INPUT_PULLUP);
        Serial.println("Welcome to Nano");
        delay(1000);
      }
    //========================================================================
          void loop() {

            started=bt_Start_pressed();
            selected =bt_Select_pressed();
            rejected=bt_Reject_pressed();
            finished=bt_Finish_pressed();
            decision=Stop_Main();

              int x=0;
              int Lx=0;

           if (selected&&decision){x=1;}
           if (rejected&&decision){x=2;}
            if (started&&decision){x=3;}

           if (x!=Lx){
            switch(x) {
              case 1:Serial.println("Selected");delay(250);Serial.print("");
                     break;
              case 2:Serial.println("Rejected");delay(250);Serial.print("");
                     break;
              case 3:Serial.println("Started");delay(250);Serial.print("");
                     break;

              /*case 3://do something when var equals 1
                     Serial.println("Removed");
                     break; */
                 }//switch(x)
                     x=Lx;
          }//eof  if (x!=Lx)

          }
    //====================================================================
          int bt_Select_pressed(){
             bt_Select_state=digitalRead(bt_Select);
               if (bt_Select_state == LOW){return 1;}
               else {return 0;}
          }
    //====================================================================
          int bt_Reject_pressed(){
             bt_Reject_state=digitalRead(bt_Reject);
              if (bt_Reject_state == LOW){return 1;}
              else {return 0;}
               }
    //====================================================================
        int bt_Start_pressed(){
             bt_Start_state=digitalRead(bt_Start);
              if (bt_Start_state == LOW){return 1;}
              else{return 0;}
                }
    //====================================================================
          int bt_Finish_pressed(){
            bt_Finish_state=digitalRead(bt_Finish);
              if (bt_Finish_state == LOW){return 1;}
              else {return 0;}
          }
    //=====================================================================

          boolean Stop_Main(){
            bt_Start_state=digitalRead(bt_Start);
            bt_Finish_state=digitalRead(bt_Finish);

            if (bt_Finish_state == LOW){decision=false;return decision;}
            if (bt_Start_state == LOW){decision=true; return decision;}

           // else if (bt_Start_state ==HIGH  && bt_Finish_state == HIGH){decision=false;return decision;}
              }

    /*  boolean bt_Start_pressed(){
             bt_Start_state=digitalRead(bt_Start);
              if (bt_Start_state == LOW){started=true; return decision;}
              else{started=false; return decision;}
                }   */
        //===================

title: Deactivate pushbuttons at initial stage

Answers: 1

body: Keep a boolean that begins as `false`. Set it to `true` when the start button is pressed, and set it to `false` again when the finish button is pressed. Then you can read and respond to the `add` and `reject` buttons only if that boolean is `true`, and otherwise ignore them.


body:
For a project, I am looking for a board to connect eight different neopixel-strips to, on eight different data pins. Because of the great length between the board and the first pixels of these strips, I'd like to have a board that operates on 5V instead of 3.3V. Then, the software that I have written to manage the LEDs is made in (micro)python. Finally, for simplicity's sake, I'd like to run this complete project on a single board without too many external components.

So, I have found 5V boards with enough pins, like the Arduino Mega. I have also found boards with enough pins and support for micropython or circuitpython, like the ESP32 or Arduino Zero.

However, I haven't quite found a board that matches all my requirements. The Arduino Mega doesn't support Python (or does it?) and the ESP32 and Arduino Zero operate at 3.3V, not 5V.

Of course, an ESP32 with a level shifter (as shown on https://github.com/Aircoookie/WLED/wiki) would work, but is there perhaps a board out there that fits all my requirements our of the box?

**Edit**: It seems I misunderstood some things about the hardware - sorry, I'm a software guy, not really a hardware guy :-) I fixed the question above as not to confuse future readers.

title: 5V pin out, 8 pins and micropython/circuitpython support: does such a board exist?

Answers: 1

body:
Both your "requirements" for the board are non-requirements.

* You do not need PWM pins to drive Neopixel strips. The Neopixel uses its own custom protocol that has nothing at all to do with PWM.
* Your board only communicates with the *first* Neopixel. That one then communicates with the second, which then communicates with the third, etc. The length of the strip is completely irrelevant to what voltage you might want to drive it with.

You only care about having enough general IO pins and if the IO voltage is suitable to communicate with one Neopixel.

According to the WS2812B datasheet VIH is 0.7Vdd, which if you are powering the strips from 5V means 0.7 * 5 = 3.5V. However I think that is an overestimation and they are erring on the side of caution there, since I regularly drive WS2812B strips from a 3.3V microcontroller with no problems whatsoever (after all it's only 0.2V difference).

So just use your favourite 3.3V microcontroller that can run micropython and, if you really want to, drop the power supply voltage to the strips a fraction with a diode.


body:
I have an ESP-01s that I want to use as a shield so my Arduino Uno project could communicate with my Blynk app through wi-fi. I used this [video][1] as reference.

This is the sketch. It is just the ESP8266_Shield example from the Blynk library.

    #define BLYNK_PRINT Serial

    #include <ESP8266_Lib.h>
    #include <BlynkSimpleShieldEsp8266.h>

    char auth[] = "-------";

    char ssid[] = "-------";
    char pass[] = "-------";

    #include <SoftwareSerial.h>
    SoftwareSerial EspSerial(2,3); // RX, TX

    // Your ESP8266 baud rate:
    #define ESP8266_BAUD 115200

    ESP8266 wifi(&EspSerial);

    void setup()
    {
      // Debug console
      Serial.begin(9600);

      delay(10);

      // Set ESP8266 baud rate
      EspSerial.begin(ESP8266_BAUD);
      delay(10);

      Blynk.begin(auth, wifi, ssid, pass);
    }

    void loop()
    {
      Blynk.run();
    }

This is the schematic:
[![enter image description here][2]][2]

What happens:

 1. The moment the Arduino is connected, the Blue LED flashes for a split second then all LEDs will remain off. No red led, no blue led, really just off.
 2. The ESP-01s becomes very hot to the touch
 3. Tried speaking to the ESP-01s through the Serial Monitor with BareMinimum example @115200 baud rate (Many Sources say that this is the default baud rate of the recent boards) however no response whatsoever.
 4. Running the code above will get this error:
[![enter image description here][3]][3]

What I've done:

 1. Tried using the HW-131 Breadboard Power Supply to supply the 3.3V (Common GND with Arduin) of the ESP-01s because I read that the ESP-01 could need more current than the Arduino Uno could provide. Still the same effect, no LEDs blinking, (even more) hot to touch, not responding to AT commands.
 2. Attempted to follow a [video][4] on how to flash the ESP-01s but when I tried to run the flasher, it could not communicate with the ESP-01s.
 3. Switching Rx Tx ESP-01s but no luck.
 4. Added a 10k ohm resistor from 3.3V to the Vcc and EN pins of the ESP-01s. Not heating up anymore but still no response.

I really thought this is a power problem but could it be the ESP-01s itself is defective?

This is what the ESP-01s look like
[![enter image description here][5]][5]

  [1]: https://youtu.be/6rx-iyUq1Po
  [2]: https://i.stack.imgur.com/wN3kM.png
  [3]: https://i.stack.imgur.com/qYuZV.png
  [4]: https://www.youtube.com/watch?v=QVhWVu8NnZc
  [5]: https://i.stack.imgur.com/8BYEi.jpg

title: ESP8266 ESP-01s Not Responding, No LED Blinking, Overheating

Answers: 1

body:
The ESP8266 is (highly likely) dead. The overheating is the giveaway.

Because you haven't mentioned it, please measure the 3.3V coming out of the Arduino with a multimeter. Unlikely to be the issue but it is easy to check.

You will need to buy a new ESP8266 and connect it up and give it a try with a fresh board. If all is good with the new board then the first thing you will notice is that there is no overheating. (If there is overheating, then you need to check your wiring.)

I notice that quite a lot of people add a 10K pull up resistor between the 3.3V line and the Enable pin. It certainly won't hurt to add a 100 ohm resistor between the Tx pin and D2 of the Arduino as a current limiting resistor, just in case.


body: Is there any circuit with opamp where can be (by arduino) set constant output amplitude which doesn't depend on input signals amplitude?

title: Stable amplitude of amplified signal

Answers: 1

body: Just a word of advice about AGC in general.  There is almost always an issue of attack (how fast the AGC reacts to a sudden increase in signal) and release (how fast it reacts to a decrease).  In most AGC cases you will need to make compromises on these two settings to avoid poor behavior from the AGC... it's not a drop-in solution with pre-determined optimum settings for every purpose.  Depending on the program material, you can get obnoxious artifacts like breathing and pumping for speech and music.  ACG is much better at slow drifting things like signal fade.  I just want you know what you might be getting into...


body:
I want to use Raspberry Pi as an aggregator & distributor of messages originating from or destined for a flight simulation program.

I'd like to use arduinos connected via spi as specific hardware controllers, communicating as fast as possible with the raspberry pi to accept messages as they come from the simulation program and at the same time to transmit messages to the simulator when various switches and controls are operated.  At the same time, each arduino might need to control additional spi devices by acting as master, when not operating in slave mode to the raspberry pi.

Can several Arduinos be connected to a Raspberry Pi as a slave devices while acting as a master device for other devices connected to the arduino like an adc, a dac, a multiplexer or motor controller?

title: Raspberry Pi with multiple arduinos using spi

Answers: 0


body:
I have a Digistump's ATtiny85 (probably a clone ), which fails uploading any sketch, saying: `Warning: device with unknown new version of Micronucleus detected.
This tool doesn't know how to upload to this new device. Updates may be available.` I'm using Ubuntu 20.04 and Arduino IDE 1.8.13.

I used these guides:

1) [Digistump's troubleshooting for linux][1]

2) [Kovo's blog][2]

After using these guides, finally I stop getting this error, but when trying to upload the firmware ( last line at Kovo's ), I get this error:

    `guy@desktop:~/tmp/micronucleus/upgrade$ ~/.arduino15/packages/digistump/tools/micronucleus/2.0a4/launcher -cdigispark  --timeout 60 -Uflash:w:~/tmp/micronucleus/upgrade/t85_default.hex:i
    Running Digispark Uploader...
    Plug in device now... (will timeout in 60 seconds)
    > Please plug in the device (will time out in 60 seconds) ...
    > Device is found!
    connecting: 16% complete
    connecting: 22% complete
    connecting: 28% complete
    connecting: 33% complete
    > Device has firmware version 2.4
    > Device signature: 0x1e930b
    > Available space for user applications: 6522 bytes
    > Suggested sleep time between sending pages: 7ms
    > Whole page count: 102  page size: 64
    > Erase function sleep duration: 714ms
    parsing: 50% complete
    > Program file is 1594 bytes too big for the bootloader!

Did someone encounter this ?

  [1]: https://digistump.com/wiki/digispark/tutorials/linuxtroubleshooting
  [2]: https://kovo-blog.blogspot.com/2019/01/how-to-upgrade-bootloader-on-digistump.html

title: Fail uploading any sketch to ATtiny85 USB

Answers: 1

body:
[from Daves Comment][1]

need to download `upgrade-t85.hex` (which is not `t85_default.hex`) from [link][2]

  [1]: https://github.com/micronucleus/micronucleus/issues/148
  [2]: https://github.com/micronucleus/micronucleus/files/2837520/upgrade-t85_default.zip


body:
There are 5 push buttons each which correspond to a certain pin let us use 1 to 5 for an example. I want to set a variable, ActiveButton which will be set equal to the the only pin in the one state.

We make use of the concept that there can only be one active push button which means the pin of that would have a state of 1 and the rest would be 0. The long method for this would be to use if else and digitalRead(pin) == 1 for everything but that would be so long on code. I've considered using do while or while loops to stop until it reaches the push button with a state of 1, but I was wondering if there was a more efficient code. I was also thinking if there was something that made use of an array?

Though the idea of like having a simple array return the index which results to a corresponding value, like lets say Array[3] ==1 among all the indices from 0 to 4 feels easy to conceptualize, I believe this can be really taxing on the code and might not be able to be done in a short way.

I've also thought of possibly using switch case, where the variable would be the high state and the case values would be the digitalRead for each pin? But that would be wrong probably because I'm not sure but you can't set the switch variable to be a constant. It would be something like

switch (1)

case digitalRead(1):

case digitalRead(2):

title: I have five pushbuttons that refer to a specific pin number. I need an efficient code to set the variable for the pin with state 1. What can I use?

Answers: 1

body:
> There are 5 push buttons each which correspond to a certain pin let us use 1 to 5 for an example. I want to set a variable, ActiveButton which will be set equal to the the only pin in the one state.

Here is a short snippet of what I think, that you want. This is quite a long shot, since your question is not clear enough.

    int ActiveButton = -1; // Initialize ActiveButton with -1 (no button is active)
    int button_pins[] = {1, 2, 3, 4, 5}; //Array with pin numbers of the buttons

    void loop(){
        for(int i=0;i < sizeof(button_pins)/sizeof(button_pins[0]); i++){
            if(digitalRead(button_pins[i])){ //if the i-th button was pressed
                ActiveButton = button_pins[i]; // assign i-th button pin number to variable
                break; // Break, since we have found our pressed button, ignore all following pins
            }
        }
    }

This will assign the pin number of the pressed button to the variable. The condition in the for loop

    sizeof(button_pins)/sizeof(button_pins[0])

will make the code loop over all elements of the `button_pins` array. `sizeof(button_pins)` returns the number of bytes in the `button_pins` array. Then we divide through the number of bytes in the first element to get the number of elements in the array.


body:
I apologise in advance if this question is not right for this Stack or if it has been answered before but here goes. I have been learning C/C++ using the Arduino Uno and have gotten the hang of it now (I have knowledge of other programming languages so it wasn't too difficult to get in the rhythm). I was wondering how I would go about creating a standalone solution if I wanted to display information on four or five displays similar to [These][1]. I would pull data from a network to display on them too so I would also need to accommodate an ethernet or WiFi shield.

I appreciate any help or pointers in the right direction that can get me started on my journey to creating some custom embedded systems (if that is indeed what I am describing).

  [1]: https://uk.banggood.com/Geekcreit-IIC-I2C-2004-204-20-x-4-Character-LCD-Display-Screen-Module-Blue-Geekcreit-for-Arduino-products-that-work-with-official-Arduino-boards-p-908616.html?akmClientCountry=GB&cur_warehouse=UK

title: Where to go after Arduino?

Answers: 3

body: You can use an ESP8266 or ESP32. This makes it easy to transmit data via WiFi. You can also use the Raspberry Pi, which is much more powerful.

body:
Uno's major limitation is it's 2KB of RAM. If your project can live within that, an Uno can host it.

The display you pointed to is an I2C device and can be bussed, so Uno pin count won't be a limitation. Current might be, and you may need to power the displays independently of the Uno.

The ESP-01 module makes a good Wifi accessory which you can build onto a proto-shield or wire separately. If your Uno is powered at 5v as most are, you'll need to provide 3.3v power to the ESP, and a voltage divider on the ESP's receive pin to reduce the serial input from the Uno to 3.3v.

Update:<br>Upon re-reading, your title suggests you *want* to move on from an Uno, and you've mentioned a stand-alone solution.

There are many more capable boards available. Arduino Mega is one which would be an easy, almost trivial, transition up from Uno, in flash memory, RAM, and available I/O.

If by "stand-alone" you meant "roll your own", there are any number of how-to projects on the web for rolling your own Atmega 328p-based clone of an Uno.

The direction you take really depends on your project needs and/or your personal preference.

body:
There are a lot of much more powerful boards available (especially if you move from ATmega to ARM).  I'm partial to the [Teensy](https://www.pjrc.com/teensy/) boards myself but Adafruit also makes some nice ARM-based boards.  For example, I'm currently developing a multi-user, Arduino-compatible Unix-ish OS on [one of the latter](https://www.adafruit.com/product/3010) (48MHz ARM, 256kB flash, 32kB SRAM) and it runs great.  Honestly, controlling 5 non-graphical LCDs over I²C is should be easily doable on an Uno but if you need more power there are lots of options available.

(No affiliation to either of those companies)


body:
I am working on a project where the ESP 8266 sends a very long string about 3000 chas over http as client. Likewise sensor data are determined. The loop repeats itself every 250ms. Now I noticed that my server gets the data of the ESP often slower. In rare cases it takes up to a minute until the server receives something.

How do these delays occur and why are they not constantly the same?
Does the length of the string have something to do with it or how much does it affect the delay?
Are calculation and sensors processed only after sending?

The ESP works with this function.
```
String http_GET(String value){
  String payload = "non_Message";
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    //WiFiClient client;
    HTTPClient http;  //Declare an object of class HTTPClient
    String send_data = "http://" + web_conect + value;
    //http.begin(client, send_data);
    http.begin(send_data);
    //http.begin("http://" + address + ":" + port + "/" + path +"?" + param + "=" + value);  //Specify request destination
    int httpCode = http.GET();                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      payload = http.getString();   //Get the request response payload
      Serial.println(payload);             //Print the response payload
      cut_time_date(payload);

    }

    http.end();   //Close connection

  }
  return payload;
}

```

title: Http Client requests

Answers: 0


body:
I recently switched over to Visual studio code and away from the arduino IDE though I am a novice at both. When using VSC and the neopixel library and sample code(strandtest) containing animations such as color wipe, I run into declaration errors. I have insalled the library to the project im working on, it is declared by default in the platformio.ini file as expected. The animation function is in the code as well and the same code works just fine in the Arduino IDE as stated. The standard example files (ie ones other than strand test with no declared function animations) work just fine no declaration issues. When I compile in the arduino ide it works as expected can use the example and immediately upload it no problem.

```
#include <Arduino.h>

// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    3

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 4

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

// setup() function -- runs once at startup --------------------------------

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}

// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {
  // Fill along the length of the strip in various colors...
  colorWipe(strip.Color(255,   0,   0), 50); // Red
  colorWipe(strip.Color(  0, 255,   0), 50); // Green
  colorWipe(strip.Color(  0,   0, 255), 50); // Blue

  // Do a theater marquee effect in various colors...
  theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness

  rainbow(10);             // Flowing rainbow cycle along the whole strip
  theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
}

// Some functions of our own for creating animated effects -----------------

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {
  for(int a=0; a<10; a++) {  // Repeat 10 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show(); // Update strip with new contents
      delay(wait);  // Pause for a moment
    }
  }
}

// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}

// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  for(int a=0; a<30; a++) {  // Repeat 30 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        // hue of pixel 'c' is offset by an amount to make one full
        // revolution of the color wheel (range 65536) along the length
        // of the strip (strip.numPixels() steps):
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}
```

The error I run into when compiling is as follows.

```
> Executing task in folder led pir: C:\Users\Yeee\.platformio\penv\Scripts\pio.exe run --target upload <

Processing nanoatmega328new (platform: atmelavr; board: nanoatmega328new; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/nanoatmega328new.html
PLATFORM: Atmel AVR (3.1.0) > Arduino Nano ATmega328 (New Bootloader)
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 30KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr 5.1.0
 - tool-avrdude 1.60300.200527 (6.3.0)
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> https://docs.platformio.org/en/latest/librarymanager/ldf.html
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 7 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Adafruit NeoPixel> 1.7.0
Building in release mode
Compiling .pio\build\nanoatmega328new\src\main.cpp.o
src\main.cpp: In function 'void loop()':
src\main.cpp:59:43: error: 'colorWipe' was not declared in this scope
   colorWipe(strip.Color(255,   0,   0), 50); // Red
                                           ^
src\main.cpp:64:46: error: 'theaterChase' was not declared in this scope
   theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
                                              ^
src\main.cpp:68:13: error: 'rainbow' was not declared in this scope
   rainbow(10);             // Flowing rainbow cycle along the whole strip
             ^
src\main.cpp:69:25: error: 'theaterChaseRainbow' was not declared in this scope
   theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
                         ^
src\main.cpp: In function 'void colorWipe(uint32_t, int)':
src\main.cpp:81:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
                 ^
src\main.cpp: In function 'void theaterChase(uint32_t, int)':
src\main.cpp:96:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int c=b; c<strip.numPixels(); c += 3) {
                     ^
src\main.cpp: In function 'void rainbow(int)':
src\main.cpp:112:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
                   ^
src\main.cpp: In function 'void theaterChaseRainbow(int)':
src\main.cpp:136:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int c=b; c<strip.numPixels(); c += 3) {
                     ^
*** [.pio\build\nanoatmega328new\src\main.cpp.o] Error 1
================================================================================== [FAILED] Took 1.30 seconds ==================================================================================
The terminal process "C:\Users\Yeee\.platformio\penv\Scripts\pio.exe 'run', '--target', 'upload'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.
```

title: Having trouble compiling in VsCode when including arduino libraries. (Win10)

Answers: 0


body: Let's say I want to create 10 functions, named Function1, Function2 until 10. Each function is used if a certain variable is equal to its number. Rather than using individual functions, I was wondering if its possible to use some sort of array concept for functions when it comes to naming? I tried reading about it and saw pointers but I honestly didn't understand what I saw about it because I'm not that adept to the language yet.

title: Can you create an array for functions?

Answers: 1

body:
Here's an example of how to do it:

**jump_table.ino**

``` lang-cpp
int func1(int arg)  { return arg + 1; }
int func2(int arg)  { return arg + 2; }
int func3(int arg)  { return arg + 3; }
int func4(int arg)  { return arg + 4; }
int func5(int arg)  { return arg + 5; }
int func6(int arg)  { return arg + 6; }
int func7(int arg)  { return arg + 7; }
int func8(int arg)  { return arg + 8; }
int func9(int arg)  { return arg + 9; }
int func10(int arg) { return arg + 10; }

int (*jump_table[10])(int) = { func1, func2, func3, func4, func5,
                               func6, func7, func8, func9, func10 };

void setup() {
}

void loop() {
  int index = 2;
  int argument = 42;
  int result = (*jump_table[index])(argument);
  // result is 45
}
```

All functions stored in the array must have the same signature.  This simply means that they must return the same type (e.g. `int`) and have the same arguments (a single `int` in the example above).

---

You can do the same with *static* class methods (but not instance methods).  For example you could use `MyClass::myStaticMethod` in the array above but not `MyClass::myInstanceMethod` nor `instance.myInstanceMethod`:

``` lang-cpp
class MyClass {
public:
  static int myStaticMethod(int foo)   { return foo + 17; }
  int        myInstanceMethod(int bar) { return bar + 17; }
}

MyClass instance;
```

There's a [similar question](https://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers) over on stackoverflow.


body:
Currently I am using [OneButton.h library](https://github.com/mathertel/OneButton) to code  my push button project.

When I press button1, it will loop for 10sec and print TRIG, and after that it will break the loop.

However, now I want to program it where when I pressed button1, it will jump to `void(singleclick)` function, but right now, during the 4th seconds (anytime before 10th sec) , I pressed other button, thus Arduino will break the 10sec loop and run other function.

Thus, I need help how to make sure Arduino actively read other button and when pressed, it will break the current loop/function to other related functions.

```
#include <OneButton.h>

OneButton button1(2, true);
OneButton button2(3, true);
OneButton button3(4, true);
OneButton button4(5, true);

int led1=12;
int led2=11;
int led3=10;
int led4=9;

int flag;

unsigned long mode;

void setup()
{

  Serial.begin(9600);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);

  button1.attachClick(singleclick1);
  button1.attachLongPressStop(longclick);

  button2.attachClick(singleclick2);
  button3.attachClick(singleclick3);
  button4.attachClick(singleclick4);

   digitalWrite(led1,HIGH);
   digitalWrite(led2,HIGH);
   digitalWrite(led3,HIGH);
   digitalWrite(led4,HIGH);

}

void loop()
{
  button1.tick();
  delay(20);

  button2.tick();
  delay(20);

  button3.tick();
  delay(20);

  button4.tick();
  delay(20);

  Serial.println("No button");

}

void singleclick1()
{
   mode=millis();

   flag=1;

   while(flag==1)
   {
     Serial.println("button1");
     digitalWrite(led1,HIGH);
     digitalWrite(led2,LOW);
     digitalWrite(led3,LOW);
     digitalWrite(led4,LOW);

     if(millis()-mode>5000)
     {
      Serial.println("TRIG");
      flag=0;

      button2.tick();
      delay(20);

      button3.tick();
      delay(20);

      button4.tick();
      delay(20);

      return;
     }
   }
}

void singleclick2()
{
   Serial.println("button2");
   digitalWrite(led1,LOW);
   digitalWrite(led2,HIGH);
   digitalWrite(led3,LOW);
   digitalWrite(led4,LOW);
}

title: How can I break the loop if it hasn't finished in 10 seconds?

Answers: 1

body:
This is just a rough, incomplete and untested code.

```
void singleclick1() { startTimer = true; }      // this only sets a flag

void singleclick2() { runTimer   = false;}      // this only clears a flag

void loop() {

   button1.tick();
   button2.tick();

   if startTimer {                 // this occurs once every click 1

      mode        = millis();
      runTimer    = true;
      startTimer  = false;         // run this block only once every click 1

      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW );
      digitalWrite(led3, LOW );
      digitalWrite(led4, LOW );
   }

   unsigned long currentMillis = millis();

   if (currentMillis - mode > 10000) {
      runTimer = false;                      // 10 second timer expired
   }

   if (runTimer) {

      Serial.println("TRIG");

   }
   else {
      // optional stuff to do
   }
}
```


body:
so I have an Arduino mega as the master and an attiny84 as the slave
the master can send a command `S` to the slave and the slave should respond with `TRUE` so here is my code:

master:

```
bool GetOnline(){
      Serial.println("Sending Status Command ");

      if(Address != NULL){
        String res;
        Wire.beginTransmission(Address);
        Wire.write('S');
        Wire.endTransmission();
        Wire.requestFrom(Address, 4);
        while (Wire.available())
        {
          char inChr = Wire.read();
          res += inChr;
        }

        Serial.print("Device responce: ");
        Serial.println(res);
        if(res == "TRUE"){
          return true;
        } else {
          return false;
        }
      }
    }
```

Slave:
```
void normalReceive(uint8_t howMany) {
  while(Wire.available())//if wire is avaliable
  {
    char command=Wire.read();//state read the recived command from master, high or low
    MySerial.print("recived: "); MySerial.println(command);
    switch(command){
      case 'A': digitalWrite(LEDReboot,LOW);
       break;
      case 'B': digitalWrite(LEDReboot,HIGH);
       break;
      case 'S': Wire.write("TRUE");
       break;
      case 'R': resetFunc();
       break;
      default: MySerial.print("recived: "); MySerial.println(command);
       break;
    }
  }
}

void normalRequest() {
  MySerial.println("Request SN");
  Wire.write(sn);
  }
```

now the issue is when I request from the slave it returns the SN rather than the expected `TRUE`

I am very new to I2C so any help is appreciated.

title: send command to Slave and receive response over I2c

Answers: 0


body:
Basically I have three push buttons that correspond to pins 1 to 3 and want to set an efficient code that sets a certain variable into the index whose digitalRead is equal to one. Let us assume that only one button can be pressed at a time.

```
void loop()
 {
 if(digitalRead(1) == 1)
  {
       Variable = 1;
  }
 if(digitalRead(2) == 1)
  {
       Variable = 2;
  }
 if(digitalRead(3) == 1)
  {
       Variable = 3;
  }
 }
```

Is there are more efficient way to do this, like for example using other loops?

I can't wrap around my head with doing this concept using the other loops such as for, do while, while and switch because the nature of the idea feels so unconventional for their uses.

title: I have three pushbuttons where only one can be pushed at a time and want to set a variable for this. How?

Answers: 1

body:
If you can only have one button pushed at a time, you can only end up with at most one of the if statements evaluate to true. In your code, you check button 2 and 3, even if button 1 is pressed. Your code can be a bit faster if you do this:

    if(digitalRead(1) == 1)
    {
      Variable = 1;
    }
    else if(digitalRead(2) == 1)
    {
      Variable = 2;
    }
    else if(digitalRead(3) == 1)
    {
      Variable = 3;
    }


body:
I have an esp32-S2 saola and I am trying to use it as a MIDI usb device. I am trying to use this github repository: https://github.com/chegewara/EspTinyUSB. I am able to run the code on the esp32s2 saola and I am connecting to a mac with a USB cable to pins 19 and 20 but don't get any MIDI input in my computer (mac), in fact my computer is not recognizing the device.

So I guess my actual question is about how to get the esp32s2 recognized as a USB device by the mac. Any clues of what might be happening? Or should connecting  to pins 19 and 20 be enough and I am dealing with a hardware bug?

For the usb cable I took a usb only-power cable apart and then rewired it so it was only data (two middle cables connected to pins 19 and 20). As it was not working I then connected also the usb's power and ground cables with no luck:

[![Soldered USB for esp32-S2][1]][1]

  [1]: https://i.stack.imgur.com/JRxrS.jpg

EDIT: This the documentation for the USB driver on the esp32-S2 and why I think the power cables might not be needed at all (which I tried but didn't work): https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/usb.html

title: How to get the esp32s2 recognized as USB device?

Answers: 0


body:
I am using a nodemcu and running a code using hall effect sensor and ultrasonic sensor with deep sleep except the deep sleep keeps hanging when I use this code. With code taken from online it does not hang. What is wrong with my code Please help me!
```C++[![][1]][1]
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define sleepInterval 30 // 6 minutes

// Update these with values suitable for your network.

const char* ssid = "Thief_3h";
const char* password = "lan";
const char* mqtt_server = "ipaddress";
const char* mqtt_topic = "sensors/test/";
const int trigPin = 2;  //D4
const int echoPin = 0;  //D3
long duration;
float distance_cm;
const int hallSensorPin = 14;                      // connect the hall effect sensor on pin 2
const unsigned long sampleTime = 1000;
float radius = 10;
float velocity;
float rdepth = 5;
float cm;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[75];
int value = 0;
int rpm;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("/test/river/rpm", "starting");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void ultra(){
  delay(2000);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
   // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  //cm = microsecondsToCentimeters(duration);
  distance_cm = ( duration / 29 ) / 2;rdepth;
}

int getRPM()
{
  int count = 0;
  boolean countFlag = LOW;
  unsigned long currentTime = 0;
  unsigned long startTime = millis();
  while (currentTime <= sampleTime)
  {
    if (digitalRead(hallSensorPin) == HIGH)
    {
      countFlag = HIGH;
    }
    if (digitalRead(hallSensorPin) == LOW && countFlag == HIGH)
    {
      count++;
      countFlag=LOW;
    }
    currentTime = millis() - startTime;
  }
  int countRpm = int(60000/float(sampleTime))*count;
  return countRpm;
}

void goToSleep() {
  ESP.deepSleep(1000000 * sleepInterval);
}

void setup(){
  pinMode(hallSensorPin,INPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  //pinMode(D0, WAKEUP_PULLUP);
  delay(1000);
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  delay(100);
   rpm = getRPM();
  velocity = (rpm * 2 * 3.14 * radius)/60;
  ultra();
  delay(1000);
  senddata();
  delay(1000);
  goToSleep();

}

void senddata(){

distance_cm = ( duration / 29 ) / 2;rdepth;

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;

    snprintf (msg, 75, "Velocity:%6.2f Depth:%.2f", velocity, distance_cm);
    Serial.print("Publish message: ");
    Serial.println(msg);

    client.publish(mqtt_topic, msg);
  }
    Serial.print("RPM:");
  Serial.println(rpm);
  Serial.print("Velocity:");
  Serial.println(velocity);
  Serial.print("depth:");
  Serial.println(distance_cm);

}

void loop(){}

```

  [1]: https://i.stack.imgur.com/qgCPE.png

title: Deep sleep hangs when used with D0 pin when we connect to reset pin in nodemcu

Answers: 0


body:
I posted this on StackOverflow but figured I'd post it here too. Sorry in advance if thats not allowed.

I'm trying to get my Arduino Yun to talk to an API that I'm hosting on Azure. I would like to do more complex things in the future, but at the moment I'm just trying to make a GET request over wifi with a single integer as a parameter.

I'm basically just using the example found [here](https://www.arduino.cc/en/Tutorial/LibraryExamples/HttpClient) with the url changed. The example works fine, so as far as I'm aware the issue isn't with my board, its with the interaction with Azure.

HttpClient.get() returns an int as a response (not the regular HTTP response codes, its unique for the library). I got 60 initially, and changing my API to accept http calls (instead of just https) fixed this and made me get a response of 0 instead. 0 is the same response that I get from the working Arduino example, but for some reason I'm not actually getting anything in the body of my response, as the client.available() loop is never entered.

The API works as intended when I go to the url in chrome, so the issue arises somewhere due to how the arduino and azure talk to each other, but I don't know enough about either to figure it out and nowhere that I look seems to point me in the right direction.

Any help would be greatly appreciated, thanks.

title: Arduino Yun not getting response from Azure API

Answers: 1

body: Found the problem, even though I set azure to accept http requests, it was still redirecting to https which the arduino couldn't handle. I've written a python script to offload the requests to the linux processor, and now it works.


body:
I've designed a custom ESP32 board around the WROVER-IE module. To keep power low, I'm using a GPIO-controlled high-side FET to turn off unused peripherals when in deep sleep mode.

The issue: I've pulled up an SSD1306 I2C OLED to this AUX rail. It works great when the device is awake. On setup() I start the OLED via ssd1306.begin() and from that point on, the SCL and SDA pins (22, 21) are always tied to 3.3V until I reset the ESP32.

The I2C bus always being tied to 3.3 ends up bleeding through the pull-up resistors and into the AUX rail, resulting unwanted current draw when in sleep mode.

Is there any way to turn off the internal I2C pullups on the ESP32?

Things I've tried so far:

 - Setting SDA, SCL as OUTPUT and writing LOW (does not work)
 - Setting SDA, SCL as INPUT and/or writing LOW (does nothing)
 - Calling `gpio_hold_dis(gpio_num_t(SDA));`(does nothing)

I'm now trying to dissect the esp32-hal-i2c.c lib but am not finding any fixes. Any help is greatly appreciated!

Edit, added diagram:
[![enter image description here][1]][1]

-N

  [1]: https://i.stack.imgur.com/awTMG.png

title: Can't Turn Off Internal I2C Pull-Ups on ESP32

Answers: 1

body:
I have solved this problem. For anyone who would like to turn off the internal pullups on the I2C bus of the ESP32, you need to disable the entire driver before putting the device to sleep.

First, include the peripheral driver library from the IDF:

    #include "driver/periph_ctrl.h"

Next, call the following before sleep:

    rtc_gpio_isolate(gpio_num_t(SDA));
    rtc_gpio_isolate(gpio_num_t(SCL));
    periph_module_disable(PERIPH_I2C0_MODULE);
    digitalWrite(SDA, 0);
    digitalWrite(SCL, 0);
This will stop the ESP32 from pulling up pins 21 and 22 while in deep sleep. Calling display.begin() or wire.begin() will restartthe driver upon wake.


body:
I'm trying to turn ON/OFF a DC motor from Arduino Iot Cloud. I have to turn it ON/OFF physically and through the cloud, physically it works well but from the cloud it does nothing. I'm using an Arduino MKR WiFi 1010, a L298N Dual full bridge,9V DC motor and a push buttom. Mi circuit is like this [![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/wvWVg.jpg

My code is this

    #include "thingProperties.h"
    #include <FTDebouncer.h>

    #define MOTOR_PIN 2
    #define BUTTON_PIN 5

    FTDebouncer buttons;

    void setup() {
      pinMode(MOTOR_PIN, OUTPUT);
      buttons.addPin(BUTTON_PIN, LOW);
      buttons.init();

      setDebugMessageLevel(2);
      Serial.begin(9600);
      delay(1500);
      // Defined in thingProperties.h
      initProperties();
      // Connect to Arduino IoT Cloud
      ArduinoCloud.begin(ArduinoIoTPreferredConnection);
    }

    void loop() {
      buttons.update();
      ArduinoCloud.update();
      onMotorChange();
    }

    void onPinActivated(int pinNr) {
      // do something according to the _pinNR that is triggered. For instance:
      Serial.println(pinNr);
      toggle = !toggle;
    }

    void onPinDeactivated(int pinNr) {
      // do something according to the _pinNR that is triggered. For instance:
      Serial.println(pinNr);
    }
    void onMotorChange() {
      digitalWrite(MOTOR_PIN, toggle || motor);
      Serial.print("The motor is ");
      if (toggle) {
        Serial.println("ON");
      }
    }

 I tried with a LED and it worked very well, but with the motor doesn't.
This is the code that Arduino creates when I add things to the board, the part of

> thingProperties.h

    // Code generated by Arduino IoT Cloud, DO NOT EDIT.
    #include <ArduinoIoTCloud.h>
    #include <Arduino_ConnectionHandler.h>

    // x-ed id
    const char THING_ID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    const char SSID[]     = SECRET_SSID;    // Network SSID (name)
    const char PASS[]     = SECRET_PASS;    // Network password (use for WPA, or
    use as
    key for WEP)

    void onMotorChange();

    bool toggle;
    bool motor;

    void initProperties(){

    ArduinoCloud.setThingId(THING_ID);
    ArduinoCloud.addProperty(toggle, READ, ON_CHANGE, NULL);
    ArduinoCloud.addProperty(motor, READWRITE, ON_CHANGE, onMotorChange);

    }

    WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Thanks for the help and suggestions. How should I connect the motor or what do I have to change in the code?

title: I can't turn on the motor by Arduino IoT Cloud

Answers: 1

body:
The following code is an example. As I don't know the Arduino Cloud Thing configuration, and I don't have access to the cloud and the Hardware, the
programm is just a guess making some commented assumptions.

There are many assuptions I made, and I can not rely on the schematics you showed me. Eventually you have to redesign the circuit/hardware setup. A L298N is a so called H-Bridge. That needs at least two control pins in1 and in2 connected to arduino outputs to define the direction of the motor. You only connected one of them to an output of the arduino. And one of them to the button and an input of the arduino. (If that part of the schematics is correct.)

If you press the button in1 gets HIGH but the in2 may also get HIGH because your program switches the motor pin to HIGH. That turns the motor off. If one pin is HIGH the other must be LOW to drive the motor. Some L298N modules also need a PWM signal at the enA pin. That sets the speed of the motor with the duty cycle of the PWA signal. Most modules have a pullup at enA and enB so per default the motors are configured for full speed.

    #include "thingProperties.h"
    #include <FTDebouncer.h>

    #define MOTOR_PIN 2
    #define BUTTON_PIN 5

    FTDebouncer buttons;

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

      // !!! PPK: Handler methods added
      buttons.addPin(BUTTON_PIN, LOW, onPinActivated, onPinDeactivated);

      buttons.init();

      setDebugMessageLevel(2);
      Serial.begin(9600);
      delay(1500);
      // Defined in thingProperties.h
      initProperties();
      // Connect to Arduino IoT Cloud
      ArduinoCloud.begin(ArduinoIoTPreferredConnection);
    }

    void loop()
    {
      buttons.update();
      ArduinoCloud.update();
    }

    void onPinActivated(int pinNr)
    {
      if ( pinNr == BUTTON_PIN )
      {
         toggle = !toggle;
         // this assumes that the toggle triggers a change for
         // the motor variable in arduino cloud. If not,
         // the motor variable will not be changed and my code does not work.
         // uncommenting the following line may help, in that case
         // motor = toggle;
      }
    }

    void onPinDeactivated(int pinNr)
    {
      // Intentionally left empty, we only react to the rising edge.
    }

    void onMotorChange()
    {
      digitalWrite( MOTOR_PIN, motor );
      Serial.print("The motor is ");
      if ( motor )
      {
        Serial.println("ON");
      }
      else
      {
        Serial.println("OFF");
      }
    }


body:
I need help I use Touch Sensor(T)v.0 and it always receives a signal not matter I touched it or not.
Here is the code.

    #define TOUCH_PIN 7
    int touchVal = 0;

    void setup() {
       Serial.begin(115200);
       pinMode(TOUCH_PIN, INPUT);
    }

    void loop() {
       touchVal = digitalRead(TOUCH_PIN);
       Serial.println(touchVal);
       delay(200);
    }

title: Touch sensor trouble

Answers: 0


body:
I'm making my own Arduino Mega board with ATmega2560-16AU and CH340G. Jump to the end of the post to find the real question.

I have already ordered a PCB before and it didn't work when I soldered everything in place. I have already asked why that board didn't work on another post here: https://arduino.stackexchange.com/questions/81361/programming-custom-arduino-mega-with-arduino-uno

But I was not satisfied with the answers and started suspecting whether or not the capacitor values were right on my board.

I'm trying to make my own version of RobotDyn's Arduino Mega 2560 Pro so I tried taking the already programmed ch340g and soldering it on my own board and still failed. When I put a previously not programmed ch340g on the RobotDyn board and programmed it using Arduino UNO (Arduino as ISP) the board would work.

So now I ordered a new PCB but haven't ordered any of the passive components yet.

**If any of you could tell me where I could find the values from the schematic below I would really appreciate that.**

https://robotdyn.com/pub/media/0G-00005641==MEGA-PRO-CH340GATmega2560/DOCS/Schematic==0G-00005641==MEGA-PRO-CH340GATmega2560.pdf

title: Arduino Mega Clone CH340G Capacitor Values

Answers: 1

body:
The values can be easy found for generic CH340g and ATmega2560 implementations:

C1,C2 = 100nF, C3,C4 = 22pf but depends on the crystal you use, all others excepting C12 are decoupling capacitors.

C12 has no sense where it's placed, it seems that the schematic is a bad combination of schematics, this capacitor should reset the board at power on but it breaks the reset pulse from CH340g so you cannot program the board without pressing manually the reset pushbutton. Don't place C12 at all.

Also place a short instead of R5 and R6 that are not in the CH340g manual (there is a special mention to connect the pins straight to USB bus), remove R8, is not needed for the same reason, R3 makes no sense, don't place-it.

R2,R4 = 1K, R7,R9 = 10K, R10,R11,R12,R13 = 1K, R14 = 10K , R15 = 1M.

Y1 = 12Mhz, Y2 = 16Mhz

A 22 ohm resistor is missing in your schematic between Y2 XTAL1 and ATMEGA XTAL2 but I don't think it's important.

I hope I didn't missed something

The bare minimum is to remove c12 and maybe R3 and check the values you used for R2, R4 against the above values.

If something would be wrong with CH340g the USB connection would fail but I see no mention about that in your post.


body:
I am using HC-05 bluetooth module for sending large data(80-100KByte) from my phone to arduino-uno.
I had found that this module has a flash chip of 8Mbit  (SST39VF800A) but the datasheet of HC-05 has nothing about buffering of the data to be transmitted or data received.
I need clarity about the buffering of data so that I can change the data rate from the phone for better communication.

title: Does HC-05 module buffers the data received?

Answers: 1

body:
I didn't find any help in any data-sheets I could find, either. So I'd suggest a simple experiment, sending increasingly large packets before the other side attempts to read anything, until some data goes missing. The packets don't even need to be very sophisticated if all you want to know is whether N_received == N_sent and for what maximum 'N'.

Update:

> All bytes were received correctly if there is no delay but with
> increasing delay in reading from HC-05 data was not fully received and
> with delay greater than 800ms only 63Bytes were received.

If you're collecting data as it arrives, then you can't measure the buffering. When you wait until all data has been set before you try to read any, the number of bytes you receive is the buffer size. It sounds like a 63 byte buffer, probably only what the HC-05's hardware UART can buffer, without using any of its memory to increase that.


body:
How do I write a program to wait (delay) until one out of two buttons pressed? I have written a program to wait until a single button is pressed. It's working fine. However, if I extend it to second button it's not working. Pls help on this.

1. `while (digitalRead(bt_Select) == HIGH) {}` -- working.
2. `while (digitalRead(bt_Select) == HIGH || digitalRead(bt_Reject) == HIGH) {}` -- Not working.

Complete code:

```c++
const int bt_Select = 2;
const int bt_Reject = 3;

void setup() {
  Serial.begin(9600);
  pinMode(bt_Select, INPUT_PULLUP);
  pinMode(bt_Reject, INPUT_PULLUP);
}

void loop() {
  Serial.println("Pl input:");

  while (digitalRead(bt_Select) == HIGH {} // This is the line I change

  if (digitalRead(bt_Select) == LOW) {
    Serial.println("Select Button pressed:");
    delay(250);
    Serial.println("");
  }

  if (digitalRead(bt_Reject) == LOW) {
    Serial.println("Reject Button pressed:");
    delay(250);
    Serial.println("");
  }
}
```

title: Arduino while loop with or condition

Answers: 1

body:
`while (digitalRead(bt_Select)==HIGH||digitalRead(bt_Reject)==HIGH){}`

should be

`while (digitalRead(bt_Select)==HIGH&&digitalRead(bt_Reject)==HIGH){}`

The first loop would break only if you press both buttons at once. I believe you are trying to break if either button is pressed.


body:
Apologies, but I have little experience of Arduino etc. Has the "Sim808 init error" been resolved anywhere please? I have tried every example I can find including the ones on these StackExchange pages without success. I started with the GPS test code from:

https://wiki.dfrobot.com/SIM808_GPS_GPRS_GSM_Shield_SKU__TEL0097

Parancey, Marcello, Pooya and Arslan and others have suggested mods to this code so I have tried all of them I think - none working.

Then I watched this video:

https://www.youtube.com/watch?v=fN8fwX3KmsM

which appears to show the code below or similar:

    /*
    ### Get GPS data
    1. This example is used to test SIM808 GPS/GPRS/GSM Shield's reading GPS data.
    2. Open the SIM808_GetGPS example or copy these code to your project
    3. Download and dial the function switch to Arduino
    4. open serial helper
    4. Place it outside, waiting for a few minutes and then it will send GPS data to serial

    create on 2016/09/23, version: 1.0
    by jason

    */
    #include <DFRobot_sim808.h>
    #include <SoftwareSerial.h>

    //#define PIN_TX    10
    //#define PIN_RX    11
    //SoftwareSerial mySerial(PIN_TX,PIN_RX);
    //DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

    DFRobot_SIM808 sim808(&Serial);

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

      //******** Initialize sim808 module *************
      //while(!sim808.init()) {
      while(!sim808.checkPowerUp()) {
          delay(1000);
          Serial.print("Sim808 init error\r\n");
      }

      //************* Turn on the GPS power************
      if( sim808.attachGPS())
          Serial.println("Open the GPS power success");
      else
          Serial.println("Open the GPS power failure");

    }

    void loop() {
       //************** Get GPS data *******************
       if (sim808.getGPS()) {
        Serial.print(sim808.GPSdata.year);
        Serial.print("/");
        Serial.print(sim808.GPSdata.month);
        Serial.print("/");
        Serial.print(sim808.GPSdata.day);
        Serial.print(" ");
        Serial.print(sim808.GPSdata.hour);
        Serial.print(":");
        Serial.print(sim808.GPSdata.minute);
        Serial.print(":");
        Serial.print(sim808.GPSdata.second);
        Serial.print(":");
        Serial.println(sim808.GPSdata.centisecond);

        Serial.print("latitude :");
        Serial.println(sim808.GPSdata.lat,6);

        sim808.latitudeConverToDMS();
        Serial.print("latitude :");
        Serial.print(sim808.latDMS.degrees);
        Serial.print("\^");
        Serial.print(sim808.latDMS.minutes);
        Serial.print("\'");
        Serial.print(sim808.latDMS.seconeds,6);
        Serial.println("\"");
        Serial.print("longitude :");
        Serial.println(sim808.GPSdata.lon,6);
        sim808.LongitudeConverToDMS();
        Serial.print("longitude :");
        Serial.print(sim808.longDMS.degrees);
        Serial.print("\^");
        Serial.print(sim808.longDMS.minutes);
        Serial.print("\'");
        Serial.print(sim808.longDMS.seconeds,6);
        Serial.println("\"");

        Serial.print("speed_kph :");
        Serial.println(sim808.GPSdata.speed_kph);
        Serial.print("heading :");
        Serial.println(sim808.GPSdata.heading);

        //************* Turn off the GPS power ************
        sim808.detachGPS();
      }

    }

I have mounted the DFRobot shield on a UNO and powered through the barrel connector with a 2_amp 5V stabilised power supply. The DFRobot shield shows a red LED power light and pressing reset flashes the blue LED next to it, but nothing else does anything - the serial monitor just repeats "Sim808 init error". In the video the blue "net" LED flashes even before the SIM card is inserted, but my card does not flash. Do I return the board to Farnell and ask for my money back?

Any help greatly appreciated!

title: DFRobot GSM / GPS shield with UNO init error

Answers: 1

body:
The dfrobot board has a weird boot button system that you need to follow to boot up the board. I spent ages attempting to get it to work before I finally achieved anything.

There is a blue led on the board that will flash continuously at different rates if the chip has been correctly powered and is searching for a mobile network.

I noticed that you commented it the while(!sim808.init()) line. This attempts to power the board on automatically, but I do remember something being funny about the implementation.

From doing a while load of research the dfrobot breakout board is not the best hardware implementation of this chip. Actually from memory there aren't any modules I have been able to find which satisfy all my requirements for a project. I'd recommend looking at all the documentation dfrobot provide on their website. Hopefully that should get you working.

Summary: it's an insanely fiddly process to get it working, but the boards are functional


body:
I need help regarding on EEPROM library. Based on the code below, for every 2 sec, I want to store the int for ```increase``` where it will keep increasing by 1. Then, I upload to my Arduino Nano and open Serial Monitor to let it run for about 20 seconds. The serial monitor shows that ```Increase Count:10```.

Then I unplug the Arduino Nano, closed the Serial Monitor, wait for 5 sec, then plug back to computer and turn on the Serial Monitor, then the Serial Monitor start to shows ```Increase Count:1```, which indicated it start to recount again from 1,2,3,4.....

I thought that EEPROM will store the last number it counted which is 10, then when I unplug and plug back it should continue to count from 10, 11, 12, .......

So I would like to ask community whether my execution code and the method is correct or not. If not, can you advice me on how I can execute it correctly. Thank you in advance!

```
#include <EEPROM.h>

int addr=5;
int storagedata;
int increase;

void setup()
{
  storagedata=EEPROM.read(addr);
  Serial.begin(9600);

  timevalue=millis();
}

void loop()
{
  if (millis()-timevalue>=2000)
  {
    increase=increase+1;

    EEPROM.update(addr,increase);
    storagedata=EEPROM.read(addr);

    Serial.print("Increase Count:");
    Serial.println(storagedata);

    timevalue=millis();
  }
}

```

title: How can I read the data stored in EEPROM although is powered OFF based on the simple code I wrote?

Answers: 3

body: The problem is that you fail to initialize `increase` during `setup()`. The variable is undefined when the code enters the `loop()` function, even regardless of whether this is the first boot or not. You should change `storagedata` to a local variable in loop() and use `increase` to keep the value (or vice versa).

body:
`increase` is a global variable and will implicitly initialized with the value zero. Then you are reading the EEPROM data into the variable `storagedata` in `setup()`. And then in `loop()` you are increasing `increase` from zero to one and write that to EEPROM. This value then gets read back into `storagedata`. So now `increase` and `storagedata` have the same value: one. So the counting begins on every reset at 1.

It is unclear, why you even have 2 variables for this. One would be enough (unless you want to test the EEPROM for some reason). **In `setup()` read the data into `increase`.** Then the counting starts with the number in EEPROM.

body:
You never use the value you read in `setup()`.  Go through your `loop()` function one line at a time:

```c
// this does not use the value read in setup, variables are initialized to 0
// so the first time through this loop it will set 'increase' to 1
increase=increase+1;

// you are updating EEPROM with the value of 'increase' which is 1 the first
// time this is executed
EEPROM.update(addr,increase);

// you are reading the '1' that was just written into 'storagedata', you have
// never used the value you read in 'setup()' and have now overwritten it
storagedata=EEPROM.read(addr);
```

I'm not sure why you have two separate variables, but in `setup()` you just need to
initialize `increase` as the value read from EEPROM:

```c
storagedata=EEPROM.read(addr);
increase=storagedata; // start with stored value instead of default 0
```


body:
I am looking for a way to create a true analog output with the arduino uno. I am constantly reading an analog input on a0 pin, all i want to do is send the same signal as output. I know arduino convert the signal into pwm so have can i convert it back to true analog signal.I have a dac too but not getting proper output.

secondly, i want to send same signal by converting it into bits through Ethernet so how do i send and receive the signal.

here is my code for dac.

    #include "Wire.h"
    #include "MCP4725.h"
    MCP4725 MCP(0x60);
    uint32_t microphonePin=A0;
    uint32_t threshold=800;
    const int ledPin=13;

    void setup() {
        Serial.begin(115200);
        pinMode (ledPin, OUTPUT);
        Serial.print("MCP4725 test program: ");
        Serial.println(MCP4725_VERSION);
        if (MCP.begin() == false)
         {
           Serial.println("Could not find sensor");
         }

        MCP.begin();
     }

    void loop() {
       uint32_t sensorValue=analogRead(microphonePin);
       MCP.writeDAC(sensorValue);
       Serial.println(MCP.getValue());
       if (MCP.getValue()>threshold)
        {
          digitalWrite (ledPin, HIGH);
        }
       else{ digitalWrite(ledPin, LOW); }

      }

here is my circuit for audio amplifier

![![here is my circuit for audio amplifier][1]][1]

  [1]: https://i.stack.imgur.com/uZCkP.png

and for the speaker it is connected on DAC out.

FYI:I am able to read the data on my serial monitor form mic and i am also able to so do some operation by just using mic and GUi. I just want to here what i am saying on speaker.

title: Generate a true analog signal using Arduino

Answers: 3

body:
I see that MCP4725 uses Wire library with the default clock of 100,000 kb/sec.

At a rough estimation each call to MCP.writeDAC or MCP.getValue will be at least 32 bits long and you use three of them in each loop.

This one:

       Serial.println(MCP.getValue());
       if (MCP.getValue()>threshold)

is redundant, you should store the value in a temporary variable and use-it in both "print" and "if".

With 10us for each bit that means 1060 us for each loop. The analog read also takes 100us to complete. Over all 1160us minimum.  Is the MCP.getValue() really needed?

That means that your maximum sample frequency is 860hz and the maximum bandwidth of 430hz. I doubt that this is an acceptable bandwidth for a microphone.

Even so, the output signal will not look similar at all, you need a higher bandwidth or to limit,  if possible, the bandwidth of the input signal.

In some limits you can modify the MCP4725 library to use fast wire clock four times faster if it suits your requirements. If you also drop the two MCP.getValue() calls that will be again almost three times faster, up to 5khz.

Also you might search for faster ADC on Arduino.

Update after OP microphone amplifier schematic.

The ADC works only with positive voltages. Short the output capacitor of the amplifier!

body:
**If you want something simpler than the transistor version you can use the following as well, for the other options skip to the --here-- mark**

Oldstyle analog out. this works like a vacuum tube and while I use a LDR you can also use heat or a actual vacuum tube.

You Will Need:

 1. A low resistance LDR or multiple in parallel.
 2. A cardboard box or other box to isolate it from outside interfearance.
 3. A Led or light bulb.

How this works: while a led flashes, a LDR needs a certain time to just to values(often up to around a few ms), so this will stabilize your adc signal.

You send the desired amplitude as a pwm signal to the light source(you probably want to invert your pwm signal) which aims at the LDR. you send your sound frequency through the LDR which now works as a variable resistor.

!!Use low resistance LDR's and a bright light source to get a as low as possible resistance. with (weak)lasers, you can get a normal LDR down to around 1 to 0 ohm. but you will most likely use a led since that has more precise intensity control.
[![Ancient style analog out like vacuum tubes][1]][1]

Another simple method is the inversed of analog read in an Arduino. you basically use multiple output ports with different resistances to get a different output when done right in this case you can get around 257 different volume levels with 8 pins for the amplitude and 1 for the frequency.

**--here--**

--added this here as a simple explaination/guide--
first start with getting your microphone working, see if analogread get's some high values. if it stays at 0, 1 or another low value, then you didn't include a transistor with your microphone to boost the signal.

-For your output start with a project without a microphone to see if it is working right.
you will need to use 2 pins to power your output(excluding ground, assuming single-channel audio).
-the first one you will need for your frequency signal output. the other one is for the amplitude/volume/gain.
-We will use any type of amplifier(transistor) to merge the signals, in this case, I assume a transistor since they are cheap, simple low power, have an easy dynamic range and you most likely have them already.

-this following part can be done in 2 ways I would suggest to try way 1 first since it is most simple, logic, and fast which is great on a slow which is great in a small microprocesssor. where method 2 might seem the exact different way around but is much more precise.

Method 1: connect the frequency output pin to the emmitor pin of the transistor where the + normally goes in.
Connect the amplitude output pin to the base pin of transistor(the trigger)
add a very small capacitor(or a bigger one for tests) between the base and ground this is to convert the adc in analog voltage instead of pulses.
the collector of the transistor which you would normally connect to ground is your output.
you might want to add a small resistor after it especially if you are using it for a audio cable. you can also directly connect a very small speaker to hear the sound
for a cable(be careful withconnection arduino IO to things like a pc) you connect the output as oudio signal and the ground as ground. but check what voltage the other devices audio port supports since arduino's IO pins generate 5v

method 2.
connect the frequency out pin to the base of the transistor,
connect the ADC of the amplitude pin to the emitter pin of the transistor,
connect a capacitor of larger capacity than described in method 1 between the emitter pin of the transistor and the ground of the Arduino.
the collector is again your output, you might to as a resistor to it.
connect a wire from an analog port to the emitter pin of the transistor to read out the voltage in the capacitor for precise voltage regulation.

tinker with the values of the capacitor size.
(you now have an energy pool in front of the transistor if you know the ampere on which you are outputting you can use this to regulate the pool, otherwise you can read it using the analog feedback loop).
important to know is that if you want precise volume you use a larger capacitor, if you want fast volume differences you use a small one and intentionally use more pins to charge it. for an Arduino Uno due to low analog read speeds, I would recommend a larger capacitor or this.

extra stuff when putting your complete system together you might run into speed issues. you should therefor connect the microphone to both a digital and a analog pin. the digital pin constantly monitors the frequency and the analog pin measures the amplitude sometimes(add a small capacitor get that average amplitude).
I would also recommend turning down the analog read precision to prevent checking for all 1024 possible voltages.

for method one you can also add a extra resistor from base to ground and from the amplitude pin to base so you can more precisely calculate the voltages.

**--end of simple version--**

You can try to use 2 pins for the output signal. one of them sends the frequency digital(3 pins if you also want negative polarity audio). this output signal should go to something like a transistor or a digitally controlled potentiometer or amplifier. I will assume you use a transistor since those are most common and quite fast.
the first digital output pin should go to the emitter pin of the transistor
the second pin is used for the volume and will go to the base of the transistor. you might want to add a very small capacitor between the base and ground to reduce frequency interference(to prevent the ADC frequency from showing up in your signal).
the Collector pin would be your analog output suitable for audio.

So in basic steps:

 1. split  the signal into a digital frequency and volume. (the digital frequency can be made by seeing a signal above a certain value as high and the other as low, this works best when you use both the positive and the negative side of the soundwave otherwise you will need to chose a very low number)(the analog frequency is literally your analog value for the positive only audio, and value-512 (*-1 for negative numbers to get only positive numbers) for the full audio if you include the negative audio)
 2. Send the frequency signal to the emitter pin of your diode.
 3. Send the amplitude signal to the base pin of your diode.
 4. Connect a small capacitance between the ground and the amplitude signal.

however What Dorian said are things you might want to keep track of as well since they often indeed tend to kill of such projects because you very easily run into speed limits(analog read in a arduino ar basically 1024 digital reads(literally)).

So some tips for your cirquit:

 1. If your microphone doesn't have a transistor connected to boost the signal add it since otherwise the analog port of the arduino uno is not sensitive enough to measure the signal. you can and should fist test this using a project where you constantly analogread and print it. you can add a small capacitor between the analog port and the ground to just measure the voltage.
 2. use diodes to rectify your microphone signal, your arduino can only read positive voltage, the microphone also sends negative voltage. redirect the negative voltage to the arduino ground (you can invert the negative voltage using a transistor or some other methods to read the negative voltages as well, but I would recommend just sending the negative signals to ground since this would give the near full range as well)
 3. use 2 microphones one for frequency and one for amplitude. or connect the output of your microphone to both a analog and a digital port. you might in the last case want to use resistors to lower the current going to them this makes sure both get a accurate voltage.
 4. avoid as manny analog reads as possible, storing it is better than calling it twice
 5. use c/c++ bitmanipulations for IO, not only is it more easy to program larger systems when using them, they are also a lot faster( arduino uno has the PORTB, PORTD, PIND, PINB, DDRD, DDRB, and the a and c version(however you most often only use the D and B) predefined. bitmanipulations require very little knowledge compared to other programming methods as long as you know the port addresses, which again for the arduino uno are predefined search: arduino uno bit manipulations or arduino uno PORTB.
 6. you can sample the sound frequency using a digital port at a higher frequency than the amplitude. since sound often doesn't jump to fast in amplitude in a way that you would clearly notice. this means you could read the sound with a IO pin and write it on another IO pin, additionally you could once every interval measure the amplitude to use which makes your code a lot faster.

there are way more ways but these are easy to do.
for sending it trough internet however you need to be careful that the module you use processes those things itself so that you do not just connect a internet port since that would slow your system down even more.

For those wanting a ms paint sketch:

version one:
[![Version one resistors are optional, feedback look can be added][2]][2]

version two:
[![Version 2 can be made without resistors or feedbackloop][3]][3]

note that I just used random images from the internet you can use any capacitor and transistor that works for this, I did not check if this transistor is right for this, but it had a nice layout description in the image. the capacitor doesn't need to be ceramic, but this way you see clear legs and I don't need to focus on polarity. in general, all cheap 5v compatible transistors should work and all capacitors if they are not way too big or too small for your load.

I used descriptions rather than logos, so anyone should be able to read it as long as they have software to tell the differences in colour if they are colourblind.

**How it works**
*The transistor sends the voltage from the emittor to the collector(or the other way around based on the transistor you use). so in method 2 which also works for mosfets. we send a adc signal to the capacitor to charge it to a voltage, you should however also add a resistor to ground or a cable to a analog port, because otherwise the voltage can just build up to 5v. this capacitor which we charge becomes a analog power source. we connect it to the emmitor(or pin which you normally connect on the + side). so if we where to open up the transistor(close the cirquit) by putting a positive voltage on the base or trigger pin then you would get a output of the voltage that you put into it. so if you play the frequency digital on the base completely opening and closing the transistor then you get a voltage output similar to the voltage in the capacitor.
method one works more difficult but a transistor opens up relative to the current going from base to exit, so we use the pwm and a small capacitor to get a analog voltage on it only partly opening it, then we send the digital audio signal into the pin that you normally connect to 5v and you get that frequency payed with the transistor acting as a variable resistor*
So method one uses a transistor as a variable resistor
And method two uses the transistor or fet as a switch which uses the voltage build up in the capacitor, doesn't actually need to use pwm if you use a feedback loop.
[![Basic True analog without the transistor to create the frequency(for method 2 only)][4]][4]

  [1]: https://i.stack.imgur.com/R3yEn.png
  [2]: https://i.stack.imgur.com/SW0ZB.png
  [3]: https://i.stack.imgur.com/aANAj.png
  [4]: https://i.stack.imgur.com/DsJum.png

body:
for your case(**audio**) there also is https://github.com/connornishijima/arduino-volume1
or the volume library
to install it you can use the git to get the files, or if you use the arduino ide, you can open the arduino ide and in the software go to helptools(4thoptionoftopmenubar)->Manage Libraries-> {type volume in the search bar}-> install the library called volume. you can also chose volume 2 or volume 3, see the git for the differences.

this however still uses micro pwm, so it is not true analog, but for audio it works. also note that on low volume the audio will be affected. I recommend only using the volumes between 100 and 255.

this method requires no additional hardware, and works well for audio, to send the audio to other devices however I would just send the audio values and not the audio output.

NOT TRUE ANALOG, but it solves your problem, the methods I described in my other answer will work well for most other analog cases, or even adding a small capacitor at that ma output might work in many cases. however since audio needs the voltage to change really fast this method works well, also since the volume is read by your mind instead of a digital device it has a normalization filter built in and a low sampling speed as well as sensitivity to only low frequency(how human brains and ears work) which will make the volume sound natural.

If you have a separate arduino for the volume or a well optimized code you can also avoid interrupts to get a higher frequency, this method is harder and requires you to know what your code does but allows for better quality volume(speciffically at low volume levels)

**alternatively you can use a shiftregister** and connect resisters to the outputs, for example 12800, 6400,3200,1600,800,400,200,100. like a bit but reversed, since the audio gets louder with a lower resistance. you can use other values, just don't make them to low, since arduino outputs 5v and 3.3v, both are much higher than your average headphone connection, also turning on multiple parallel resistors at once gives a lower resistance. for example if we turn on the 100 ohm and the 200 ohm resistor we get a output of around 66,66666666 ohm since the output is 1/((1/Resistor1)+(1/Resistor2)+(1/ResistorETC)
like this a singe 8 bit shift register can deliver 256 different volume levels as true analog. if you have ports free on your ardruino you can also use those instead of a shift register, however be sure to use bitmanipulations and not digitalwrite, since the later one is slow.
1 pin = 2 volume levels
2 pin = 4 volume levels
3 pin = 8 volume levels
4 pin = 16 volume levels
5 pin = 32 volume levels
6 pin = 64 volume levels
7 pin = 128 volume levels
8 pin = 256 volume levels
etc.
this method can give true analog voltage if you know the resistance at the output, a speaker however differs in resistance based on many factors, but doing this will still give you good volume control like actual analog voltage, even though it is actually more analog amperage. to get voltage content the terminal connected to all the outputs of the resistors to another resistor to generate a voltage divider, this works for things like vga displays, however for things like audio, lights, etc. you should jus directly take the output of the resistors