message:
Arduino Due 16U2 Programming Port Documentation

Are there any documentation or whatsoever on what the 16U2 does when trying to program the Sam3X in Arduino Due?

I reallynwant to know what's wrong with my made up board but can't get pass the No Device Found problem.

Been searching it but I can't find any. I can't understand the hex file burned on the MCU.

body:
The 16u2 is merely acting as a usb to UART adapter. You can just as well use an ft232 or other serial adapter.

Of course the 16u2 needs firmware installing on it in order to function. That firmware is available (possibly bundled with the ide, or for download, not sure at the moment).


sender: user

message:
Pin Mappings from Arduino Metro M4 Express Board to SAMD51

After looking through the internet, I coudln't find any document (not in the datasheet, no nice graphics on google image searches) that showed the mapping between the Arduino breakout board and the SAMD51. Does such a mapping exist?

I'm answering my own question below, but perhaps someone knows of a better table.

sender: assistant

body:
Edit: Since writing my answer, I found this [Google Sheet](https://docs.google.com/spreadsheets/d/1hWHiM1Sk-gxcUmfgm5XQctqf9yaOnkmY66cwd_sRCiY/edit#gid=0) that does a much better job.

Arduino has some schematics on their website. [Here's an image](https://cdn-learn.adafruit.com/assets/assets/000/053/093/original/adafruit_products_schem.png?1524085993) I used to build the following list below.

There are in this format: Arduino Pin | SAMD51 Pin | SAMD51 Description

## Analog

    A0 3 PA02/EINT2/AIN0/VOUT0
    A1 14 PA05/I5/AIN5/VOUT1/SERCOM0.1
    A2 15 PA06/I6/VREFC/AIN6/SERCOM0.2
    A3 13 PA04/I4/VREFB/AIN4/SERCOM0.0
    A4 11 PB08/I8/AIN2/SERCOM4.0
    A5 12 PB09/I9/AIN3/SERCOM4.1

## Digital

    D0 44 PA23/I2C/I7/SERCOM3.1 +5/SOF/I2SFS1
    D1 43 PA22/I2C/116/SERCOM3.0+5.1/I2SDI
    D2 40 PB17/SERCOM5.1/I2SMCK0
    D3 39 PB16/I0/SERCOM5.0/I2SCK0
    D4 26 PB13/I13/SERCOM4.1/I2SMCK1
    D5 27 PB14/I14/SERCOM4.2/PCC8
    D6 28 PB15/I15/SERCOM4.3/PCC9
    D7 25 PB12/I12/SERCOM4.0/I2SCK1
    D8 42 PA21/I5/SERCOM3+5.3/I2SDO
    D9 41 PA20/I4/SERCOM3+5.2/I2SFS0
    D10 37 PA18/I2/SERCOM1+3.2
    D11 38 PA19/I3/SERCOM1+3.3
    D12 36 PA17/I2C/I1/SERCOM1.1+3.0
    D13 35 PA16/I2C/I0/SERCOM1.0+3.1
    GND
    AREF 4 PA03/I3/VREFA/AIN1
    SDA 63 PB02/I2/AIN14/SERCOM5.0
    SCL 64 PB03/I3/AIN15/SERCOM5.1

## SPI

    MOSI 29 PA12/I12/I2C/SERCOM2.0+4.1
    SCK 30 PA13/I13/I2C/SERCOM2.1+4.0
    MISO 31 PA14/I14/SERCOM2+4.2

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


message:
Can someone help me solve a loop question and a couple others for my program?

I am currently working on the elegoo uno r3 smart car kit that uses arduino. I have written some code and it works for the most part but I could use some help. I am trying to figure out what i need to do to get my square program to loop and continue to until the stop button is pressed. Can anyone help me out please? Also, I have 2 programs in it. One is for obstacle avoidance which is controlled by button 2 and the square program which is controlled by button 3. The stop button will be used to stop either program. I will put the code below.

    #include <Servo.h>

    //#include <ArduinoJson.h>

    //     Left motor truth table
    //Here are some handy tables to show the various modes of operation.
    //  ENB         IN1               IN2         Description
    //  LOW   Not Applicable    Not Applicable    Motor is off
    //  HIGH        LOW               LOW         Motor is stopped (brakes)
    //  HIGH        HIGH              LOW         Motor is on and turning forwards
    //  HIGH        LOW               HIGH        Motor is on and turning backwards
    //  HIGH        HIGH              HIGH        Motor is stopped (brakes)
    #include <IRremote.h>

    //#include <Servo.h>
    Servo myservo;      // create servo object to control servo
    //Servo servo;             //  Create a DC motor drive object
    //#include <stdio.h>

    //#include "HardwareSerial.h"

    int Echo = A4;
    int Trig = A5;

    //ir remote button values/ data sheet===========================
    #define f 16736925    // FORWARD
    #define b 16754775    // BACK
    #define l 16720605    // LEFT
    #define r 16761405    // RIGHT
    #define s 16712445    // STOP
    #define KEY2 16750695 //obstacle avoidance mode
    #define KEY3 16756815 //square mode

    #define KEY_STAR 16728765
    #define KEY_HASH 16732845

    // define IO pin
    #define ENB 5
    #define IN1 7
    #define IN2 8
    #define IN3 9
    #define IN4 11
    #define ENA 6
    #define RECV_PIN 12
    #define ECHO_PIN A4
    #define TRIG_PIN A5
    #define LED_Pin 13
    #define carSpeed 250
    int rightDistance = 0, leftDistance = 0, middleDistance = 0;

    IRrecv irrecv(RECV_PIN);

    decode_results results;

    void forward(){

      analogWrite(ENA, carSpeed);
      analogWrite(ENB, carSpeed);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      Serial.println("Forward");
    }

    void stop_r() {
      digitalWrite(ENA, LOW);
      digitalWrite(ENB, LOW);
       /* digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);*/
      Serial.println("Stop!");
    }

    void back() {
      analogWrite(ENA, carSpeed);
      analogWrite(ENB, carSpeed);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      Serial.println("Back");
    }

    void left() {
      analogWrite(ENA, carSpeed);
      analogWrite(ENB, carSpeed);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      Serial.println("Left");
    }

    void right() {
      analogWrite(ENA, carSpeed);
      analogWrite(ENB, carSpeed);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      Serial.println("Right");
    }

    void stop_() {
      digitalWrite(ENA, LOW);
      digitalWrite(ENB, LOW);
       /* digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);*/
      Serial.println("Stop!");
    }

    //Ultrasonic distance measurement Sub function
    int getDistance() {
        digitalWrite(Trig, LOW);
        delayMicroseconds(2);
        digitalWrite(Trig, HIGH);
        delayMicroseconds(10);
        digitalWrite(Trig, LOW);
        return (int)pulseIn(Echo, HIGH) / 58;
    }

    //--------------------ZIG ZAG----------------------------------------------------

    //init the car
    void setup() {

      //pinMode(IN1, OUTPUT);     //set IO pin mode OUTPUT
      //pinMode(IN2, OUTPUT);
     // pinMode(ENB, OUTPUT);
     // digitalWrite(ENB, HIGH);  //Enable left motor

      myservo.attach(3);  // attach servo on pin 3 to servo object
      Serial.begin(9600);
      pinMode(Echo, INPUT);
      pinMode(Trig, OUTPUT);

      pinMode(IN1, OUTPUT);     //set IO pin mode OUTPUT
      pinMode(IN2, OUTPUT);
      pinMode(ENB, OUTPUT);
      digitalWrite(ENB, HIGH);  //Enable left motor

      pinMode(ENB, OUTPUT);
      pinMode(IN1, OUTPUT);
      pinMode(IN2, OUTPUT);
      pinMode(IN3, OUTPUT);
      pinMode(IN4, OUTPUT);
      pinMode(ENA, OUTPUT);
      digitalWrite(ENA, HIGH);//forward
      digitalWrite(ENB, HIGH);

      pinMode(IN3, OUTPUT);     //set IO pin mode OUTPUT
      pinMode(IN4, OUTPUT);
      pinMode(ENA, OUTPUT);
      digitalWrite(ENA, HIGH);  //Enable right motor

      //attachInterrupt(0,CHECK_IR,CHANGE);

      irrecv.enableIRIn(); // Start the receiver

      stop_r();

      //pinMode(IN3, OUTPUT);     //set IO pin mode OUTPUT
      //pinMode(IN4, OUTPUT);
      //pinMode(ENA, OUTPUT);
      //digitalWrite(ENA, HIGH);  //Enable right motor
    }

    //main loop============================================================================================================================
    void loop() {

      if(irrecv.decode(&results)){
        Serial.println(results.value);
        irrecv.resume();
      }
      delay(100);

      //square();

     if (results.value == KEY2){
         digitalWrite(ENA, HIGH);
         digitalWrite(ENB, HIGH);
        obstacleavoidance();
        //irrecv.resume();
      } else if (results.value == s){
        stop_r();
        results.value = 12;
        //irrecv.resume();
      }

      if(results.value == KEY3){
        digitalWrite(ENA, HIGH);
         digitalWrite(ENB, HIGH);
        square();
        //irrecv.resume();
      }else if (results.value == s){
        stop_r();
        results.value = 12;
        //irrecv.resume();
      }

     /* if(results.value == KEY3){
        square();
      } else if (results.value == s){
        stop_r();
      }*/

     /*if (key_pressed == KEY2){
        obstacleavoidance();
      }
      while (key_pressed == s){
        stop_r();
      }*/

    }

    void get_keys(){

    }

    void CHECK_IR(){
      if(irrecv.decode(&results)){
        Serial.println(results.value);
        //key_pressed = results.value;
        irrecv.resume();
      }
      delay(100);
    }

    void square (){
      Serial.println("square");
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);   //go forward
      delay(1000);

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);   //stop
      delay(1000);

      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      delay(660);              //right wheel turning forward

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);   //stop
      delay(1000);
    }

    void obstacleavoidance(){

      myservo.write(90);  //setservo position according to scaled value
        delay(500);
        middleDistance = getDistance();
        //delay(100);
        if(middleDistance <= 20) {
          stop_r();
          delay(500);
          myservo.write(10);
          delay(1000);
          rightDistance = getDistance();

          delay(500);
          myservo.write(90);
          delay(1000);
          myservo.write(180);
          delay(1000);
          leftDistance = getDistance();

          delay(500);
          myservo.write(90);
          delay(1000);
          if(rightDistance > leftDistance) {
            right();
            delay(360);
          }
          else if(rightDistance < leftDistance) {
            left();
            delay(360);
          }
          else if((rightDistance <= 20) || (leftDistance <= 20)) {
            back();
            delay(180);
          }
          else {
            forward();
          }
        }
        else {
            forward();
        }
    }

body:
Hannah, people that write software are inherently lazy, so because your code does the same thing over and over its quite alien to most people on here :)  As a rule of thumb if you copy and paste it more than once it should be a function.

You have functions at the top of the code to move the car, but you don't seem to use them.  Using them will make the code shorter, but also much more readable.

In your `setup()` function you are repeatedly setting the pinMode of pins.  Group all your `pinMode()` call and then use the pins.  Again this will make the code easier to read and less likely to break when you change it.

So back to your question, how to you make the bot run around in squares until you press a button?  Well it will be something like this:

    void square ()
    {
      while (condition)
      {
        for (int side = 0; side < 4 && condition; ++side)
        {
          forward ();
          delay (1000);
          stop ();
          right ();
          delay (660);
          stop();
        }
      }
    }

You need to work out a way to tie make `condition` be updated by your stop button, I don't want to do everything for you.

Hope that helps, if not shout out.


message:
Using Library LoRa.h with Wemos D1 R2 for Lora Shield

I have been using Lora Shield for communication between server and multiple sensor with arduino . But recently i must save data received to Cloud . So i choose Wemos D1 R2 that have similar board with arduino . But when i upload example code , LoraReceiver can'nt get data . Can Anyone Solve my problem ?

body: When you change boards you must also update your pin connections in software.  When you initialize the Lora Shield, you probably specify a few data pins.  Those pin numbers cannot be assumed to remain the same on the new ESP8266 based board.  Furthermore, the ESP8266 board is 3.3v logic while Arduinos are 5v logic so you must ensure that the shield and sensors will also work with 3.3v.


message:
Circuit with a tip120 works with a Led but, don't with a vibrator motor

I have 2 Arduino Nano's. One with RF transmitter and the other with an RF receiver of 433 MHz and both have two 3v vibration motors. I want one Arduino to send a string to the other Arduino to let him know which vibrator he needs to vibrate. The connection works, when the Arduino with the transmitter sends a message, the other Arduino gets the message and sets pin 2 (the pin for the vibrator via a tip120) HIGH. But the vibrator does not vibrate. When I change the vibrator with a led it works, the led goes on and off. So why does the led work but the vibrator motor not?

I use the VirtualWire library for the RF modules.

Here is the code and I also provided a summary:

Summary: This is the receiver Arduino code. `message_received` is the string variable where the message is stored and in the loop, that message is to check what it contains. And then the led goes off and on.

```
#include <VirtualWire.h>

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = 255; // the size of the message

String message_received;

String message_go_on = "GO_ON";
String message_go_off = "GO_OF";

void setup() {
  Serial.begin(9600);
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
  Serial.println("RECEIVER MONITOR");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (vw_get_message(message, &messageLength)){ // Non-blocking
    Serial.print("Incoming message: ");
    for (int i = 0; i < messageLength; i++){
      Serial.write(message[i]);
      message_received += (char)message[i];
    }
    Serial.println();
  }
  if(message_received == message_go_on){
    digitalWrite(2, HIGH);
  }
  if(message_received == message_go_off){
    digitalWrite(2, LOW);
  }
  message_received = "";
}
```

----------

Summary: This is the code of the transmitter. I have 2 messages that I want to send every 200 milliseconds. The send method is for the transmitter and it needs a char array so I change the string to a char[] with a toCharArray() method.
```
#include <VirtualWire.h>

String message_go_on = "GO_ON";
String message_go_off = "GO_OF";

char buf1[VW_MAX_MESSAGE_LEN];
char buf2[VW_MAX_MESSAGE_LEN];

void setup() {
  Serial.begin(9600);
  vw_setup(2000); // Bits per sec

  message_go_on.toCharArray(buf1,VW_MAX_MESSAGE_LEN);
  message_go_off.toCharArray(buf2,VW_MAX_MESSAGE_LEN);
  Serial.println("TRNASMITTER MONITOR");
}

void loop() {
  send(buf1);
  delay(200);
  send(buf2);
  delay(200);
}

void send (char *message)
{
  vw_send((uint8_t *)message, strlen(message));
  vw_wait_tx(); // Wait until the whole message is gone
}
```

The circuit of the receiver Arduino:
[![The circuit of the receiver Arduino][1]][1]

I think the vibrator might be undersupplied but why would the led work?

Thanks for the help!

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

body:
The TIP120 is possibly the worst transistor you could choose for this job. It is a Darlington Pair transistor, and those have a (relatively) massive forward drop of between 2 and 4 volts. It works with the LED because an LED needs a tiny amount of current to operate compared to the motor and has a fixed forward voltage of its own. The motor doesn't, and is being starved of power by that huge voltage drop (it's getting maybe half the voltage it should be getting).

Throw away that TIP120 and replace it with a decent N-channel MOSFET - one with a "threshold voltage" (VGS) of 2.5V or less ideally.


message:
How can I switch from ArduinoOTA to ESPhome safely?

I have an ESP8266 (ESP-12F), which I flashed with [ElegantOTA][1], a variation of [ArduinoOTA][2] with just a different interface. I never managed to flash anything else, so it's a bare OTA I flashed using a dedicated programmer. The device has not even pins for further flashing.

The ESP is already in a device that I would like to avoid opening, and I would like to avoid soldering pins and flashing locally the [ESPhome][3] firmware (which is also configured for future OTA).

I'm not sure the bootloader or whatever is used to self-flash the chip is compatible. How can I do it?

Or should I just open the ArduinoOTA webpage, upload a ESPhome firmware and go on?

  [1]: https://github.com/ayushsharma82/ElegantOTA
  [2]: https://github.com/jandrassy/ArduinoOTA
  [3]: https://esphome.io

body: I've found that when a flash fails with a space error, I've usually specified the wrong flash size option in Arduino (under Tools->Flash Size).  Depending on the version of Arduino you're using, try picking the one with "No SPIFFS" or FS:none that matches the size of your flash memory.  Once you make that change, recompile and reupload.


body:
I compiled a minimal ESPhome firmware, with only OTA and wifi captive portal, and I tried to upload it (300 KB, more or less).

Initially I got the error "Space is not enough", but then after rebooting the ESP8266 and trying again, it worked.

So as said by dandavis, different firmwares are compatible in regard to OTA capabilities and it's possible to cross-flash.


message:
Simple ESP32 MCP2515 CAN code to VP230

I have a working code that uses MCP2515 CAN module. Wanted to minimize the size of required modules, so I went with VP230 CAN transceiver and I'm having trouble relaying the same data through different libraries. All it has to do is log in with the first ''payload1'' and send voltage instruction with ''setdefaultvolt''. Any help is greatly appreciated.

**MCP2515** code:

```
#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <SPI.h>
#include <Wire.h>

//pins:
#define CAN_INT 17                                                                         // Set INT to pin 2
MCP_CAN CAN(5);                                                                         // Set CS to pin 4 (D2)

void setup()
{
  Serial.begin(115200);
  delay(100);
  pinMode(17, INPUT);                                                                       //Setting pin 2 for /INT input

START_INIT:
  if(CAN_OK == CAN.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ))
  {
  }
  else
  {
    delay(100);
    goto START_INIT;
  }
  CAN.setMode(MCP_NORMAL);                                                        //set operation mode to normal so the MCP2515 sends acks to received data.
  unsigned char payload1[8] = {0x16, 0x50, 0x71, 0x07, 0x97, 0x089, 0x00, 0x00};  //login data with serial number
  CAN.sendMsgBuf(0x05004804, 1, 8, payload1);                                     //send login data

  unsigned char setdefaultvolt[5] = {0x29, 0x15, 0x00, 0xFE, 0x10};              //this is the command for setting the default output voltage (Last two bytes, LSB first). 16 80 is the maximum voltage of 57.6 V
  CAN.sendMsgBuf(0x05019C00, 1, 5, setdefaultvolt);                              //send setup data
}

void loop()
{
}
```
**VP230** code:
```
#include <CAN_config.h>
#include <ESP32CAN.h>

CAN_device_t CAN_cfg;

#include <SPI.h>
#include <Wire.h>

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

  CAN_cfg.speed=CAN_SPEED_1000KBPS;
  CAN_cfg.tx_pin_id = GPIO_NUM_19;
  CAN_cfg.rx_pin_id = GPIO_NUM_23;

  CAN_frame_t my_frame;
  CAN_frame_t my_frame2;

  ESP32Can.CANInit();

  my_frame.FIR.B.FF = CAN_frame_ext;
  my_frame.MsgID = 0x05004804;
  my_frame.FIR.B.DLC = 8;

  my_frame.data = {0x16, 0x50, 0x71, 0x07, 0x97, 0x089, 0x00, 0x00};

  ESP32Can.CANWriteFrame(&my_frame);

  my_frame.FIR.B.FF = CAN_frame_ext;
  my_frame.MsgID = 0x05019C00;
  my_frame.FIR.B.DLC = 5;

  my_frame2.data = {0x29, 0x15, 0x00, 0xFE, 0x10};

  ESP32Can.CANWriteFrame(&my_frame2);

}

void loop()
{
}
```

body:
Just spotted this, but if any one else finds it: You have one CAN speed set to 125kb on MCP2515 the other to 100kb on the VP230.

That's why you won't get anything!

Mark


body:
from datasheet ...CAN controller at speeds up to 1 Mbps.

On the SN65HVD230 and SN65HVD231, pin 8 provides three different modes of operation: high-speed, slope
control, and low-power modes. The high-speed mode of operation is selected by connecting pin 8 to ground,
allowing the transmitter output transistors to switch on and off as fast as possible with no limitation on the rise
and fall slopes.


message:
send data from serial to client

I am using Arduino Yun to send data from serial to client, and it's partially working because I only get short data delivered, while long data (like more than 100Byte) get the most of them lost.

on the other hand when I use the softwareSerial example in the IDE to send from serial to serial it works with no data lost. I am trying to do like the example as possible with the serial to client code using client.write(mySerial.read()) but not working as expected!

 what am I missing? I guess client.write doesn't hold long data or is too slow to transmit. How can I solve this or even another way to achieve my point?

I am sending the data (as a file content on one time, not in sequence- it's a must) from pc using terminal Programm sscom32e. I am receiving the data as client using netcat on the same pc (Yun and PC connected with RS232-USB and are on the same network).

```
#include <SoftwareSerial.h>
#include <Bridge.h>
#include <BridgeClient.h>

#define PORT 9000
// Make the client connect to the desired server and port
IPAddress addr(192, 168, 0, 100);

//not all pins on the yun are assignable as rx/tx
SoftwareSerial mySerial(10, 11); // RX, TX

// Define our client object
BridgeClient client;

int led = 4;
bool Connected;
void setup() {
 pinMode(led, OUTPUT);

 // set the data rate for the SoftwareSerial port
 mySerial.begin(9600);
 mySerial.println("Hello, world?");
 mySerial.setTimeout(20000);

 // Start Bridge
 Bridge.begin();
 //delay(1000);

 // Start Console communications and wait for port to open:
 //Console.begin();
 //delay(1000);

 //Console.println("Client connecting on port 9000");
 Connected = false;

 //delay(1000);
 //connectToClient();
 connectToClient();
}

void loop() // run over and over
{
 while (mySerial.available()) {

 client.write(mySerial.read());

 //mySerial.read();
 }

}

void connectToClient() {

 // is a connection currently active?
 if (!Connected)
 {
 // not currently connected, try to establish a connection
 client.connect(addr, PORT);
 delay(1000);

 if (client.connected())
 {
 // send something to the client
 client.println("hello server!");
 digitalWrite(led, HIGH);
 }
 else
 {
 connectToClient();
 digitalWrite(led, LOW);

 }
 }
}

```

body: I believe now that serial.read and client.write cann't work together at the same time efficiently. when client.write starts, serial.read stops and therefore data get lost. Now I am saving the serial.read in 3 arrays of each 255Byte, when they are full or I receive an end-marker I call client.write to send them and Serial stops anyway. This works but for max. 765 Byte data. I can't do more arrays to save the serial data due to the limited memory of the module, which means it's not the best solution right now. I will post the code for this later when I optimate it. the other Idea coming to my mind is to send orders to the serial to wait until client.write finishes and then continue receiving and so on. Is this possible? Is Serial that flexible? How could I communicate with Serial to wait and to continue?


message:
Help me get my robotic car to work?

So I recently followed a tutorial on youtube and I've assembled the car and connected all the wires.

What I've done:
[Car][1]

Now in the video, when the guy connects all the wires, the wheels constantly turn while in mine they're completely still. Is this a problem? Is it an indicator of some connection issue or something else?

For context, here is the schematic:
[![Schematic][2]][2]

  [1]: https://i.stack.imgur.com/WPDvZ.jpg
  [2]: https://i.stack.imgur.com/SIvPR.png

body: The ENA/ENB (enable A/B) pins have to be at HIGH level (either hardwired or set by an mcu). There exist lots of different driver boards (mostly based on the same ICs) which sometimes don't provide this enable pin, but they are "enabled" by default. You didn't tell us which board you use, but you'll know.


body:
I don't know your motor-driver board but here are several things to check out, based on your schematic:

- The motor driver board has two enable pins. Neither of them is connected. It would make sense that these pins turn the two motors on and off.
- The pin marked 12v is connected to +5v.
- The linked photo of your car shows two 9v "Transistor" batteries. This kind of battery in particular is not capable of supplying much current and they most likely unable to turn the motors.


body:
Check These:

As you have used two power source you must **common ground them**.

**You must check L298N motor drivers enable pins.As you have only 4 wire to motor driver from arduino.So you must give EnA and EnB pin 5v/HIGH or  You can give it (0-255) pwm value from arduino to control the speed of your motor **

If wiring is ok then you must go throw you Sketch.


message:
Which Datasheet applies to the ATmega328p?

While researching minimum-voltage requirements of Uno, I came across what appear to be two datasheets for the ATmega328P:

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

Specifically, the power requirements (and processor speed for each power level) differ. How could I know which datasheet applies to the chip I'm using? I'm actually using an Arduino clone:

[![enter image description here][1]][1]

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

body:
You have ATmega328P-AU. Classic Uno with DIP socket has ATmega328P-PU. Arduino Uno SMD has Atmega328P-MU.

The automotive versions ATmega328P-15AZ and ATmega328P-15MZ have separate datasheet starting in Revision History with

> Creation of the automotive version starting from industrial version
> based on the ATmega48P/88P/168P/328P datasheet 8025F-AVR-08/08.
> Temperature and voltage ranges reflecting automotive requirements.


body:
I think there is an error concerning the information about supply voltages. At least it's not consistent: microchip has separate websites for [ATmega328][1] and [ATmega328p][2]. On the site for the ATmega328p it says: "The device operates between 1.8-5.5 volts". But the [datasheet][3] they link on the exact same site says:
    "Operating voltage: 2.7V to 5.5V for ATmega328p".

This is not just confusing, but contradictory and so I think the only one who can say for sure is the manufacturer.

Actually, the specified supply voltages are determined by the manufacturer by testing at various levels, but the device **might** operate even below or above this range (of course, a developer has to comply with the specs). The 2.7V rating could be an old spec and in the meantime they might have found out that the device indeed runs fine even at 1.8V. Maybe, they have just missed to apply this change everywhere this rating is mentioned, but that's just a guess.

  [1]: https://www.microchip.com/wwwproducts/en/ATmega328
  [2]: https://www.microchip.com/wwwproducts/en/ATmega328p
  [3]: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf


message:
SoftwareSerial interfers with TVout

I'm using an Arduino uno and a Nodemcu for making a game with the phone as a WiFi controller.
The Nodemcu is the server that receives inputs from the phone connected via WiFi and sends to the Arduino a signal via SoftwareSerial.
The Arduino reads the signal and then decides what to do and sends output to TV.
It works good but every time the nodemcu sends something, the TV signal gets distorted.

note: the code for the game is not completed

code for Nodemcu:
```c++
#include<SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "SpaceInvaders";
const char* password = "12345678";

IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
SoftwareSerial SUART(4, 5); //SRX=Dpin-D2; STX-DPin-D1
ESP8266WebServer server(80);

void setup() {
  SUART.begin(115200);
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(local_ip, gateway, subnet);
  server.on("/", handle_OnConnect);
  server.on("/0", nope);
  server.on("/1", left);
  server.on("/2", right);
  server.on("/3", left_right);
  server.on("/4", shoot);
  server.on("/5", left_shoot);
  server.on("/6", right_shoot);
  server.on("/7", left_right_shoot);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handle_OnConnect() {}

void nope()             {SUART.print(0); server.send(200, "text/html", " "); Serial.println(0);}
void left()             {SUART.print(1); server.send(200, "text/html", " "); Serial.println(1);}
void right()            {SUART.print(2); server.send(200, "text/html", " "); Serial.println(2);}
void left_right()       {SUART.print(3); server.send(200, "text/html", " "); Serial.println(3);}
void shoot()            {SUART.print(4); server.send(200, "text/html", " "); Serial.println(4);}
void left_shoot()       {SUART.print(5); server.send(200, "text/html", " "); Serial.println(5);}
void right_shoot()      {SUART.print(6); server.send(200, "text/html", " "); Serial.println(6);}
void left_right_shoot() {SUART.print(7); server.send(200, "text/html", " "); Serial.println(7);}

void handle_NotFound(){
  server.send(404, "text/plain", "Error: not found");
}
```

code for arduino (note: seems that stackexcange identifies this function as spam so i posted them [here](https://pastebin.com/0sZ0KbCG) ):
```c++
#include<SoftwareSerial.h>
#include <TVout.h>
#include <fontALL.h>
SoftwareSerial SUART(2, 3); //SRX=Dpin-2; STX-DPin-3
TVout TV;
//#define debug 1
#define numberOfEnemies 15
#define maxEnemyMovement 20
#define playerDefaultY 75

int points = 0;
unsigned long int millisLastEnemyMovement = 0;
int enemyMovementDelay = 850;
int enemyMovement = 0;
bool directionIsRight = true;

struct b {
  int x;
  int y;
  bool shootable;
} bullet;

struct p {
  int x;
} player;

typedef struct {
  int x;
  int y;
  bool alive;
} Enemy;

Enemy enemyList[numberOfEnemies];

void renderEnemy(int x, int y){ //draws sprite of enemy

}

void renderPlayer(int x, int y){  //draws player sprite

}

void shoot(){
  if(bullet.shootable){
    bullet.shootable = false;
    bullet.x = player.x;
    bullet.y = 100;
  }
}

void left(){
  if(player.x > 0){
    player.x--;
  }
}

void right(){
  if(player.x < 105){
    player.x++;
  }
}

void spriteUpdate(){
  if(!bullet.shootable){
    bullet.x--;
    if(bullet.x < 0){
      bullet.x = 0;
      bullet.y = 0;
      bullet.shootable = true;
    }
    for(int i = 0; i < numberOfEnemies; i++){
      if(bullet.x > enemyList[i].x && bullet.x < enemyList[i].x + 11 && bullet.y > enemyList[i].y && bullet.y < enemyList[i].y + 8){
        bullet.shootable = true;
        enemyList[i].alive = false;
        points++;
      }
    }
  }
  if(millis() - millisLastEnemyMovement > enemyMovementDelay){
    millisLastEnemyMovement = millis();
    if(directionIsRight){
      enemyMovement++;
      for(int i = 0; i < numberOfEnemies; i++){
        enemyList[i].x++;
      }
    }else{
      enemyMovement--;
      for(int i = 0; i < numberOfEnemies; i++){
        enemyList[i].x--;
      }
    }
    if(enemyMovement == 0 || enemyMovement == maxEnemyMovement){
      directionIsRight = !directionIsRight;
    }
  }
}

void render(){
  TV.delay_frame(1);
  TV.clear_screen();
  renderPlayer(player.x, playerDefaultY);
  for(int i = 0; i < numberOfEnemies; i++){
    if(enemyList[i].alive){
      renderEnemy(enemyList[i].x, enemyList[i].y);
    }
  }

}
void setup()
{
  char x;
  Serial.begin(115200); //enable Serial Monitor
  SUART.begin(115200); //enable SUART Port
  bullet.x = 0;
  bullet.y = 0;
  bullet.shootable = true;
  for(int i = 0; i < 5; i++){
    enemyList[i] = {4+i*20, 3, true};
    enemyList[i+5] = {4+i*20, 18, true};
    enemyList[i+10] = {4+i*20, 33, true};
  }
  player.x = 50;
  TV.begin(PAL, 120, 96);
  while(true){

  }
}

void loop(){
    if (SUART.available() != 0){
      x = SUART.read();  //read char
      switch(x){
        case 'a':
          left();
          break;
        case 'b':
          right();
          break;
        case 'c':
          //left and right same time?
          break;
        case 'd':
          shoot();
          break;
        case 'e':
          left();
          shoot();
          break;
        case 'f':
          right();
          shoot();
          break;
        case 'g':
          //left and right at same time?
          shoot();
          break;
      }
    }
    spriteUpdate();
    render();
}
```
This is the schematic:
[![this is the schematic][1]][1]

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

body: Software Serial is a pretty crummy library.  It disables interrupts while receiving for long periods of time.  That causes any other library that uses interrupts to have issues with timing.  There are a plethora of other soft serial libraries out there.  Try one of those instead.  AltSoftSerial comes to mind.


body:
> It works good but every time the nodemcu sends something, the TV signal gets distorted.

That's correct. SoftwareSerial is terrible. Don't use it. When it's sending or receiving data nothing else at all can happen.

If you have to resort to SoftwareSerial then you are using the wrong hardware for your project. Maybe an ESP32 would be a better choice.

----

Basically you have two software hacks. Both require precise timing and neither is happy if that timing is messed with. To that end both will disable interrupts to stop things interfering. You can use one or the other, but not both.


message:
Sound sensor's output stucks to high values when I include a delay in the loop function

I use Waveshare's Sound Sensor module.

I use this simple code to test the module:

[![enter image description here][1]][1]

[![enter image description here][2]][2]

As you can see, the problem is that when I have a delay in the program, once the analogRead is bigger than 10 (by slightly knocking the mic with my finger), "sound" is printed, but sound sensor's output doesn't decrease back to normal values and "sound" keeps repeating.
If I don't use delay, everything works fine, but I need that delay.

*NOTE: on this test I use a STM32 nucleo development board, but I don't think that contributes to the problem.

  [1]: https://i.stack.imgur.com/schvL.png
  [2]: https://i.stack.imgur.com/IEFVr.png

body:
The AOUT pin of that device is no more than an amplified audio signal. It is AC coupled, and that means that it is a small signal varying around 0v. It is not suitable for feeding directly into an ADC.

The AC coupling capacitor will be being charged up by the DC offset it is there to remove, and the lack of any drain on that capacitor while the ADC is not sampling means it never discharges as it should. By sampling faster you increase the load and thus it can discharge somewhat.

To properly read that AOUT pin you need to add your own DC offset to half the VCC voltage of your Arduino board, which can be done by adding two 10k resistors. One from VCC (5v) to the analogue input, and the other from the analogue input to ground.

The signal you read will then be "idle" at around 2.5v (512 ADC reading) and vary around that as sound is picked up.

Note that you can't just sample once per second and hope: there is no provision for "did sound happen in the past second". Instead you really need to be sampling all the time.

If all you care about is if some sound above a threshold has happened then you would be better using DOUT connected to a digital input or even an interrupt pin, and using the threshold setting trim pot on the board.


message:
facing issue with while loop

I'm trying to develop one code in which I'm facing issue with while loop. Once I enter any newString it enter in while loop it count till 10 times, when (n>10)  then it is coming out from the while loop.
Facing issues in this part
```
while (n < 10)
  {
    while (!ret) {
      if ((time + 15) < millis()) {
        ret = dmd.stepMarquee(-1,0);
        time = millis();
      }
    }
     ret = false;
     n++;
    }
```

I just want when I enter any newString when loop is running it break while loop and start display newString.

Please help me out with this. Thanks for your help, greatly appreciated.

Here is my full code:
```
#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"

//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 2
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

String inputString = "";
boolean stringComplete = false;
String commandString = "";

void ScanDMD()
{
  dmd.scanDisplayBySPI();
}

void setup()
{
   Serial.begin(9600);
   Serial.println("Arduino Start");
   Timer1.initialize( 5000 );
   Timer1.attachInterrupt( ScanDMD );
   dmd.clearScreen( true );
}

void loop() {

if(stringComplete)
{
  stringComplete = false;
  getCommand();

  if(commandString.equals("STAR"))
  {
   dmd.clearScreen( true );
  }

  else if(commandString.equals("PRSC"))
  {
    String text = getTextToPrint();
    printText(text);
  }
  inputString = "";
}

}

void getCommand()
{
  if(inputString.length()>0)
  {
     commandString = inputString.substring(1,5);
  }
}

String getTextToPrint()
{
  String value = inputString.substring(5,(inputString.length())-2);
  return value;
}

void printText(String text)
{
  char newString[256];
  int sLength = text.length();
  text.toCharArray( newString, sLength+1 );
  dmd.selectFont(System5x7);
  dmd.drawMarquee( newString , sLength , (32*DISPLAYS_ACROSS )-1 ,1);
  unsigned long time;
  boolean ret = false;
  time = millis();
  int n;
  n= 0;
  while (n < 10)
  {
    while (!ret) {
      if ((time + 15) < millis()) {
        ret = dmd.stepMarquee(-1,0);
        time = millis();
      }
    }
     ret = false;
     n++;
    }
}

void serialEvent() {
  while (Serial.available()) {            // get the new byte:
    char inChar = (char)Serial.read();    // add it to the inputString:
    inputString += inChar;                // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
```

body:
Forget using `serialEvent()`. It is one of the more stupid things that Arduino implemented.  It is nothing more than a function that gets called (wrapped in a check to `Serial.available()`) once per iteration of the main `loop()` function.

It is not really an "event" function at all.

To have anything happen inside your `while` loop you have to *manually* implement that thing within your `while` loop.  Nothing outside of the `while` loop (with the exception of interrupts) can affect the operation of the `while` loop.


body:
**Now my code is working like** I'm sending the data to Arduino UNO via serial(Keyboard) when it receive any serial data it scroll till 10 time and when _(n>10)_ then will loop stop. In between this whenever I'm send any new data it is not accepting.

 **what exactly I want** whenever I send new data to Arduino UNO via serial(Keyboard) and then UNO is to turn expected to scroll the received text on P10 DMD display at the same time.  Whenever it receive new serial data it start displaying that data into display.


message:
Receiving two values over serial port using servos

So I have a java program that picks up the mouses x and y coords on the display and I would like to send those to my arduino and then move my two servos to those locations (I have two servos ontop of each other, one for x and one for y).

The java (processing) code i have is below

```
void draw(){  //same as loop in arduino
  background(52, 152, 219); // background color of window (r, g, b) or (0 to 255)
  //lets give title to our window
  fill(0, 255, 0);               //text color (r, g, b)
  textFont(font);
  text("Ripple Effect Maker", 80, 30);  // ("text", x coordinate, y coordinat)
  ellipse(mouseX, mouseY, 33, 33);
  int x = mouseX;
  int y = mouseY;
  port.write(x+":"+y);
  println(x+":"+y);
  //println("y="+y);
  delay(100);
}
```
I don't have much arduino code because I can't seem to find a way to split the data into two seperate variables and then move the servos to their corresponding variables.

Thanks for the help!

body:
Your problem is that you aren't providing anything to split the data on. You just send the X and y coordinates with a : between X and y. There's nothing between the y and the next X.

By changing the line

    port.write(x+":"+y);

To

    port.write(x+":"+y+"\n");

You then get the linefeed character to split your data on.


message:
What image formats are accepted for displays?

Sorry if there is information missing, im new to the topic.

I have an ESP32 connected to a Waveshare 7,5" RWB display, and i can display images by converting them to .xbm and including the hex code in the sketch.
So far, so good.

But heres my question: What exactly are the requirements? Because when looking at the .xbm image code, i see that its a pattern like this:

`0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00`

But when i used a python script

```
import binascii
filename = 'test.png'
with open(filename, 'rb') as f:
    content = f.read()
print(binascii.hexlify(content))
```
to convert an image to hex code, i got this:
`b'89504e470d0a1a0a0000000d4948445200000280000001800802000000b2...'`(continues for thousands of characters, didnt want to paste all of that here

However, when trying to run the sketch that successfully displayed the first hex code with the second image code, i got this error(cut out the big string clutter again):

```
...0000000049454e44ae426082};

^

C:\Users\Administrator\Desktop\SPI_test_example\SPI_test_example.ino: In function 'void loop()':

SPI_test_example:167:21: error: 'showBitmapExample' was not declared in this scope

   showBitmapExample();

                     ^

SPI_test_example:171:51: error: 'showFont' was not declared in this scope

   showFont("FreeMonoBold9pt7b", &FreeMonoBold9pt7b);

                                                   ^

exit status 1
'showBitmapExample' was not declared in this scope
```

So, i would like to know - does the image code have to have exactly the format i mentioned first? This is very important for me because i want to automate the process from inputting any image to using the resulting code to feed the display.

Thanks in advance!

EDIT: As far as my understanding goes, both formats are the same data, just that the first one is displaying a white pixel as 0xFF,0xFF,0xFF and the second one as FFFFFF.

body:
This is not a question of image format but of what format c accepts data in.

What you have is a python representation of binary data in hexadecimal encoding. That can only be used with python.

C represents hexadecimal values by prefixing them with `0x`.

You will have to change your python code to output each byte individually and prefix it with `0x` and separate each one with a comma. That can then be wrapped in an array structure.


message:
Measuring current directly with Arduino

Is it safe to measure current like this as long as measured voltage drop don't exceed 5V? I'm asking this because in almost all current sense circuits they used an opamp in their design.

Do I need a resistor between shunt resistor and ADC? Is there any noticeable current flowing to Arduino ADC that can damage it?

<!-- Begin schematic: In order to preserve an editable schematic, please
     don't edit this section directly.
     Click the "edit" link below the image in the preview instead. -->

![schematic](https://i.stack.imgur.com/BPV4f.png)

<!-- End schematic -->

body:
Let's imagine that you expect to have 1A of current going through your circuit.
Resistor of 0.1 Ohm will give you a voltage drop of 0.1V.
This causes two problems.

Firstly Arduino has a cheap ADC builtin. It's supposed to be easy to use, but not really precise. It has nonlinearities towards lower and higher range. You will get more precise reading when measuring voltages around 2.5V.

Secondly The precision of your measurement is cut by factor of 50. Arduino contains ADC that has resolution of 10 bits. This means that there are only 20 steps between 0V and 0.1V That's not really precise.

Solution? Use an opamp as "[differential amplifier][1]". If you multiply the voltage difference by 25 you will get 2.5V on the amplifier output when the resistor has 0.1V of drop on it. This value is much more precise when reading it by analogRead of the Arduino, and even a tiny change in voltage on the resistor will give you 25 times larger change on the amplifier output. This way it's much more precise and accurate.

  [1]: https://www.electronics-tutorials.ws/opamp/opamp_5.html


body:
There's a bad current flowing into your Arduino, if ADC voltage exceeds Vcc (5V). This undesired current can be limited by a resistor. But if there's such a high voltage across a shunt resistor, something else is wrong. You rather suffer from a too small voltage for useful measurement.

My first doubt is rather, whether it's safe for the shunt resistor.


message:
Serial communication problem with sim7600 with Arduino Mega

I am just sending the AT command to test if the serial communication works, the response I am getting is a flipped "?" or a square or rectangle symbol.

I have tested the communication using software provided by Waveshare connected through on board USB to UART.

https://www.waveshare.com/wiki/SIM7600E-H_4G_HAT

    int PowerKey = 2;
    char answer;

    void setup()
    {
     Serial3.begin(96u00);
     Serial.begin(9600);
     pinMode(PowerKey, OUTPUT);
     Serial.println("initializing");
     digitalWrite(PowerKey, HIGH);
     delay(1000);
     digitalWrite(PowerKey, LOW);
    }

    void loop()
    {
      answer = send_at_cmd("AT\r\n");
      Serial.println(answer);
    }

    uint8_t send_at_cmd(const char* ATcommand)
    {
      char response;
      // send test command AT
      Serial3.print(ATcommand);
      Serial.println(ATcommand);
      delay(1000);
      //Check for response from the sim7600E-H
      while(Serial3.available() != 0)
      {
        response = Serial3.read();
      }
      return response;
    }

[![enter image description here][1]][1]

[![enter image description here][2]][2]

  [1]: https://i.stack.imgur.com/sI4wE.png
  [2]: https://i.stack.imgur.com/jWzjC.png

body:
You have change baud rate by at+iprex=9600 before upload your code, because sim7600 set 115200 by default then save your setting by at&w.

hope this help someone.


message:
How to convert char[12] to byte[6]

I am trying to convert a char[12] array that contains a Mac Address into a byte[6] in order to use within the Ethernet.begin method. I have tried various methods from online, but had little success to date. The char array currently contains "A4BDC334688C" and i would like a byte array containing { A4, BD, C3, etc }.

The following is extremely close to working, with the output of the following
"File Opened.
A4BDC334688C
A4
9D
C3
34
68
6C" With BD incorecctly converting to 9D, and 8C becomming 68.

    #include <SPI.h>
    #include <SD.h>
    #include <ArduinoJson.h>

    byte ByteMacAddress[6]; // RESERVED MAC ADDRESS
    StaticJsonDocument<200> doc;

    int ShotCount = 0;
    byte buf[40];

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

      //Initialise SD Card for Config
      if (!SD.begin(4)) {
        Serial.println("initialization failed!");
      }

      // open the file for reading:
      File ConfigFile = SD.open("Config.txt");
      if (ConfigFile) {
        Serial.println("File Opened.");
      } else {
        Serial.println("error opening Config.txt");
      }

      //Deserialize Json Config in File
      delay(5);
      DeserializationError error = deserializeJson(doc, ConfigFile);
      ConfigFile.close();
      // Test if parsing succeeds.
      if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.c_str());
        return;
      }

      char* SDMacAddress = doc["MacAddress"];
      Serial.println(SDMacAddress);

      char arr[12];
      strcpy(arr, SDMacAddress);

      auto getNum = [](char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
      byte *ptr = ByteMacAddress;

      for(char *idx = arr ; *idx ; ++idx, ++ptr ){
        *ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
      }

      //Check converted byte values.
      for( byte b : ByteMacAddress )
        Serial.println( b, HEX );
    }

    void loop(){
      //Maintain DHCP Lease
      //Ethernet.maintain();

      //GetShotCount();
      delay(500);
    }

body:
Please see the comments in the code:

    // SDMacAdress is zero terminated
    // (it should be because you use strcp not strncp)
    // char arr[12]; you need one extra byte for the zero
    char arr[13];
    strcpy(arr, SDMacAddress);

    auto getNum = [](char c)
    {
      // you forgot upercase HEX letters (A-F)
      // don't use sophisticated code
      // the best programmers produce understandable code IMHO at least ;-)
      // perhaps you copied it from the internet
      // return c > '9' ? c - 'a' + 10 : c - '0';

      if ( c >= '0' && c <= '9' ) return c - '0';
      if ( c >= 'a' && c <= 'f' ) return 10 + c - 'a';
      if ( c >= 'A' && c <= 'F' ) return 10 + c - 'A';

      return 0;
    };

    byte *ptr = ByteMacAddress;

    for(char *idx = arr ; *idx ; ++idx, ++ptr )
    {
      *ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
    }


body:
It is not clear why you are trying to implement it from scratch. If your input is always properly formatted you can perform the conversion in one call to `sscanf`

    char arr[] = "A4BDC334688C";
    byte ByteMacAddress[6];

    sscanf(arr, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
      &ByteMacAddress[0],
      &ByteMacAddress[1],
      &ByteMacAddress[2],
      &ByteMacAddress[3],
      &ByteMacAddress[4],
      &ByteMacAddress[5]);

Of course, you can also implement it [more elegantly] through a cycle, i.e. `sscanf` it one byte at a time.


message:
How to control multiple leds independently with arduino but they share a 12v external power source

I have a project with many components.  I'd like to use 12v adapter from wall.
I have a simplified experiment to apply to the actual project.  I can't find any info on the internet after days of searching.
I want to control each of 6 leds independently with an arduino. They all have different forward voltages.  But I would like them to be powered by the same 12v external power supply.  I suspect I would use transistors, Could someone point me in the right direction to wire these up?
I can figure out the code myself.

body:
The basic schematic for one LED is:

<!-- Begin schematic: In order to preserve an editable schematic, please
     don't edit this section directly.
     Click the "edit" link below the image in the preview instead. -->

![schematic](https://i.stack.imgur.com/gvfg7.png)

<!-- End schematic -->

Make sure that:

* R1 is the right resistance for the supply voltage, forward voltage, and desired current
* M1 has a threshold low enough that the Arduino turns it fully on.

Just duplicate the circuit for each LED you want to control. The code is no different to having an LED directly attached to the Arduino.


message:
AD7476 adc and SPI

I'm trying to understand if I can use the AD7476 with Arduino and SPI:

[datasheet][1]

My rough idea is that I should put CS low, then read two bytes using SPI.transfer() twice, and then bring CS high again. Is that correct?

But looks like the AD7476 doesn't have any buffer, so I guess timing must be really precise in order to not miss any bit?
what if SPI.transfer() occurs too late after CS going low?

[![enter image description here][2]][2]

  [1]: https://www.analog.com/media/en/technical-documentation/data-sheets/AD7476_7477_7478.pdf
  [2]: https://i.stack.imgur.com/zTPP6.png

body:
> My rough idea is that I should put CS low, then read two bytes using SPI.transfer() twice, and then bring CS high again. Is that correct?

Yes, that is correct.

>But looks like the AD7476 doesn't have any buffer, so I guess timing must be really precise in order to not miss any bit?

No. The conversion is controlled *by the SPI clock*.  The ADC is a *successive approximation* ADC.  That means for each "sample clock" a more accurate approximation of the voltage is created. In this instance that "sample clock" is the same as the "SPI clock".  Each time you send one clock pulse a new, more accurate, approximation is created.

> what if SPI.transfer() occurs too late after CS going low?

Within reason, nothing untoward.

The way an SAR ADC works is it uses a DAC which is controlled by a clock to create a voltage. That voltage is halfway between 0V and "the current estimated voltage" (starting at "vref" - 5V in this case).  The voltage is compared with the "sampled" voltage (stored in a tiny capacitor). That forms one bit of the resultant value. At the next clock the DAC voltage is halved (and maybe added to the previous voltage depending on the previous comparison result) and repeated. This is the next bit of the result. It repeats with the voltage getting smaller and smaller and smaller.

Each time you clock one bit through your SPI interface it generates and compares the next voltage generating the next bit of data for you to read.

The knock-on effect of this is that there is both a maximum and a minimum frequency that you can use the SPI interface at.  Too fast and it is unable to generate the comparison voltages and do the comparison in time. Too slow and the internal capacitor loses charge through self-discharge and leakage current.  Thgis is why on page 8 of the datasheet it specifies a minimum and a maximum frequency for the SPI interface (10kHz to 20MHz).


message:
Control DC Motor with Arduino Uno WiFi Rev2 Html Slider Code

I would like to control my dc motor using the Arduino Uno WiFi Rev2 using an html range slider. Now I can control the motors speed and direction using push buttons just fine. My code is posted below of what I've done so far.

    client.println("");
    client.println("<input type=\"range\" class=\"class-name\" orient=\"vertical\" id=\"range\" min=\"0\" max=\"255\" step=\"0\" onchange=\"motor1(this.value)\" value=\"0\">");

This code below is the current button code. I need the below code to be used by the slider code above:

    if (currentLine.endsWith("GET /MONFWD")) {
    digitalWrite(MOTOR_A_ONE, HIGH);  // Establishes forward direction of Channel A
    digitalWrite(MOTOR_A_TWO, HIGH);  // ENGAGE BRAKE
    delay(1000);                      // DELAY BEFORE RELEASING BRAKE
    digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
    analogWrite(MOTOR_A_ENGAGE, 255); // Spins the motor forward on Channel A at full speed

body:
**Here's an example of slider that changes a Servo speed,**
**In html page code you have to add this:**

      <input type="range" min="0" max="1023" value="512" class="slider" id="myRange">

Value:

**In arduino you have to define handler like this:**

    void handle_pwm() {
     String  pwm_val = server.arg("PWMval"); // reading from slider on html pagae

     Serial.print("slider val: ");
     Serial.println(pwm_val);
    int pwm_value = map(pwm_val.toInt(), 0, 1023, 0, 180);
    Servo.write(pwm_value);

    server.send(200,"text/plane","0"); // handling the webpage update

    }

**And define servo and handler in Setup() section:**

    void setup(void){
    ....
    Servo.attach(MotorPin,1000,2000);
    server.on("/setPWM", handle_pwm);
    ....
     }


message:
It is possible to revive my Arduino Leonardo (mini beetle) board?

I have this board:

[![Arduino Leonardo Beetle mini board][1]][1]

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

It has the `atmega32u4` chip.

I have updated its code several times, and used it without issues for several months. Today, while updating (using Arduino IDE) it started to throw some errors. At the end it said that the checksum didn't match, so I repeated again few more times with the same behavior until it reported "Uploaded" without errors. I used for few minutes and it was working as expected. Then, I had to do another update and the same happened (checksum didn't match). However this time I got to the point in which it was no longer detected by the OS (Ubuntu), and no LED is turning on.

Whenever I connect it to the USB port, I can only see in the logs:

`usb 1-9.4: new full-speed USB device number 9 using xhci_hcd`

However `/dev/ttyACM0` is not longer created.

I'm trying to use `avrdude` to reset it, but it is reporting:

`avrdude: ser_open(): can't open device "/dev/ttyACM0": No such file or directory`

which make sense as `/dev/ttyACM0` doesn't exists. So, what are my options? Is there anything I can do to make it work again?

**UPDATE**

I didn't realize that this board has a reset marked as "RES". Following `Juraj` recommendation I bridged `RES` and `GND`. The LED marked as "SDA" turns ON after I release the contact and I can read in the logs:

```
[1212051.679205] usb 1-9.4: USB disconnect, device number 22
[1212058.532030] usb 1-9.4: new full-speed USB device number 23 using xhci_hcd
[1212058.621972] usb 1-9.4: New USB device found, idVendor=2341, idProduct=0036
[1212058.621982] usb 1-9.4: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[1212058.621988] usb 1-9.4: Product: Arduino Leonardo
[1212058.621994] usb 1-9.4: Manufacturer: Arduino LLC
[1212058.622933] cdc_acm 1-9.4:1.0: ttyACM0: USB ACM device

```

Which I think its good news. However, it seems ttyACM0 becomes available for few seconds. I was able to upload the Blink example into the chip and I can confirm its working because its blinking. Still the ttyACM0 disappears after few seconds.

Output of `avrdude -c avr109 -p atmega32u4 -P /dev/ttyACM0` :

```
Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: safemode: Fuses OK (E:FB, H:D8, L:FF)
avrdude done.  Thank you.
```

body:
Probably you have overwritten your Bootloader.

Connect MOSI, MISO, SCK and RESET to an ISP-Programmer and flash the file
Caterina-LilyPadUSB.hex
to it.

There are lots of tutorials on the net how to use a spare arduino as an ISP-Programmer.


message:
Stepper Motor Direction via Flow Amps

I have a problem with stepper motor direction.

I want to change the direction of the stepper motor rotation, when the stepper motor shaft is stuck.

For example, when I hold the shaft of the stepper motor with my hand, the direction should change.

How can I do it?
Someone told me you should calculate the current, but I don't know how to do this?

My material components:

 - Arduino
 - stepper motor gearbox 12 V
 - ULN 2003A

body:
When motors stuck their current draw increases, you can sense that increase in the current and then change the rotation of the motor. The simplest way to measure current is with using a shunt resistor, for sensing high currents, current sensors based on hall effect sensors is usually used. [ACS712][1] is one example.

How to sense current using a shunt?

Basically we're measuring the voltage drop over a resistor (RS), but the voltage drop is too small for Arduino to measure so we need an opamp to "amplify" the input:

![schematic](https://i.stack.imgur.com/BiIi4.png)

The schematic above is the simplest way to measure the voltage drop at "LOW SIDE", there is differential mode and "HIGH SIDE" measurement methods.

The gain of opamp can be set with resistors and this formula:

VOUT = (1 + R2/R1) * V1

The output of opamp then can be calculated to get current using ohm's law:

I = V / R

A simple code to calculate current:

    int rawAnalog;
    float voltageDrop;
    int resistor = 0.1;
    float current;

    void setup() {
      pinMode(A0, OUTPUT);
    }

    void loop() {
      rawAnalog = analogRead(A0); // Read A0 input
      voltageDrop = (rawAnalog * 5) / 1024; // Convert the raw value to voltage
      current = voltageDrop / resistor; // calculate the current
    }

Bonus: [INA219][2] is an easy to use high-side I2C module which can measure both current and voltage.

  [1]: https://www.sparkfun.com/datasheets/BreakoutBoards/0712.pdf
  [2]: https://www.adafruit.com/product/904


message:
Is it possible to synchronize two SPI busses to send data out in parallel on the same clock signal?

We're working with these very odd driver chips which are sort of like shift registers except rather than latch the data at the end, the latch needs to happen over the last 'n' clock cycles in parallel with the data being shifted out.  Depending on the value of 'n', the data is 'latched' into different registers. Like I said, it's an odd protocol.

Here's an example sending six bytes of data (top row) synced to the latch (bottom row).  The latch is HIGH for the last two bits of the data as it's being shifted out.

    Time-->
    11001100 01010101 10011010 11001011 01001010 10010101
    00000000 00000000 00000000 00000000 00000000 00000011

In this example, the latch is synced to the fifth and sixth bit of that last frame (as opposed to the 7th and 8th in the first example) which tells the chip to load this data into a different register.

    Time-->
    11001100 01010101 10011010 11001011 01001010 10010101
    00000000 00000000 00000000 00000000 00000000 00001100

Like I said, it's an odd design, but it basically lets you shoot out data non-stop without having to pause to latch.

Because of having to 'sync' the latch with certain clock cycles in the data, we can't use the standard SPI bus and have to use normal GPIO pins, which of course are substantially slower.

BUT... if I could set up the data for a second SPI bus, then use a buffer to define where the latch is high/low accordingly, that would work.

The issue is I don't know how to, or if it's even possible to sync SPI busses together.

Only thing I can think of is to use one SPI bus as the master and another SPI bus (on the same processor) as a slave, then try to leverage the bi-directional nature of it.  But this is just a guess.

I was considering looking into DMA too, but I'm starting with SPI since I know my chip has four SPI busses on it. (Update: I'm starting to think DMA is the way to go here since I now know it is possible to sync two DMA controllers to the same clock.)

body:
As [the Wikipedia page on SPI][1] shows (as introductory material), the SPI **main** device generates the clock. The SPI **sub** device waits for the leading edge on its `CS` (chip select) to start transmitting its data on the following clock edges.

So you can set up one of the senders as main device, and the other sender as sub device. Use MOSI of main as the first data output, and MISO of sub as the second data output, like this:

<!-- Begin schematic: In order to preserve an editable schematic, please
     don't edit this section directly.
     Click the "edit" link below the image in the preview instead. -->

![schematic](http://i.stack.imgur.com/dioSI.png)

<!-- End schematic -->

You can add more SPI sub devices, if you need more synchronized outputs.

  [1]: https://en.wikipedia.org/wiki/Serial_Peripheral_Interface


message:
Could i use a stepper motor?

i want to make a 3-axis gimbal with arduino but brush-less motors seem incredibly hard so would stepper motors work for this purpose?

if their is a way if not, is how can i connect 3 gimbal motors to one arduino board and control them seperately? (links if possible)

body:
A stepper motor might work, but it is certainly not the best choice for what you try.

Instead you should use brushless motors. There are not so hard to use, as you think. Driving them directly is difficult, but no one does that. Instead just buy an ESC (Electronic Speed Control) (one for each brushless motor). That is an extra chip/board, where you need to connect a PWM signal and ground from the Arduino and the power source of the motor. Then connect the motor to the ESC and you will be able to control the brushless motor with a simple PWM signal.

For further information you can read [this post][1] in the Arduino forum, or you just google. There is plenty of information the web about ESCs.

  [1]: https://forum.arduino.cc/index.php?topic=270309.0


body:
DC Motors with a feedback line (so called PID Controller; https://en.wikipedia.org/wiki/PID_controller) would be the best way to do it. You need the motor as an actor and some sensors that sense the direction of the axis's of the gimbal. The PID controller should be very fast for smooth operation. (Perhaps an ESP32 is a better MCU for this purpose).

The PID controller uses a motion sensors.

It's good to have strong motors that do not react "nervous" on on voltage changes. You probably will have to use motors with gears. Even fast PID controller won't be able to keep your camera from turning cartwheels if a 10.000 cycles per minute racing motor gets full voltage applied.

I searched the internet for videos of such a setup but the only thing I found, are the commercial (?) gimbal "alexmos" "how to" videos. I'm with you that it is hard to build such a gimbal without much knowledge of how PIDs work. I'm also not sure if there is an arduino that's fast enough to realize a PID that gets its feedback from motion sensors and drives dc motors with this information, then get's feedbach of the motor movements (from the motion sensors) and so on.

And probably a better/stronger servo and and very fast PID setup might eventually smooth the gimbals movements.

Here is a link that might help you as a start point for PID (if you are not discouraged yet):
https://www.youtube.com/watch?v=9TTDmWiMg_Y

And even professional setups can make problems: (It's a matter of the PID parameters)
https://www.youtube.com/watch?v=UeeTXH2Qv2o


message:
DS1820B only returns -127

ive used one DS1820B sensor in a AVR bread board circuit and it works there, now i tried attaching it to a arduino mega board with the code below and also another library below that.

currently the data pin is connected to pin 10 on the arduino board, but i also tried pin 2 as in this picture:

https://lastminuteengineers.com/wp-content/uploads/2018/12/Wiring-Multiple-DS18B20-Temperature-Sensors-With-Arduino.png

i changed: #define ONE_WIRE_BUS 10   accordingly.

So after that i put the chip back onto the bread board and it still works.
I also tried it with the 4.7k resistor on the data line as pullup....still -127.

Any ideas?

    #include <SPI.h>
    #include <SD.h>
    /********************************************************************/
    // First we include the libraries
    #include <OneWire.h>
    #include <DallasTemperature.h>
    /********************************************************************/
    // Data wire is plugged into pin 2 on the Arduino
    #define ONE_WIRE_BUS 10
    /********************************************************************/
    // Setup a oneWire instance to communicate with any OneWire devices
    // (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);
    /********************************************************************/
    // Pass our oneWire reference to Dallas Temperature.
    DallasTemperature sensors(&oneWire);
    /********************************************************************/

    void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
     sensors.begin();
    }

    void loop() {

      Serial.print("Temperature is: ");
     Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
       // You can have more than one DS18B20 on the same bus.
       // 0 refers to the first IC on the wire
       delay(1000);

    }

second code:

    #include "DS18B20Events.h"

    // Pin for DS18B20
    #define ONE_WIRE_BUS 10

    /********************************************************************/
    // Setup a oneWire instance to communicate with any OneWire devices
    // (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);

    // Init DallasTemperature
    DallasTemperature dallasTemperature(&oneWire);

    //Init thermometer with index 0 (default)
    DS18B20Events thermometer(0);

    // the setup function runs once when you press reset or power the board
    void setup() {
      Serial.begin(9600);
      DS18B20Events::setup(&dallasTemperature); // Link DS18B20Events to DallasTemperature object
      DS18B20Events::setInterval(5000); // Set min interval in milliseconds between 2 measurements. Default 10000 set in setup() method.

      // subscribe to temperature changed event
      thermometer.onChange = temperatureChanged;
    }

    // the loop function runs over and over again until power down or reset
    void loop() {
      thermometer.loop(); // Mandatory to measure temperature and fire an event if needed

      Serial.print(thermometer.getTempC()); // Get last measured tempC if needed.
      Serial.print(" measured ");
      Serial.print((millis() - thermometer.getMillis()) / 1000);
      Serial.println(" seconds ago.");

      delay(1000);
    }

    void temperatureChanged(uint8_t index, float tempC)
    {
      Serial.print(tempC);
      Serial.print(" new tempC at index ");
      Serial.println(index);
    }

body:
127 error is a **connection error**, check the wiring:

[![][1]][1]

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


message:
Controlling multiple servos as one

I want to attach multiple servos to Arduino but all them must receive same command. If it possible to connect all servos to one command pin (I don't want to use "for" in code to minimize processor time for operation)?

body:
IMHO it is possible to connect more than one server PWM control inputs to a Arduino output pin.

The Servo input is something like a comparator with an very high resistance. So the servos should not influence each other in any way, nor will the output pin be overloaded.

Because of the input capacities of the servos, there might be a limit of the count of the servos.

And if you supply the servos with power from the arduino then, there is of cause a limit.

EDIT:

It is always a good idea to use buffers in a servo control circuit, to keep a sharp square wave signal. Parasitic capacities can reshape the signal even to a sinus signal.

That might confuse the servo control unit which works well only with a square wave (i.e a PWM-Signal).

<!-- Begin schematic: In order to preserve an editable schematic, please
     don't edit this section directly.
     Click the "edit" link below the image in the preview instead. -->

![schematic](https://i.stack.imgur.com/IVbtj.png)

<!-- End schematic -->


message:
Water pump control hysteresis

I'm using arduino with analog pressure sensor, temperature sensor and a relay to control water pump. The simple code I wrote works, but I want better control.
 ```
#include <OneWire.h>
#include <DallasTemperature.h>

  const int pinCidlaDS = 2;
  int pinA0 = A0;
  int teplota;
  int rele;
  float tlak = 0;  //ideal 290+-

OneWire oneWireDS(pinCidlaDS);
DallasTemperature senzoryDS(&oneWireDS);

void setup() {
  senzoryDS.begin();
  rele = 3;
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  tlak = analogRead(A0);
  teplota = (senzoryDS.getTempCByIndex(0));
   if ((tlak < 285) && (teplota < 30)) {
    digitalWrite(rele, LOW);
    delay(7000);
  }
    else {
    digitalWrite(rele, HIGH);
    delay(500);
  };
  Serial.print("Tlak: ");
  Serial.println(tlak);
  Serial.print("Teplota: ");
  senzoryDS.requestTemperatures();
  Serial.print(senzoryDS.getTempCByIndex(0));
  Serial.println(" stupnu Celsia");
  teplota = (senzoryDS.getTempCByIndex(0));
  delay (200);

}
```
So now I'm using delay to get away from the treshold value. And I wanted to write a code that would work like:

**Pressure is lower than 'x', relay ON.
Pressure rises to 'Y', relay OFF until Pressure is lower than 'x'.**

What should I use? Could you please give me a example, if there is?
Thank you very much! :)

body:
Implementing hysteresis turns out to be quite straight-forward:

```c++
float pressure = read_pressure();
if (pressure < x)
    turn_relay_on();
else if (pressure >= y)
    turn_relay_off();
```


message:
Multiple buttons to control relays

Dears,

I have a problem that I could not solve for a while, I tried the code below but it did not work.
There will be 4 buttons controlling 4 relays but in a way that button one will turn relays 1&3 on, if button 2 is pressed only relay 2 turns on, if button 3 is pressed relays 1,2 and 3 will turn on, if button 4 is pressed it is supposed to toggle relays 1&4. one more thing: I need to be able to pass from any button to any of the others and the corresponding relays will be activated while the others will go off.

        #include <Bounce2.h>

        const int     BUTTON_COUNT = 4;
        const int16_t DEBOUNCE_DELAY = 50;
        const uint8_t buttonPins[BUTTON_COUNT] = {28, 29, 30, 31};
        const uint8_t ledPins[BUTTON_COUNT] = {34, 35, 36, 37};

        Bounce buttons[BUTTON_COUNT];
        uint8_t ledStates[BUTTON_COUNT];

        void setup() {
        for (int i = 0; i < BUTTON_COUNT; i++) {
        buttons[i].attach(buttonPins[i], INPUT);
        buttons[i].interval(DEBOUNCE_DELAY);
        pinMode(ledPins[i], OUTPUT);
        ledStates[i] = HIGH;
        digitalWrite(ledPins[i], ledStates[i]);
         }
        }

        void loop() {

    // Update the debouncers.
    for (int i = 0; i < BUTTON_COUNT; i++)
        buttons[i].update();

    // When the first "laban" button is pressed:
    if (buttons[0].rose()) {

        // Light up the first LED.
        digitalWrite(ledPins[1], LOW);
        digitalWrite(ledPins[3], LOW);

        // and turn off all the others.
        for (int i = 0; i < BUTTON_COUNT; i++) {
            ledStates[0] = HIGH;
            ledStates[2] = HIGH;
            digitalWrite(ledPins[i], ledStates[i]);
        }
    }

    // When the first button is released:
    if (buttons[0].fell()) {

        // turn off relays.
        digitalWrite(ledPins[1], HIGH);
        digitalWrite(ledPins[3], HIGH);

    }

    // When the first "Soda" button is pressed:
    if (buttons[1].rose()) {

        // Light up the first LED.
        digitalWrite(ledPins[2], LOW);

        // and turn off all the others.
        for (int i = 0; i < BUTTON_COUNT; i++) {
            ledStates[0] = HIGH;
            ledStates[1] = HIGH;
            ledStates[3] = HIGH;
            digitalWrite(ledPins[i], ledStates[i]);
        }
    }

    // When the first button is released:
    if (buttons[1].fell()) {

        // turn off the first LED.
       digitalWrite(ledPins[2], HIGH);

    }

    // When the first "labanUP" button is pressed:
    if (buttons[2].rose()) {

        // Light up the first LED.
        digitalWrite(ledPins[1], LOW);
        digitalWrite(ledPins[2], LOW);
        digitalWrite(ledPins[3], LOW);

        // and turn off all the others.
        for (int i = 0; i < BUTTON_COUNT; i++) {
            ledStates[0] = HIGH;
            digitalWrite(ledPins[i], ledStates[i]);
        }
    }

    // When the first button is released:
    if (buttons[0].fell()) {

        // turn off the first LED.
         digitalWrite(ledPins[1], HIGH);
         digitalWrite(ledPins[2], HIGH);
         digitalWrite(ledPins[3], HIGH);
    }

    // When circulation button is pressed:
     if (buttons[3].rose()) {

        // Toggle its state.
        ledStates[3] = !ledStates[3];
        digitalWrite(ledPins[3], ledStates[3]);
        digitalWrite(ledPins[0], ledStates[0]);
         }
        }

body:
Below code is an simple example of what you described, each button will turn on the relays defined for them and turn off other relays. except for button 4 which will toggle relay 1 and 4.

    int relay1 = 2;
    int relay2 = 3;
    int relay3 = 4;
    int relay4 = 5;
    int sw1 = 6;
    int sw2 = 7;
    int sw3 = 8;
    int sw4 = 9;
    int sw1Status, sw2Status, sw3Status, sw4Status;
    int order = 1; // default is to set all relays off.

    void setup() {
      pinMode(relay1, OUTPUT);
      pinMode(relay2, OUTPUT);
      pinMode(relay3, OUTPUT);
      pinMode(relay4, OUTPUT);
      // input pull-up, so default state of buttons is HIGH.
      pinMode(sw1, INPUT_PULLUP);
      pinMode(sw2, INPUT_PULLUP);
      pinMode(sw3, INPUT_PULLUP);
      pinMode(sw4, INPUT_PULLUP);
    }

    void loop() {
      // read the buttons status
      sw1Status = digitalRead(sw1);
      sw2Status = digitalRead(sw2);
      sw3Status = digitalRead(sw3);
      sw4Status = digitalRead(sw4);

      if (sw1Status == LOW) { // if button 1 pressed
        order = 2; // go to case 2
      }
      if (sw2Status == LOW) {
        order = 3;
      }
      if (sw3Status == LOW) {
        order = 4;
      }
      if (sw4Status == LOW) { // button 4 toggle
        if (order != 5) { // if order is not equal 5 turn on relay 1 & 4
          order = 5;
        }
        else { // else go to case 1, which will turns off all relays
          order = 1;
        }
      }

      switch (order) {
        case 1: // if order equals 1
          digitalWrite(relay1, LOW);
          digitalWrite(relay2, LOW);
          digitalWrite(relay3, LOW);
          digitalWrite(relay4, LOW);
          break;

        case 2: // if order equals 2
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, LOW);
          digitalWrite(relay3, HIGH);
          digitalWrite(relay4, LOW);
          break;

        case 3: // if order equals 3
          digitalWrite(relay1, LOW);
          digitalWrite(relay2, HIGH);
          digitalWrite(relay3, LOW);
          digitalWrite(relay4, LOW);
          break;

        case 4: // if order equals 4
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, HIGH);
          digitalWrite(relay3, HIGH);
          digitalWrite(relay4, LOW);
          break;

        case 5: // if order equals 5
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, LOW);
          digitalWrite(relay3, LOW);
          digitalWrite(relay4, HIGH);
          break;

        default: // if none of above cases is valid go to case 1
          order = 1;
          break;
      }
    }

Having a tidy code format and using simple coding style (if possible) will help to debug/edit/improve the code more easily.

*Don't forget to de-bounce the buttons.*


message:
Matching baud rate on serial monitor

I am using different codes which set different values of baud rate through the Serial.begin() function. Do I need to change the baud rate of the serial monitor to match the initialized value on the code I am running every time, or can I just leave the serial monitor baud rate set at the highest frequency of all my sketches (115200)?

body:
If your sketch has

      ...
    void setup() {
      Serial.begin(115200);
      // ... other stuff here;
    }
      ...

And you run the IDE serial monitor at 9600, you will get gibberish. The same happens the other way round. You need to match the monitor to the baud rate you've defined on the `Serial.begin();` call.


body:
Sometimes yes, and sometimes no.

The majority of Arduino boards are connected to your computer through a USB connection (there are some rare outside cases that use other connections like bluetooth or RS485...).  That USB connection can fall into two broad categories:

1. A USB to UART adaptor that then connects to a UART on the Arduino's main MCU, and
2. A direct USB connection to the main MCU

Type 1 are such boards as the Uno, Mega and Nano. Basically any board that has both a main MCU and a secondary chip that lies between the USB connection and that MCU.

In this case the baud rate in the serial monitor is communicated to the USB interface chip and tells that chip "You are to communicate with the main MCU at this speed".  You also then have to have the main MCU communicate with the USB chip at that same speed. So it is *imperative* that those two speeds match.

In the second case this isn't true. USB itself has no real concept of "baud rate". Instead the baud rate you set is merely a piece of configuration information the device at the end of the USB connection can use for something if it sees fit (what that "something" is, is entirely up to the device).  Changing that baud rate has no real effect on the communication.  Boards like the Leonardo and the Mini fall into this category.  They all have a "U" chip, like the ATMega32U4, which directly talk USB.

So, in short:

* If you have a "Non-U" chip based board with a USB interface chip you *must* match the baud rates, and
* If you have a "U" chip based board (or some other make of board with a direct USB interface) then the whole concept of baud rate is meaningless1 and it really doesn't matter what the numbers are you choose to use where.

----

1. 1200 baud is a special case in Arduino USB boards as it is used to trigger the bootloader for uploading new code.


message:
Control 4 digital outputs with one switch

I want to control 4 digital outputs (d0, d1, d2, d3) with one switch. When the Arduino starts, they should all be in state LOW.

Now, after pressing the button once, d0 should get HIGH. Another push brings d0 to LOW and d1 to HIGH. So actually the 4 outputs shall behave like a bitmask. After every push the bitmask shall increment by one.

    d0 LOW
    d1 LOW
    d2 LOW
    d3 LOW

push

    d0 HIGH
    d1 LOW
    d2 LOW
    d3 LOW

push

    d0 LOW
    d1 HIGH
    d2 LOW
    d3 LOW

Is there an easy way to implement this, without typing every case in the code?

I am glad for any hints.
Cheers and thanks.

body:
As usual with programming, there are many solutions, for example:

1. You can use a bitmask, put 4 variables in an array with binary values 1000, 0100, 0010 and 0001 and when the switch is pressed, you store the value into each LED pin by comparing the value.
2. However, a more simpler solution is just use a counter from 0 to 4, if the value is 1, the first LED should be one, then after a switch increase the value, and use modulo 5 (% 5) to go back to 0.

To check if a button is clicked, use a boolean, which check against the previous value. Also implement the debounce algorithm which can be found at the Arduino site.

Without debouncing it could look like this:

    #include "stdlib.h"

    int buttonPin    =   7;
    int buttonState  = LOW;

    int led1Pin      =   8;
    int led2Pin      =   9;
    int led3Pin      =  10;
    int led4Pin      =  11;
    int ledMode      =   0; // All off, 1-4 means LED 1-4 is on

    void setup()
    {
    }

    void loop()
    {
       // Add debouncing yourself
       if ((buttonState == LOW) && (digitalRead(buttonPin) == HIGH))
       {
          buttonState = HIGH;
          ledMode = (ledMode + 1) % 5;
       }
       else if ((buttonState == HIGH) && (digitalRead(buttonPin) == LOW))
       {
          buttonState = LOW;
       }

       digitalWrite(led1Pin, ledMode == 1 ? HIGH : LOW);
       digitalWrite(led2Pin, ledMode == 2 ? HIGH : LOW);
       digitalWrite(led3Pin, ledMode == 3 ? HIGH : LOW);
       digitalWrite(led4Pin, ledMode == 4 ? HIGH : LOW);
    }

If you put the LEDs on subsequent pins, you can use the following code which is more simpler and works for any number of LEDs:

    #include "stdlib.h"

    int buttonPin    =   7;
    int buttonState  = LOW;

    int nrOfLeds     =   4;
    int startLed     =   8;
    int ledMode      =   0; // All off, 1 means led on pin startLed, 2 means led on pin startLed + 1 etc.

    void setup()
    {
    }

    void loop()
    {
       // Add debouncing yourself
       if ((buttonState == LOW) && (digitalRead(buttonPin) == HIGH))
       {
          buttonState = HIGH;
          ledMode = (ledMode + 1) % (nrOfLeds + 1);
       }
       else if ((buttonState == HIGH) && (digitalRead(buttonPin) == LOW))
       {
          buttonState = LOW;
       }

       for (int led = 0; led < nrOfLeds; ledPin++)
       {
           digitalWrite(startLed + led, ledMode == led + 1 ? HIGH : LOW);
       }
    }


message:
ignoring potentiometer value variations

I've got a 50k potentiometer connected to an Arduino Uno. The user should adjust the hue of an Adafruit NeoPixelStrip.

I've got the following code inside the loop function:

    int hue = analogRead(poti1);
    int mappedHue = map(hue, 0, 1023, 0, 65535);

    neoPixelStrip.fill(neoPixelStrip.gamma32(neoPixelStrip.ColorHSV(mappedHue, 255, 255)));
    neoPixelStrip.show();

If I turn the potentiometer knob, the colors are updating correctly and reflect the correct hue.
But if I let go of the knob, the LEDs are flickering a little (changing their hue values ever so slightly). If I print the potentiometer values to the console, they are changing a little. If I turn the potentiometer all the way to the left, the values range from 0 ... 13 or something. This change affects the hue of the pixels.

**How do I ignore those value variations? I want the user to constantly change the color, so I have to read the value constantly.**

**Any ideas?**

body:
There are many ways to remove noise, below are some ways:

- Only change the value when it differs enough. Assume the range is 0-1023. Than only change the value if the input value differs more than a constant value (like 5 or 10, experiment with this to find a good value for your pot meter).
- Another way is to use the average of the last x readings,  You can also combine this with the previous approach. There might be a small delay because of the averaging.

There are many more solutions, from reasonably easy to very complicated, search for removing noise in sensor data.


body:
You have two issues going on - noise pickup, and a potentiometer with imprecise stops at one or both ends.

The pot is the easiest to fix: What are the lowest low reading and highest high reading you can reliably get when you turn it to its stops? Use those in place of '0' and '1023' in your `map()` function.

Improving the noise is only slightly more involved. An exponential average is an easy filter to write and quite effective at quieting noisy data, as long the signal to noise ratio is fairly low. The basic idea is you add only a fraction of each new value and keep (1 - that fraction) of the old average; so if your chosen fraction is 0.1, you only consider 10% each new datum, keeping 90% of the current average.

`avg = f*avg + (1-f)*new_value`

The output of this averaging filter can be slow to respond to changes if your fraction is too small, but less effective at quieting noise if your fraction is too large.

I usually use .25, or 25% of each new point and 75% of the current average, when I'm just trying to clean up minor noise, which is what you probably have.

Here is my 25% averaging filter:

    int16_t xpavg(int16_t newdat, int16_t avg){

       // (3 * avg + 1 * newdat) / 4
       return( (((avg<<2) - avg + newdat) + 2) >> 2 );
    }

As you can see, I do the arithmetic in Fixed Point - just like integer arithmetic, but with an assumed binary point 2 places to the left during the computation. And I choose a fraction that represents "nicely" in binary - 1/4 in this case - to keep the arithmetic faster (since multiplication and division can be replaced by shifting).

The function just multiplies the current average by 4 and subtracts the old average (effectively multiplying by 3), adds the new value, and divides the sum by 4. (That extra "+2" rounds the fractional bits just before we shift them away). The end result is:<br> (3*avg + newData)/4 (== 3/4*avg + 1/4*newData).

If you were to only sample the pot every second or two, you could probably observe the delay as the filter falls behind while you turn the pot and catches up when you stop. If you're sampling at 10x/sec or less, you shouldn't be able to notice.

Update:

You need to add the function `xpavg()` to your code, and call it each time you read a new pot value (that is "newdat"), passing it the old average as well. It returns a new average pot value, now less affected by noise. You use that new average value instead of the raw number you just read from the pot, in your hue calculation. As Duncan offered, you can - and I do - initialize the average with a raw value from the pot. You could also start it at zero but it will take a few samples to converge on the current actual pot reading. The downside of using one raw sample to start it is that if that one value is terribly noisy, i.e. way different from the pot reading, it will *still* take a few samples to converge.

    unsigned int AvgPot = analogRead(poti1);        // initialize pot average

    // Exponential averaging functions
    int16_t xpavg(int16_t newdat, int16_t avg)
    {
       // (3 * avg + 1 * newdat) / 4
       return( (((avg<<2) - avg + newdat) + 2) >> 2 );
    }

    void setup()
    {
       ;    // initializations go here
    }

    void loop()
    {
       int hue = analogRead(poti1);
       hue = xpavg(poti1, PotAvg);
       int mappedHue = map(hue, 0, 1023, 0, 65535);

       neoPixelStrip.fill(neoPixelStrip.gamma32(neoPixelStrip.ColorHSV(mappedHue, 255, 255)));
       neoPixelStrip.show();
    }


message:
How to dynamically set pinMode and dynamically write value to pins in Arduino using arrays?

I am trying to create a library called "LED" for LED Sequencer in Arduino IDE,where the user has to configure the LED pins using an array and pass it to  LED constructor in program and then will be able to use BlinkAll(),LeftToRight() and RightToLeft() Functions which lights the LED from Left to Right or Right to Left based on the sequence mentioned in the array. The problem is when I initiate the array and pass it to the constructor Pins 4,5,7 are not working at all , Please help me find what is the possible issue
the code in LED.cpp file is shown below

    #include <Arduino.h>
    #include "LED.h"

    LED::LED(int channel, int* a){
      _channel = channel;
      for(int i = 0 ; i < channel; i ++)
      {
        _pins[i] = a[i] ;
        pinMode(_pins[i],OUTPUT);
      }
    }
    void LED :: BlinkAll(int delay_ms)
    {
       for (int i = 0 ; i<_channel;i++)
       {
        digitalWrite(_pins[i],HIGH);
       }
       delay(delay_ms);
       for (int i = 0 ; i<_channel;i++)
       {
        digitalWrite(_pins[i],LOW);
       }
       delay(delay_ms);
    }
    void LED :: LeftToRight(int numberofLEDs, int delay_ms)
    {
      for(int i =0 ;i<_channel; i++)
      {
        digitalWrite(_pins[i], LOW);
      }
      for(int i =0 ;i<_channel; i++)
      {
    //    if (i > 0)
    //    {
    //     digitalWrite(_pins[i-1], LOW);
    //    }
      for(int i =0 ;i<_channel; i++)
      {
        digitalWrite(_pins[i], LOW);
      }
        digitalWrite(_pins[i], HIGH);
        delay(delay_ms);
      }
    }
    void LED :: RightToLeft(int numberofLEDs,int delay_ms)
    {
        for(int i =_channel ;i>0; i--)
      {
        digitalWrite(_pins[i], LOW);
      }
      for(int i = _channel ;i>0; i--)
      {
        if (i < _channel)
        {
         digitalWrite(_pins[i+1], LOW);
        }
        digitalWrite(_pins[i], HIGH);
        delay(delay_ms);
      }
    }
LCD.h

    #ifndef LED_h
    #define LED_h

    #include <Arduino.h>

    class LED
    {
    public:
      LED(int channel, int* pins);
      void BlinkAll(int delay_ms);
      void LeftToRight(int numberofLEDs, int delay_ms);
      void RightToLeft(int numberofLEDs, int delay_ms);
      void BlinkAlternate(int delay_ms);
    private:
      int _channel;
      int _pins[16];
    };

    #endif
Sample Code

    #include "LED.h"

    int const LED_pins[8]={5,6,7,8,9,10,11,12};
    int const No_of_LED = 8;

    LED led(8,LED_pins);

    void setup()
    {
      Serial.begin(9600);
    }
    void loop()
    {
      led.LeftToRight(1,500);
      led.RightToLeft(1,500);
    }

body:
It seems in the `LeftToRight` method, you didn't comment out enough (or too much), because  you use a for loop inside another for loop which is probably unintended.

In the `RightToLeft' method I will try to fix the problem, so you can change the `LeftToRight` method similarly.

    void LED :: RightToLeft(int numberofLEDs,int delay_ms)
    {
      for(int i =_channel ;i>0; i--)
      {
        digitalWrite(_pins[i], LOW);
      }
      for(int i = _channel ;i>0; i--)
      {
        if (i < _channel)
        {
         digitalWrite(_pins[i+1], LOW);
        }
        digitalWrite(_pins[i], HIGH);
        delay(delay_ms);
      }
    }

The first for loop is to switch off the LEDs, I would make this a separate method, so it can be reused in both the `LeftToRight` and `RightToLeft` method, so you get:

    void LED::RightToLeft(int numberofLEDs, int delay_ms)
    {
      switchAllLedsOff();

      for(int i = _channel; i > 0; i--)
      {
        if (i < _channel)
        {
         digitalWrite(_pins[i + 1], LOW);
        }
        digitalWrite(_pins[i], HIGH);
        delay(delay_ms);
      }
    }

    void LED::SwitchAllLedsOff()
    {
      for(int i = _channel; i > 0; i--)
      {
        digitalWrite(_pins[i], LOW);
      }
    }

The problem is also, that you always write HIGH to a pin, since it overwrite the possible LOW assignment within the if statement. To correct this you get:

    void LED::RightToLeft(int numberofLEDs, int delay_ms)
    {
      switchAllLedsOff();

      for(int i = _channel; i > 0; i--)
      {
        digitalWrite(_pins[i + 1], i >= _channel ? HIGH : LOW);
        delay(delay_ms);
      }
    }

You also can use an `if` statement, but in this case the ternary (? :)  statement is more clear since the assignment is for a single destination (_pins[i + 1]). So in case i >= channel, it is set to HIGH, otherwise set to FALSE. I also inverted the operator, but you can keep it inverted too.

Third, _channel seems to be the number of elements in the array, if you use it in an index it should start with _channel - 1, so you get:

    void LED::RightToLeft(int numberofLEDs, int delay_ms)
    {
      switchAllLedsOff();

      for(int i = _channel - 1; i >= 0; i--)
      {
        digitalWrite(_pins[i + 1], i >= _channel - 1 ? HIGH : LOW);
        delay(delay_ms);
      }
    }

There might be more problems, but the above are at lest some of them.

Also I changed your way of using spaces, although it's a matter of personal style, yours I never saw before (which doesn't mean it's incorrect of course).

(note I have not tested the program).


message:
Need a component to connect a 9v water pump

I have a 9v water pump that i want to turn on\off from the arduino,
What module should i use to turn it on and off?

I am using now a mosfet for my 5v pump, can a mosfet be used for 9v as well? Or is there some better solution?

body:
Yes, a MOSFET should work just fine. As others have said in the comments, you just need to make sure that it's rated for your desired voltage and current, and that it's threshold voltage is low enough (around 2.5V) that the Arduino is able to drive it to saturation with a logic level 1 (5V).

I've used this one (rated at 60V and 30A) in various projects:

https://www.sparkfun.com/products/10213