body:
I am somewhat familiar with Arduino and am trying to use two ultrasonic HC-SR04 modules to measure the direction of water flow inside a pipe. The sensors are kept as in the image below.
The direction is determined by emitting a signal from one sensor and receiving it in the other, noting down the time and repeating with the other set(See image below). This is called an ultrasonic time of flight flowmeter.

We then use the obtained values to determine the direction by seeing which one is greater. Further details on how it works can be found here: https://www.youtube.com/watch?v=rqJ9snaqYiY
![](https://aws1.discourse-cdn.com/arduino/original/4X/c/2/a/c2a54541f965aeda3f3366b1d01ab1739327414a.gif)

I used the below code to collect the data to determine direction for this but it doesn't seem to be working, as only the first value is displayed and the second is always 0.
```
long firstduration = 0;
long secondduration = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, INPUT);
pinMode(11, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13,LOW);
delayMicroseconds(2);
digitalWrite(13,HIGH);
delayMicroseconds(10);
digitalWrite(13,LOW);
firstduration = pulseIn(12,HIGH);
secondduration = pulseIn(11, HIGH);
Serial.print(firstduration);
Serial.print(", ");
Serial.println(secondduration);
}
```

Updated code:

```
long firstduration;
long secondduration;

void setup() {
  // put your setup code here, to run once:
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
pinMode(11, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13,LOW);
delayMicroseconds(2);
digitalWrite(13,HIGH);
delayMicroseconds(10);
digitalWrite(13,LOW);
firstduration = pulseIn(12,HIGH);

digitalWrite(10,LOW);
delayMicroseconds(2);
digitalWrite(10,HIGH);
delayMicroseconds(10);
digitalWrite(10,LOW);
secondduration = pulseIn(11,HIGH);

Serial.print(firstduration);
Serial.print(", ");
Serial.println(secondduration);
}
```

What would the issue be?

I am using an Arduino Uno board.

I have ruled out wiring issues...

Thanks!

title: How to use HC-SR04 sensor to make a flowmeter?

Answers: 1

body:
I don't think this will work with HC-SR04 sensors. They are not just ultrasonic transducers. They incorporate extra electronics for the sole purpose of measuring the time difference between the sended and received signal. They just don't provide the needed control on the ultrasonic transducers. To change their function to suite you you will need to fully understand their circuitry (I surely don't). Or build your own support circuitry. Additionally you need to think about how to transmit the ultrasonic wave into the fluid. It isn't enough to just tag a standard transducer to the pipe. The small transducers on the HC-SR04 sensors are made for transmitting through air, so the actual transducer is hidden behind the grill inside the capsule. I thin you need to at least bring the transducer itswlf directly to the pipe, if not directly to the water. You will need to buy fizting ultrasonic transducers.

 Also you have a problem due to the workings of `pulseIn()`.

If you want to read a HIGH pulse it will wait for the pin to go LOW, then to go HIGH again, and then counting the time until it goes LOW again. This means it blocks from the moment it is called until the pulse is finished (or the 1s timeout hits).

I will assume that you trigger the sensor which has its echo pin on pin 12, though a similar problem arises when you trigger the other sensor.

With the HC-SR04 the HIGH pulse will start at a specific time after the trigger pulse. Lets assume the pulse starts at the same time on both sensors (despite the described problems above). Directly after the trigger pulse you call `pulseIn()` on the sending sensor. It will block until it receives the each. Since this needs about twice as long as the other sensor to receive the signal (signal needs to go to the second sensor and then back to its source) the end of the echo pulse from the receiving sensor will be inside this time, thus putting its echo pin low again. The sending snsor receives its echo, the `pulseIn()` function ends. Now you call `pulseIn()` on the receiving sensor. But its pulse already ended, so the function waits and waits for the pulse to start and finally reaches its timeout. In this case it returns zero.

You would need to check the pins in a non-blocking way. Maybe even an Input Capture Interrupt would be fitting (depending on the requirements on the resolution).


body:
I am new to programming and am trying to create an array of 10 thermistors on one arduino. So far I have found a code for Temperature Sensor Using NTC Thermistor that is only for one thermistor. How do I fix this code for 10 thermistors?
Here is the link where I found the code.
https://thecustomizewindows.com/2020/10/temperature-sensor-using-ntc-thermistor-arduino-ntc/
The code below has been changed a little for the thermistor I am using.
Thank you.
~~~
#define RT0 10000   // Ω
#define B 3470      // K
#define VCC 5       //Supply voltage
#define R 10000     // R=10kΩ

float RT, VR, ln, TX, T0, VRT;

void setup() {
  Serial.begin(9600);
  T0 = 25 + 273.15;                 //Temperature, Celsius to Kelvin
}

void loop() {
  VRT = analogRead(A0);              //Acquisition analog value of VRT
  VRT = (5.00 / 1023.00) * VRT;      //Conversion to voltage
  VR = VCC - VRT;
  RT = VRT / (VR / R);               //Resistance of RT

  ln = log(RT / RT0);
  TX = (1 / ((ln / B) + (1 / T0))); //Temperature from NTC thermistor

  TX = TX - 273.15;                 //Conversion to Celsius

  Serial.print("Temperature:");
  Serial.print("\t");
  Serial.print(TX);
  Serial.print("C\t\t");
  Serial.print((TX * 1.8) + 32);    //Conversion to Fahrenheit
  Serial.println("F");
  delay(500);
}
~~~

title: How do I create a code that lets me use an array of 10 thermistors on one arduino?

Answers: 1

body:
You haven't said which (of the many kinds) of "Arduino" you plan to use, but Uno being the most common, I'll assume that. You also haven't said why/whether you must use a particular temperature sensor, but if you can be flexible about that, there will more choices of solution.

First, thermistors: You read a thermistors with an an A/D converter. Uno has an A/D converter but it has only 6 channels available - not enough for your spec of 10 sensors.
 - You could add an external A/D multiplexer with 5 channels fed into one of your on-board channels plus the remaining 5 on-board channels.
 - Or add a 10-channel multiplexer for all 10 sensors and feed it to one of the on-board A/D channels. This is better because it's simpler - all of the sensor signals will be processed identically, and all accessed the same way in the code.
- Add an external 10-channel A/D. It will cost a little more but will be even easier to work with.

If you don't require thermistors, I'd suggest the Dallas/Maxim DS18b20 "1-wire" sensors. The "1-Wire" bus is a proprietary protocol developed by Dallas Semiconductor on which these sensors depend. You use one, simple, twisted-pair bus to access all of them, needing only 1 Arduino pin (and ground) to provide both power and communication to/from the sensors. Libraries are available to manage the bus and the communication. The 1-wire bus can be in a linear or a branched configuration, to suit your installation. You can use 2 wires (ground and power+data) or 3 wires (ground, power, data). Here's a link to the [DS18b20 data sheet][1] for the details.

Update:<br>
I didn't really answer your question about *coding* for 10 thermistors (since you can't directly connect 10 thermistors to an Uno), I'll try to address that now without getting into any more detail about what kind of sensors you use or how you connect them.

You'll need to read 10 sensors, compute the sensor data to temperature and store it, and do whatever you plan to do with the temperature value. Your present code prints it (as both degC and degF, but let's just describe that part as printing the value).

You'll need a `for( )` loop that executes 10 times, once for each sensor, where the loop index tells which sensor (0-9) you'll be processing. In time the loop executes, read a sensor, compute the temperature from what the sensor gives you, if it isn't already in the units you need, and print the value. So structuring that as if it were code (but I won't be writing it out in full C++) it would look like:

```
tell all 10 sensors to take a measurement; // if necessary
for( i = 0; i < 10; ++i ){
   read data from sensor[i];
   convert it to temperature;      // depends on the sensor
   print the temperature value;
}
```
If that is all you want to do, code this loop in your `setup()` function. Each time you reset the Arduino, it will measure and print all 10 temperatures and stop (actually, loop forever, doing nothing, until you power it down). If you want it to do this periodically, code this loop in your `loop()` function, with a `delay(5000);` statement after it. Now it will print the 10 temperatures every 5 seconds. You can change the 5000 (it's in milliseconds) to whatever period you want.

But it's the details that get you. You've got code to read and convert thermistors but your Arduino can't directly connect to 10 thermistors. If you use an external multiplexer, then you'll have to include code for that. If you use DS18b20 1-Wire sensors, you'll need to download and use the code library for those, as well as converting their 1/16-degC units to the units of your choice.

I'd recommend you start with 2 or 3 thermistors since you have the code for them, and write the loop for those. Once that's working, you'll have the answer to your title question. Then you'll have to decide how to connect 10 sensors of some kind, as I wrote about earlier. That will involve some additional coding, for an analog multiplexer, or for 1-Wire sensors, or whatever solution you choose. Just expand your experiment by small steps at a time and get each step working before you move on.

  [1]: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf


body:
I just start a project migrating from Arduino Uno to NodeMCU, basically this projects control some selenoid motors triggered by a website instructions. For example: if website returns 2, the motor #2 start work.

I hace everything ready, fetching from the website but, the motors are not working. In Arduino Uno works but in NodeMCU did not.

The selenoid motor is this one from AliExpress: [the 3v --> https://es.aliexpress.com/item/4000807560712.html](https://aliexpress.com/item/4000807560712.html)

The circuit I made is like this:

**NodeMCU / Motor**

D2 -> Positive motor wire (red wire).

3.3V -> Positive motor wire (red wire).

GND -> Negative motor wire (black wire).

All this is attached to a protoboard for easy my work, but those are the connections of the circuit.

At otherwise, the code looks something like this:
```
void setup() {
  Serial.begin(115200);
  pinMode(D2, OUTPUT);

}

void loop() {
  digitalWrite(D2, LOW);
  delay(3000);
  digitalWrite(D2, HIGH);
  delay(3000);

}
```
I also tried instead of pin name `D2` use `4` corresponding of the GPIO number. Didn't work.

The wired part is in Arduino Uno the same code and same circuit works perfectly, but in NodeMCU not.

title: Selenoid motor not responding to NodeMCU instructions

Answers: 1

body:
So you connected 3.3V and pin D2 both to the positive motor wire. When the pin is high the motor runs and gets its current from the 3.3V pin. When you set the pin to LOW you essentially short the 3.3V pin to ground through the output hardware of the pin. That is highly risky. The output hardware of the pin only can withstand a certain amount of current (around 20mA, 40mA absolute max). Since you have nothing that would limit the current in this path you can easily fry the output hardware of the pin. **Don't do that!** The same is valid for driving a motor directly only from the digital output pin.

The correct way of controlling a motor with an Arduino or ESP is by using a fitting driver. For just turning it on and off a simple transistor or MOSFET is sufficient (depending on the current draw of the motor a MOSFET is better than a bipolar transistor). Google something like "MOSFET as switch" and you will get many tutorials on that. You can also add the term "Arduino" and you will get tutorials specifically for Arduinos.

Why it worked for the Uno but not for the NodeMCU? You didn't describe how exactly you connected it on the Uno (to the 3.3V pin or the 5V pin?). Probably the digital output pin on the NodeMCU cannot sink enough current so that it is not able to lower the voltage enough. Though if it would be able to do this it would also cause a power failure for the whole microcontroller. Maybe that also happened with your Uno difficult to say without more information.


body:
Years ago, I encountered a fairly long debate at AVRfreaks.net about whether or not it's OK to directly connect AVR pins that are configured as inputs with internal pullup enabled directly to ground through a button or switch.

From what I recall, the consensus at the time was that it's not (at least, for switches likely to be *closed* and *conducting* at reset), because at least *some* AVR MCUs weren't absolutely *guaranteed* to have their pins in high-impedance state at reset (at least, not if the chip reset while in brownout state, or something like that).

Thinking back, I'm not sure whether the warnings even applied to the chips **I** was introduced to at the time (ATtiny2313, ATtiny13, and ATmega162)... I just remember that the others on the site were pretty adamant that I was courting disaster, and I took their word for it.

Fast forward ~15 years.

The **current** datasheet for the ATmega2560 (at https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega640-1280-1281-2560-2561-Datasheet-DS40002211A.pdf ) **does** seem to guarantee (on pages 7-9) that the pins used by ports A through L **are** tri-state high-impedance input pins at reset under all conditions, including when the clock is halted (obviously, any pin Arduino's bootloader subsequently makes use of could behave differently if I tried to hijack it for double-duty reading a button/switch... but the Mega2560 has so many pins, there really isn't any *reason* to reuse pins Arduino uses for its own purposes).

Offhand, I can think of three potential remaining concerns:

* If I screw up and accidentally execute code that configures a pin connected directly to ground through a closed switch as a high output, the pin (and probably the whole chip) is likely to go up in smoke. I can deal with this risk.

* If I were powering it with batteries, I might care about the constant power drain wasting battery life. I'm not, and I don't.

* I'm not quite sure whether "OK to connect directly to ground through a *momentary* pushbutton" necessarily equates to "OK to connect directly to ground through a *switch* that might conceivably be closed and continuously conducting current for the life of the circuit.

So... have I missed anything? And **is** #3 a legitimate concern, or is it something that's not worth caring about if you're talking about something like 8 pins connected through a DIP switch?

Illustrative circuit:

`Ground <-- [switch] --> IO pin (configured for input, internal pullup enabled)`

title: Do you actually NEED external resistors for switch connecting IO pin to ground?

Answers: 2

body:
> If I screw up and accidentally execute code that configures a pin connected directly to ground through a closed switch as a high output, the pin (and probably the whole chip) is likely to go up in smoke. I can deal with this risk.

In addition to “there is an obvious bug”, there could also be a bug that is only triggered in very rare conditions, or a “cosmic ray” event which causes the processor to mis-execute instructions.

Or, maybe a metal tool or piece of debris fell across two of the processor pins and the other pin _is_ an output that's currently high. (This is impossible to protect against in all cases, of course.)

In any of these cases, the hazard is that you're **turning a situation where the device misbehaves temporarily into a situation where the device is permanently damaged.**

Will this happen? Likely not. Is it worth protecting against? Depends on your application, on hard-to-quantify risk probabilities, and how much of a bother it would be to replace the processor.

body: It is usually better to use a resistor when connecting to either +5V or ground. This protects the uP against software bugs, particularly during a hectic debug effort. Even moving on to the next project while forgetting the existing wiring can be a source of trouble.


body:
[![proteus schematic][1]][1]

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

    #include <Servo.h>

    #define potpin A0
    int savebutton=2;
    int button1=3;
    int button2=4;
    int button3=5;

    Servo servo1;
    Servo servo2;
    Servo servo3;

    int servo1ipos=90;
    int servo2ipos=90;
    int servo3ipos=90;

    int val=0;

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(savebutton,INPUT);
      pinMode(button1,INPUT);
      pinMode(button2,INPUT);
      pinMode(button3,INPUT);

      servo1.attach(6);
      servo2.attach(7);
      servo3.attach(8);
      servo1.write(servo1ipos);
      servo2.write(servo2ipos);
      servo3.write(servo3ipos);

    }

    void loop() {
      // put your main code here, to run repeatedly:
      servocontrol();

    }

    int servocontrol(){
      if(digitalRead(button1)==LOW){
        potvalue();
        Serial.println(val);
        servo1.write(val);
        delay(200);
      }
    }
    int potvalue(){
        val=analogRead(potpin);
    }

Basicly I only read 1023 value instead of potentiometer value I couldnt figure it out need help.

Update:
I came up solution by connecting 5V into Arduino's VCC pin.

title: Proteus potentiometer value always highest

Answers: 1

body: I came up solution by connecting 5V into Arduino's VCC pin.


body:
This code  compiles without problem, but doesn't work at all... The switch does not set the LED low and the while loop doesn't work, plus the serial print doesn't print....
Until I put the while loop in pressing either button would change the LED state. I wanted to make the sequence "button one, LED change, THEN button two"
I'm certain to have missed something...
```
const int buttonPin1 = 2;     // the number of the pushbutton 1 pin 2
const int buttonPin2 = 7;     // the number of the pushbutton 2 pin 7
const int ledPin = 13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int i = 0;

void setup() {
// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin 1 as an input:
  pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin 2 as an input:
  pinMode(buttonPin2, INPUT);
  Serial.begin(9600);
}

void loop() {

  digitalWrite(ledPin, HIGH);

// read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);

// check whether a  pushbutton is pressed.
// if it is, the buttonState is HIGH:

  if (buttonState1 == HIGH) {
// turn LED off:   //
    digitalWrite(ledPin, LOW);
    while (1) {
      (buttonState1 == LOW);
      if (buttonState1 == LOW) {
        Serial.println("1");
      }
      {
        break;
      }
    }

    delay[1000];

    digitalWrite(ledPin, HIGH);

// read the state of the pushbutton 2 value:
    buttonState2 = digitalRead(buttonPin2);
    if (buttonState2 == HIGH) {
// turn LED off:
      while (1) {
        if (buttonState2 == LOW) {
          break;
        }
      }
      digitalWrite(ledPin, LOW);

      delay(10000);
      digitalWrite(ledPin, HIGH);

    }
  }
}

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

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

title: "Simple" code...but what have I done wrong?

Answers: 1

body:
I think there’s a misunderstanding of how variables work.

Your code is only reading the real state of *buttonPin1* once (before the first `if ()` statement) and copies the value read from the I/O port *as it is at that moment* to the variable *buttonState1*, by executing `buttonState1 = digitalRead(buttonPin1);`.

After you’ve copied the value read from the *buttonPin1* port to the *buttonState1* variable, this variable will no longer change or reflect the actual button status. I.e. the value of the variable *buttonState1* will not automagically change when you push or release button 1. *buttonState1* is a normal program variable and not coupled to the I/O port.

If you want to continue to check the current button status, you have to continuously read the I/O port, by using `digitalRead()` statements where appropriate.

The same goes for *buttonPin2* and *buttonState2*.


body:
I am trying to **read potential meter data from Arduino using python**, with the program on Arduino as follows :
```
#include <cvzone.h>

SerialData serialData;
int sendVals[2];

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

void loop() {
  int potVal = analogRead(A0);
  sendVals[0]= potVal;
  serialData.Send(sendVals);
}
```
the program on the arduino side is running fine

and program in python as follows
```
from cvzone.SerialModule import SerialObject

arduino = SerialObject("COM7")

while True:
   data = arduino.getData()
   print(data[0])
```
but I get this error:
```none
Traceback (most recent call last):
    data = arduino.getData()
  File "C:...\site-packages\cvzone\SerialModule.py", line 68, in getData
    data = data.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf7 in position 0: invalid start byte
```
How to solve it?

title: get data from arduino using python

Answers: 2

body:
I would embed the call to `.getData()` into a `try` block and handle this specific exception with `except UnicodeDecodeError`. There can always be a transmission error, especially when the script is started.

However, I would do it differently:

    void loop() {
      Serial.println(analogRead(A0));
    }

...and on the python side use `serial.readline()` from pyserial instead.
But I don't know cvzone and you might have a reason to use this instead of the commonly used pyserial.

body:
There seems that there *may* be an issue with the implementation of `getData()` in [`SerialModule.py`][1].

From [line 62][2]

```python
    def getData(self):
        """
        :param numOfVals: number of vals to retrieve
        :return: list of data received
        """
        data = self.ser.readline()
        data = data.decode("utf-8")
        data = data.split('#')
        dataList = []
        [dataList.append(d) for d in data]
        return dataList[:-1]
```

The block of code containing the `readLine()` should probably be with in a `try...except` block, like so:

```python
    def getData(self):
        """
        :param numOfVals: number of vals to retrieve
        :return: list of data received
        """
        try:
            data = self.ser.readline()
            data = data.decode("utf-8")
            data = data.split('#')
            dataList = []
            [dataList.append(d) for d in data]
            return dataList[:-1]
        except Exception as e:
            print(e)
            self.ser.close
```

An issue has been raised on Github: [SerialModule.py - readLine() should be within try block. #49][3]

  [1]: https://github.com/cvzone/cvzone/blob/master/cvzone/SerialModule.py
  [2]: https://github.com/cvzone/cvzone/blob/master/cvzone/SerialModule.py#L67
  [3]: https://github.com/cvzone/cvzone/issues/49


body:
This is the code. The /// are where I added the countdown. Unfortunately the countdown only goes down 9 seconds. What I want it to do is go down the full 24 hours. I have been trying to fix this error for months with no luck.

```
int beeper_pin      = 2; //Naming buzzer pin
int resetGarden_pin = 3; //Naming the warning led pin
int pump_pin        = 6; //Naming the pump pin

#include <LiquidCrystal_I2C.h> // Driver Library for the LCD Module

LiquidCrystal_I2C lcd(0x27, A4, A5);

void clearLCDLine(int line)
{
  lcd.setCursor(0, line);
  for (int n = 0; n < 20; n++) // 20 indicates symbols in line. For 2x16 LCD write - 16
  {
    lcd.print(" ");
  }
}

void beepWarning() {            //Makes beeping a subroutine

  lcd.setCursor(0, 0);
  lcd.print("WARNING");
  delay(100);
  lcd.setCursor(0, 1);
  for (int x = 0; x < 3; x++) {

    lcd.setCursor(0, 1);
    lcd.print("WATER PUMP ACTIVE");
    delay(100);
    clearLCDLine(1);
    lcd.setCursor(0, 1);
    lcd.print("WATER PUMP ACTIVE.");
    delay(100);
    clearLCDLine(1);
    lcd.setCursor(0, 1);
    lcd.print("WATER PUMP ACTIVE..");
    delay(100);
    clearLCDLine(1);
    lcd.setCursor(0, 1);
    lcd.print("WATER PUMP ACTIVE...");
    delay(100);
    clearLCDLine(1);
  }

  lcd.setCursor(0, 1);
  lcd.print("WATER PUMP ACTIVE...");

  for (int x = 0; x < 3; x++) {   //Repeats 3 times

    digitalWrite(beeper_pin, HIGH); //Turn on buzzer
    delay(100);                     //Delay of 400 milliseconds
    digitalWrite(beeper_pin, LOW);  //Turns off buzzer
    delay(100);                     //Delay of 400 milliseconds

  }
}

void SOSWarning() {            //Makes SOS a subroutine

  clearLCDLine(1);
  lcd.setCursor(0, 0);
  lcd.print("WPA Reset Aerogarden");

  for (int x = 0; x < 3; x++) {   //Repeats 3 times

    for (int d = 0; d < 3; d++) {   //Repeats 3 times
      digitalWrite(beeper_pin, HIGH); //Turn on buzzer
      delay(200);                     //Delay of 400 milliseconds
      digitalWrite(beeper_pin, LOW);  //Turns off buzzer
      delay(200);                     //Delay of 400 milliseconds
    }

    for (int a = 0; a < 3; a++) {   //Repeats 3 times
      digitalWrite(beeper_pin, HIGH); //Turn on buzzer
      delay(400);                     //Delay of 400 milliseconds
      digitalWrite(beeper_pin, LOW);  //Turns off buzzer
      delay(400);                     //Delay of 400 milliseconds
    }

    for (int d = 0; d < 3; d++) {   //Repeats 3 times
      digitalWrite(beeper_pin, HIGH); //Turn on buzzer
      delay(200);                     //Delay of 400 milliseconds
      digitalWrite(beeper_pin, LOW);  //Turns off buzzer
      delay(200);                     //Delay of 400 milliseconds
    }

    delay(500);                     //Delay of 500 milliseconds
  }
}

void weekDelay() { //Makes waiting for a week a subroutine
  clearLCDLine(1);
  lcd.setCursor(0, 0);
  lcd.print("Waiting One Week");

long hour = 23, minute = 59, second = 59;//second //////////////////////////
long countdown_time = (hour * 3600) + (minute * 60) + second; ///////////////////////////////////////////////////////////////////////////

  long countdowntime_seconds = countdown_time - (millis() / 1000); /////////

  if (countdowntime_seconds >= 0) {   ///////////////////////////////////////////////
    long countdown_hour = countdowntime_seconds / 3600;
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
    lcd.setCursor(4, 1);
    if (countdown_hour < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_hour);
    lcd.print(":");
    if (countdown_minute < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);
  }
  delay(500);///////////////////////////////////////////////////////////////

  delay(500);

  delay(86400000);
  clearLCDLine(0);
  lcd.setCursor(0, 0);
  lcd.print("Waiting Six Days");
  delay(86400000);
  clearLCDLine(0);
  lcd.setCursor(0, 0);
  lcd.print("Waiting Five Days");
}

void pumpOn() {               //Makes turning on the pump a subroutine
  clearLCDLine(1);
  lcd.setCursor(0, 1);
  lcd.print("PUMPING");
  delay(100);
  digitalWrite(pump_pin, HIGH); //Turning on the pump
}

void pumpOff() {              //Makes turning off the pump a subroutine
  digitalWrite(pump_pin, LOW);  //Turning off the pump
  clearLCDLine(1);
  lcd.setCursor(0, 1);
  lcd.print("WATER PUMP OFF");
  delay(100);
}

void setup() {
  //run once:
  lcd.backlight();
  lcd.init();

  lcd.setCursor(4, 0);//////////////////////////////////////////////////////
  lcd.print("HH:MM:SS"); //////////////////////////////////////////////////

  lcd.clear(); // Clear the screen
  lcd.setCursor(0, 0);
  lcd.print("Aero_Pump V3");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print("By Alex");
  delay(1000);
  pinMode (beeper_pin,      OUTPUT); //Setting outputs
  pinMode (resetGarden_pin, OUTPUT); //Setting outputs
  pinMode (pump_pin,       OUTPUT); //Setting outputs
  lcd.clear(); // Clear the screen

  digitalWrite(resetGarden_pin, LOW); //Turning off the reset garden light

  beepWarning(); //Beeps a warning
  pumpOn();      //Turns on the pump
  delay(3000);  //Adds a delay of 24000 milliseconds
  pumpOff();     //Turns off the pump
  weekDelay();   //Waits a week

  beepWarning(); //Beeps a warning
  pumpOn();      //Turns on the pump
  delay(135000);  //Adds a delay of 48000 milliseconds
  pumpOff();     //Turns off the pump
  weekDelay();   //Waits a week
}

void loop() {

  SOSWarning(); //Beeps an SOS warning
  pumpOn();      //Turns on the pump
  delay(48000);  //Adds a delay of 48000 milliseconds
  pumpOff();     //Turns off the pump

  for (int x = 0; x < 3024000; x++) {
    lcd.backlight(); // turn on backlight.
    delay(100);
    lcd.noBacklight(); // turn off backlight
    delay(100);
  }
  lcd.backlight(); // turn on backlight.
}
```

This is more specifically what needs to be fixed:

```
long hour = 23, minute = 59, second = 49;//second //////////////////////////
long countdown_time = (hour * 3600) + (minute * 60) + second; ///////////////////////////////////////////////////////////////////////////

  long countdowntime_seconds = countdown_time - (millis() / 1000); /////////

  if (countdowntime_seconds >= 0) {
    long countdown_hour = countdowntime_seconds / 3600;
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
    lcd.setCursor(4, 1);
    if (countdown_hour < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_hour);
    lcd.print(":");
    if (countdown_minute < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);
  }
```

This is some edited code that I have tried but might have made a mistake on because it does 0 days instead of 7 and (Hours/Min/Sec) 04:09:36.
```
void weekDelay() { //Makes waiting for a week a subroutine
  clearLCDLine(1);
  clearLCDLine(0);
  lcd.setCursor(0, 0);
  lcd.print("Waiting One Week");

  unsigned long start = millis();
  unsigned long week_secs = 7 * 24 * 3600;
  unsigned long countdowntime_seconds;

  while (countdowntime_seconds = week_secs - (millis() - start)/1000){
     char buff[80];
     unsigned long countdown_day = countdowntime_seconds / 3600 / 24;
     int countdown_hour = (countdowntime_seconds / 3600) % 24;
     int countdown_minute = ((countdowntime_seconds / 60) % 60);
     int countdown_sec = countdowntime_seconds % 60;
     sprintf(buff," days, d:d:d",
            (int) countdown_day,
            countdown_hour,
            countdown_minute,
            countdown_sec
            );
     Serial.print(buff);
     clearLCDLine(1);
     lcd.setCursor(2, 1);
     lcd.print(buff);
     delay(1000);
     }
}
```

title: I have been having a lot of trouble adding a countdown to a piece of code

Answers: 1

body:
I'd try something with a `while(){}` loop and a `sprintf()`, like:

    void weekDelay() { //Makes waiting for a week a subroutine
      unsigned long start = millis();
      unsigned long week_secs = 7 * 24 * 3600;
      unsigned long countdowntime_seconds;

      while (countdowntime_seconds = week_secs - (millis() - start)/1000){
         char buff[80];
         unsigned long countdown_day = countdowntime_seconds / 3600 / 24;
         // or maybe
         //int  countdown_day = (int)(countdowntime_seconds / 3600UL / 24UL);

         int countdown_hour = (countdowntime_seconds / 3600) % 24;
         int countdown_minute = ((countdowntime_seconds / 60) % 60);
         int countdown_sec = countdowntime_seconds % 60;
         sprintf(buff," days, d:d:d",
                (int) countdown_day,
                countdown_hour,
                countdown_minute,
                countdown_sec
                );
         clearLCDLine(1);
         lcd.setCursor(0, 0);
         lcd.print(buff);
         delay(1000);
         }
    }

Or make it more general and start it off like:

    void coundownDelay(unsigned long seconds) { //Makes waiting for a week a subroutine
      unsigned long start = millis();
      unsigned long countdowntime_seconds;

      while (countdowntime_seconds = seconds - (millis() - start)/1000){

         ...

    }

and call like:

      ...
      countdownDelay(7 * 24 * 3600);   //Waits a week
      ...


body:
My scenario is the following:

I have a few buttons connected to an Arduino Nano, which is connected to my PC over Serial. Now I want to send a few bytes whenever a button is pressed, to be interpreted by my Computer. However, I noticed a significant, unregular delay (feels like something between almost instantaneous transmission up to a delay of easily ~300 milliseconds), until the message is received by my computer, and I want to get rid of that.

I understand that Arduinos make use of a Serial Buffer, and only send over data once the buffer is full (or some sort of timeout probably), and I think that is the culprit - however I don't see a way of forcing the Arduino to send the data ASAP. I tried just sending over exactly 64 bytes (which, according to what I read, should be the buffer size), but that doesn't help as well (can I however decrease the buffer size or is there a way to send bytes exactly until the buffer is full?)

I don't think baudrate changes anything either, as I'm experiencing problems with the "ping" rather than with the bandwidth (and I also tried anything from 9600 to 115200).

Maybe I'm also completely wrong and the delay happens on the side of my computer, but I don't know if there is anything to change that - I'm looking at the Serial Monitor or at a small `pyserial` script, both of which have the delay:
```
arduino = serial.Serial(serial_port}, 115200, timeout=.1)
while True:
    data = arduino.readline().decode("UTF-8").strip()
    if data:
        print(data)
```

The code on the Arduino-side boils down to:

```
#include <digitalWriteFast.h>

#define button1_pin 2

int button1_state = 0;
int last_button1_state = 0;

void setup() {
  Serial.begin(115200);
  pinMode(button1_pin, INPUT_PULLUP);
  while (!Serial) { delay(50); }
  Serial.println("Started");
}

void loop() {
  button1_state = digitalReadFast(button1_pin);

  if (button1_state != last_button1_state) {
    if (button1_state == LOW) {
      Serial.write("d\n");
    } else {
      Serial.write("u\n");
    }
    last_button1_state = button1_state;
    delay(20);
  }
}
```

Then there's also the weirdest thing which I noticed but am not sure about if it's at all plausible:
When the window of the Serial Monitor (the one that ships with the Arduino IDE) is not completely full (meaning there are only some 1-20 rows and the window doesn't need to scroll), I notice the aforementioned delay, but if the Serial Monitor is full (so new content will be on the last line and the window automatically scrolls a bit), I don't. As soon as I clear the output of the window, it's there again. I don't know what to make of this observation, because I definitely don't have any reasonable clue why that may be the case, so maybe it's also just my imagination :shrug:. Using the python-code there is no such behaviour, and the transimission always seems to have a ping of at least 200-300ms.

I hope my problem is understandable, and like I said I am really unsure what even is the culprit (and I see that it's also highly likely that it's on the side of my computer), but does anybody have any idea how I can decrease this delay? Thanks a lot!

title: Arduino Nano: Send a few bytes over Serial with as little delay as possible

Answers: 2

body:
>"Slowing down to speed up". _-   a Chinese proverb_

The problem is with your python code. Not sure what your computer's operating system is, but if you are using Mac or Linux, if you run `top` on command line, you will see you have a high CPU utilization causing by your python script. This is because your your python `while True` loop constantly looping without giving the chances for other processes on your computer to use the CPU, bear in mind that Linux, MacOS or Windows are not designed as a real-time operating system.

So how to solve the problem? put your `while True` loop to sleep for a short period of time, this allows other processes on your computer to have chance to run and you will see the CPU utilization go down significantly and everything will running smoothly.

```
import time
arduino = serial.Serial(serial_port}, 115200, timeout=.1)
while True:
    data = arduino.readline().decode("UTF-8").strip()
    if data:
        print(data)
    time.sleep(0.01)
```

With this slight delay, I've seen some CPU utilisation drop from 90% to 3.5%. This is quite a common mistake that many python programmers made, including some quite experienced programmers who are not familiar with network programming.

Try it out and let me know.

body:
Answering my own question in case anybody experiences the same problem eventually: @hcheung's answer was almost correct after all - see the comments to that question.

Important take-away however:
**don't trust the Serial Monitor or Python's `print` to check how quickly the arduino and your computer communicate**!

When ponging back to the Arduino to let it trigger an LED, I noticed that the actual transmission is definitely faster than my eye, far from the 200ms delay I mentioned in the question.
So the problems for me were

a) the *other* code I used to test the connection but didn't post here was faulty
b) The delay I saw in the Serial Monitor/python's `print` is **not the delay of the serial communication** but apparently somewhere later in the printing of the respective content.

Serial communication is definitely faster than the human eye :)


body:
For school we program an arduino with visual studio and micro visio. Since a few days ago my arduino wont run the code of the arduino. At first it didnt give any errors until i tried to upload a code with arduino ide itself.
avrdude: ser_open(): can't set com-state for "\\.\COM3"
Or saying that the acces is denied. I confirmed that my arduino is the COM3 port. The weird part is that on another pc it works fine. On the laptop where my arduino isnt working i did a loopback test following. https://support.arduino.cc/hc/en-us/articles/360020366520.
This went without a problem. I reinstalled the IDE and drivers but sadly this didnt help either. If i try a class mates his arduino it wont work either. If some one has information on how to fix this problem that would be nice. If there is some extra information needed that wont be a problem either.

title: My arduino gives me several avrdude erros

Answers: 0


body:
I am creating home automation using esp's and little home server. Idea is to connect arduinos to server, add each of them id and let them talk via websockets with server and server will forward (by some logic inside) messages to different arduinos. Pretty simple.

What I found, is that I cannot tell, what websocket client I am talking to. I use simple example from lib: https://github.com/gilmaimon/ArduinoWebsockets

    client.onMessage(onMessageCallback);
    client.onEvent(onEventsCallback);
    client.connect(websockets_server_host, websockets_server_port, "/");
    client.ping();

My idea is to send id of arduino with connection handshake. Is it even possible?

title: Arduino websockets send id in connection

Answers: 0


body:
I wanted to simulate the Dawn light with my arduino Uno using array implementation.
I desired to change the brightness of the LEDs using fade effect( Smoothly and keep it unchanged until the next function called out) from 0 brightness to specific values in array ( it means specific brightness for each LED).
I wrote my code and compiled it but unfortunately nothing happens when I upload it to my device.
The device is already connected and I have tested some other on/off led codes.
Here is my pre-code:

```c++
//Dawn Light Simulation
int LEDArray[] = {3, 5, 6, 9, 10, 11};
int LedNum = 6;
int i;
int j;

int Dawn[] = {30, 20, 20, 20, 100, 0};

int Current[] = {0, 0, 0, 0, 0, 0};
unsigned long time_for_fadestep;
#define Fade_Interval 1200;

void setup() {

  for (int i = 0; i < LedNum; i++)
    pinMode(LEDArray[i], OUTPUT);

}

void DawnMode()
{
  if (millis() > time_for_fadestep) {
    time_for_fadestep = millis() + Fade_Interval;
    for (int j = 0; j < LedNum; j++){
        if (Dawn[j] > Current[j]) Current[j++];
        if (Dawn[j] < Current[j]) Current[j--];
          analogWrite(LEDArray[j], Current[j]);

    }
  }
}
void loop() {
  DawnMode();
  delay(5000);
}
```

title: increment or decrement the values of an array

Answers: 0


body:
My Arduino code is not getting uploaded. It gives me "avrdude: ser_open(): can't set com-state for "\\.\COM4" error. It worked literally 5 mins ago. now it doesn't work. I updated the drivers and stuff. I have a new Arduino and my code worked 5 mins ago. when i made some changes to the code It stopped working. I am using an (unofficial) Arduino Uno R3. the USB to UART chip's name is ATMEL Mega 16U2 2048 TW which I'm pretty sure is the Arduino mega chip?? my Arduino uses the ATMEL ATMEGA328P U. Please help
Thanks in advance, Paradox

Answers: 0


body:
I'm trying to post the buffer generated by the ESP32 camera to my personal API service I have built with Node.JS. The post gets sent successfully. I have yet to test it on large image (higher resolution).

I'm using a `FRAMESIZE_HQVGA,    // 240x176` for my images

    void postImage(uint8_t* buf, size_t len)
    {
      // prepare JSON document
      DynamicJsonDocument doc(len);
      doc["buffer"] = buf;

      HTTPClient http;

      // Serialize JSON document
      String json;
      serializeJson(doc, json);

      http.begin("http://192.168.0.147:3001/api/pictures");
      http.addHeader("Content-Type", "application/json");

      int httpResponseCode = http.POST(json);

      if (httpResponseCode > 0) {
        // response
        String response = http.getString();
        Serial.println(httpResponseCode);
        Serial.println(response);
      } else {
        Serial.print("Error sending POST: ");
        Serial.println(httpResponseCode);
      }

      // disconnect
      http.end();
    }

In my Node.JSside, I receive the buffer but I'm unable to de serialize it to an image. This i smy body:

    { buffer: '����' }

I'm wondering what I'm doing wrong here. Should the image buffer be sent in base64, and if so, how? **I want to be able to transform the image buffer into a proper image on the API side.**

Here's my how I try to transform the image buffer to a proper image (in my API controller):

        const imageBuffer = Buffer.from(req.body.buffer);
        const filename = `./resources/pictures/image_${Date.now()}.jpg`;

        fs.writeFile(filename, imageBuffer,function(err) {
            if (err) {
                console.log(err);
            } else {
                console.log(`Writing ${filename}`);
            }
        });

title: POST ESP32 image buffer using HTTPClient to Node.JS API

Answers: 0


body: I want to know is it possible that arduino can show how much power my prototype is drawing from arduino and if it can what would be the code for that.

title: Can arduino show the power drawn from its output pin on serial monitor? If yes, what code should I use?

Answers: 2

body:
No, it cannot do power measurement. With an Arduino (and most other microcontrollers) you only have voltage measurement (on the analog input pins).

Though this is half of the power measurement, since power is voltage multiplied by current. Measure the voltage on that pin with an analog pin. Then you need a way to measure the current. For that you need extra hardware. There are current sensors made for Arduino, which measure the current flowing through its pins and giving out a corresponding analog voltage, which you can then measure on an analog pin. Make sure that the current sensor is rated for the correct current range to not overload it or loose resolution (by having a way bigger range in the sensor than with the actual current). For a digital output pin of an Arduino Uno/Nano the max. current should be 20mA.

You can also google for something like "Arduino current sensor". A short search gave me some promising results.

Then when you have measured the voltage and current you can multiply them to get the power.

body:
You cannot directly measure the power, no. But it's simple enough to arrange using a very small amount of extra hardware.

Depending on what the device is you want to monitor there are two ways of creating the circuit that you need. Both require just a single small resistor, and either one or two analog pins.

The simplest arrangement is the "low-side current shunt" and is simply a small resistor between the ground of your device and the ground of the Arduino. This causes a small voltage drop which raises the ground potential of your device slightly, so could have an effect on communication if you're not careful (see the alternative below). Then a connection from the "top" of that resistor to an analog input will allow you to monitor the voltage drop across that resistor. From that, using Ohm's Law, you can calculate the current. That then can give you the power.

Here's a schematic:

<!-- 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/MJRlq.png)

<!-- End schematic -->

This arrangement does have the benefit that it will work for any power supply voltage, not just the 5V supply pin (for example powering from the VIN pin or 3.3V pin).

If you are powering your device from no more than 5V you can place the resistor in the 5V feed and avoid the offset to the ground voltage. The idea is just the same: use an analog input to monitor the voltage drop across the resistor to calculate the current and hence the power:

<!-- 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/rnHkf.png)

<!-- End schematic -->

If you want to measure the current being drawn out of a GPIO pin, though, there is an extra step you need to consider. The GPIO has a MOSFET within it which imposes a small resistance of its own on the output. This resistance will of course cause a variation in the output voltage depending on the current drawn from the output pin. So it is important to know what the actual output voltage is rather than assuming that it would be 5V.  For that a second analog input is needed:

<!-- 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/45PkP.png)

<!-- End schematic -->

Now the voltage drop is calculated as the difference between the voltages measured by A0 and A1. Then all the other calculations are the same.


body:
I have a new project created with PlatformIO in CLion.  I've copied the _Servo-1.1.8_ library into the `include` directory.

```cpp
#include <Arduino.h>
#include "Servo-1.1.8/src/Servo.h"

Servo servo;

void setup() {
}

void loop() {
}
```

I'm getting a compilation error though:

```none
====================[ Build | Production | uno ]================================
C:\Users\USER_NAME\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\213.5744.254\bin\cmake\win\bin\cmake.exe --build C:\Users\USER_NAME\Documents\Arduino\MultiServoCallibration\cmake-build-uno --target Production
[1/1] cmd.exe /C "cd /D C:\Users\USER_NAME\Documents\Arduino\MultiServoCallibration && platformio -c clion run -euno"
FAILED: CMakeFiles/Production C:/Users/USER_NAME/Documents/Arduino/MultiServoCallibration/cmake-build-uno/CMakeFiles/Production
cmd.exe /C "cd /D C:\Users\USER_NAME\Documents\Arduino\MultiServoCallibration && platformio -c clion run -euno"
Processing uno (platform: atmelavr; board: uno; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR (3.4.0) > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr 5.1.0
 - toolchain-atmelavr 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\uno\src\main.cpp.o
Linking .pio\build\uno\firmware.elf
C:\Users\USER_NAME\AppData\Local\Temp\ccmN7v6f.ltrans0.ltrans.o: In function `_GLOBAL__sub_I_servo':
<artificial>:(.text.startup+0x98): undefined reference to `Servo::Servo()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\uno\firmware.elf] Error 1
========================== [FAILED] Took 1.18 seconds ==========================
ninja: build stopped: subcommand failed.
```

I've never used CMake before, so it might well be something super simple like adding a line to some config to include that library.

title: Add library to CLion project

Answers: 1

body:
For what it's worth, I asked a [similar question][1] on StackOverflow, on the chance that the slightly different crowd might include someone who uses CLion + PlatformIO for non-Arduino embedded work and has a more authoritative opinion on the topic.

I have no idea whether it's actually safe or a good idea, but for what it's worth, I was able to add an Arduino library to **my** project by doing the following:

1. Install PlatformIO with bundled VS Code, install CLion, then configure CLion to use PlatformIO.
2. Using CLion, create a new PlatformIO Arduino sketch. Let CLion spin its wheels, do some things that might or might not have ultimately made a difference, and make sure it's in a state where it can build and deploy successfully using CLion. I personally just called Serial.start(9600) and Serial.println("something") to output to serial, then verified it actually worked.
3. Exit CLion, and launch VS Code. Add the folder containing the sketch created in step 2 to its workspace.
4. Use VS Code to add the library. Wait for it to finish downloading, and exit VS Code.
5. Relaunch CLion, and try to use the new library.

Be warned, I have absolutely *no idea* yet whether or not this is actually kosher. I'm under the impression that when you're using PlatformIO with CLion, 100% of everything is actually managed by PlatformIO, and all CLion itself does is stub through the call to launch CMake. Assuming VS Code is the same way, this should theoretically be OK... but until someone intimately familiar with PlatformIO and the way it integrates with both CLion and VS Code reassures me that it is, I'm a little bit nervous about it.

That said... if it actually *is* safe to do, it's probably the best way to add an Arduino library to a CLion-PlatformIO project since (AFAIK) CLion itself has absolutely no comparable functionality (via the PlatformIO plugin or otherwise), and I've encountered a few libraries for SSD1306 OLED use that are a major *pain* to copy into a project by hand from their Github repo (because the OLED library has dependencies on somebody else's graphics library, which itself has dependencies on somebody else's math library and menu library, the menu library has dependencies on one or two more libraries... well, you get the idea. Pain. The exact reason things like Gradle and Maven exist for Android & Java, and CMake theoretically now exists for C(++), even though it still seems to be pretty useless in Arduino-land. ;-)

  [1]: https://stackoverflow.com/questions/70387357/how-to-add-arduino-library-to-clion-platformio-project


body:
I have an air compressor that I often give difficult tasks, where the compressor needs to run longer than it was designed for.  So it gets close to overheating, which requires periodically giving the compressor some "cool down" time, turned off.

Rather than walk into my garage every couple of minutes to turn the compressor on or off, I'd like to automate the process with a little arduino board.

The problem is the compressor is run by a 25A 12V DC line.  Most relays I can find that are Arduino compatible (3.3v DC control) max out at 10A.

I'm curious if there is a smart way to do this efficiently.  Two solutions that come to mind are:

 1) Control 3 or 4 10A relays with the Arduino, and parallelize the current heading to the compressor across multiple relays.

 2) Use one relay controlled by the Arduino, and use that relay to control a secondary big relay, that could control the 25A line.

Neither seems like a very efficient approach to the problem.  I don't know for certain but I worry that perhaps with (1), one relay might be a better conductor than the others, so perhaps one might eventually see a cascade of overheating relays.

Perhaps someone here that's more familiar with circuit design could suggest a better approach.

edit: After a little digging it looks like option (2) is the standard route most often traveled.

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

This is an OMRON relay that is powered by a 12VDC supply (0.167A), and can switch a 28VDC 30A line.   So I will double relay it, using an Adafruit featherwing to switch the 12VDC supply, which will then switch this big OMRON relay.   In my pile of old AC/DC converters I found a 1A 12V supply that's sufficient to run the OMRON relay.  Provided I haven't overlooked anything this is likely a decent solution.

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

title: Arduino controlled 25A 12V DC relay

Answers: 1

body:
Do not double relay it (at least not parallel aka Option 1).  Relays have a tendancy to bounce or take time.  This exposes you to a potential for only one relay to be connected for a time.  Worse, if someone decides to use the relays as high, low instead of closed, open it will short high to low for a time.

The series relay solution is sound but not efficient.  Relays are almost never the efficient approach (there are cases where infrequent switching occurs where a relay simply holding a position without power for long durations can be efficient).  A relay module is essentially a mosfet and a relay.  Mosfet controls the coil, and if you will have one of those, might as well go for a 12V mosfet of appropriate amperage.  I used to have some arduino compatible 3D printer heat bed mosfets that went up to 25A (on paper) so I can't imagine they're impossible to obtain.

You also mention digging power supplies for your relay.  If you are going so far as to need a 12V power supply, is there one providing for the compressor itself?  If so, could you just control the wall outlet AC that powers the supply?  Amperage would be a lot lower and AC can allow use of some rather nifty "Solid State Relay" devices (as well as relay modules, smart outlets, etc).


body:
I am doing a project using Arduino and a Rfid Mfrc522 module that consists of reading a card to unlock/lock a door. The Arduino is 3 meters far away from the Rfid Mfrc522 module and I used 7 cables to connect it, according to the schematic of the Mfrc522 Arduino library. When I previously tested the system everything was working fine but after I connect the Arduino to the module with the 3 meters cable I get this output and I can not read any card.
```
Firmware Version: = (unknown)
Scan PICC to see UID, SAK, type, and data blocks...
```

When I try to run the firmware check example for the library I get the following output
```
*****************************
MFRC522 Digital self test
*****************************
Firmware Version: = (unknown)
-----------------------------
Only known versions supported
-----------------------------
Performing test...
-----------------------------
Result: DEFECT or UNKNOWN
```

Is it possible that 3 meters away from Arduino the module is unable to communicate with it? If so what can be a possible solution to extend the "communication range" between both of them?

Thanks for the help,

Enzo

title: Rfid Mfrc522 not working using long cable

Answers: 0


body:
I'm trying to connect arduino and processing via serial. Whenever I run the processing sketch I get the following error in Processing:

```
RuntimeException: Error opening serial port COM26: Port busy
Could not run the sketch (Target VM failed to initialize).
```

Below is the arduino sketch and processing code, which I "borrowed" from [here](https://startingelectronics.org/software/processing/find-arduino-port/), to try to programmatically find the correct serial port to connect to.

Arduino:

```lang-cpp
int var;

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

void loop() {
  if (Serial.available()) {
    var = Serial.read();
  }
  Serial.println(var);
  delay(100);
}
```
Processing:

```lang-java
import processing.serial.*;

Serial ser_port;                // for serial port
PFont fnt;                      // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";

void setup() {
  size(400, 200);                         // size of application window
  background(0);                          // black background
  fnt = createFont("Arial", 16, true);    // font displayed in window

  println(Serial.list());

  // get the number of detected serial ports
  num_ports = Serial.list().length;
  // save the current list of serial ports
  port_list = new String[num_ports];
  for (int i = 0; i < num_ports; i++) {
    port_list[i] = Serial.list()[i];
  }
}

void draw()
{
  background(0);
  // display instructions to user
  textFont(fnt, 14);
  text("1. Arduino or serial device must be unplugged.", 20, 30);
  text("   (unplug device and restart this application if not)", 20, 50);
  text("2. Plug the Arduino or serial device into a USB port.", 20, 80);

  // see if Arduino or serial device was plugged in
  if ((Serial.list().length > num_ports) && !device_detected) {
    device_detected = true;
    // determine which port the device was plugged into
    boolean str_match = false;
    if (num_ports == 0) {
      detected_port = Serial.list()[0];
    } else {
      // go through the current port list
      for (int i = 0; i < Serial.list().length; i++) {
        // go through the saved port list
        for (int j = 0; j < num_ports; j++) {
          if (Serial.list()[i].equals(port_list[j])) {
            break;
          }
          if (j == (num_ports - 1)) {
            str_match = true;
            detected_port = Serial.list()[i];
          }
        }
      }
    }
  }
  // calculate and display serial port name
  if (device_detected) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    ser_port = new Serial(this, detected_port, 9600);
  }
}
```
I am a newbie at this, so not sure what I'm doing wrong. Would appreciate any help :)

title: Serial connection between arduino and processing doesn't work; Port busy

Answers: 1

body:
I tried your Processing sketch, and it does detect and try to connect to the serial port, but fails for some reason.

Processing "code" can include a `try catch` block to help you diagnose the issue. Using this modified sketch, you can print the errors to the console window.

```java
import processing.serial.*;

Serial ser_port;                // for serial port
PFont fnt;                      // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";

void setup() {
  size(400, 200);                         // size of application window
  background(0);                          // black background
  fnt = createFont("Arial", 16, true);    // font displayed in window

  println(Serial.list());

  // get the number of detected serial ports
  num_ports = Serial.list().length;
  // save the current list of serial ports
  port_list = new String[num_ports];
  for (int i = 0; i < num_ports; i++) {
    port_list[i] = Serial.list()[i];
  }
}

void draw()
{
  background(0);
  // display instructions to user
  textFont(fnt, 14);
  text("1. Arduino or serial device must be unplugged.", 20, 30);
  text("   (unplug device and restart this application if not)", 20, 50);
  text("2. Plug the Arduino or serial device into a USB port.", 20, 80);

  // see if Arduino or serial device was plugged in
  if ((Serial.list().length > num_ports) && !device_detected) {
    device_detected = true;
    // determine which port the device was plugged into
    boolean str_match = false;
    if (num_ports == 0) {
      detected_port = Serial.list()[0];
    } else {
      // go through the current port list
      for (int i = 0; i < Serial.list().length; i++) {
        // go through the saved port list
        for (int j = 0; j < num_ports; j++) {
          if (Serial.list()[i].equals(port_list[j])) {
            break;
          }
          if (j == (num_ports - 1)) {
            str_match = true;
            detected_port = Serial.list()[i];
          }
        }
      }
    }
  }
  else{
    text("else", 20, 110);
  }
  // calculate and display serial port name
  if (device_detected) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    try{
      ser_port = new Serial(this, detected_port, 9600);
    }
    catch(Exception e){
      // Print detailed error information to the console.
      System.err.println(e);
      e.printStackTrace();
    }
  }
  else{
    text("Device NOT detected:", 20, 140);
  }
}
```

These are the errors I get on my computer using a Arduino Uno.

```
 at processing.serial.Serial.<init>(Unknown Source)
 at processing.serial.Serial.<init>(Unknown Source)
 at sketch_211210a.draw(sketch_211210a.java:85)
 at processing.core.PApplet.handleDraw(PApplet.java:2475)
 at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
 at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
java.lang.RuntimeException: Error opening serial port /dev/tty.wchusbserial620: Port busy
java.lang.RuntimeException: Error opening serial port /dev/tty.wchusbserial620: Port busy
```

Another issue is with this part of the code: `if (device_detected)`. The `draw()` function is like the Arduino `loop()` function in that it keeps looping through the code. It is repeatedly trying to make a new serial connection to the same port, which MAY explain why the `Port busy` error message is repeated in the console error messages.

Using Processing IDE 3.5.3, there is a errors tab at the bottom of the console window. I am getting 2 errors there which don't seem to be involved with the port connection issue.

If you install the `controlP5` library in the Processing IDE, you can use this port testing sketch to see the Port ID change every time you create a new connection with it. I suspect your sketch may connect to the Arduino the first time through the `draw()` function, then it keeps trying to connect to the same port which is busy.

```java
// Serial port(s) connection tester. Processing version 3.5.3.
// For a list of all the ScrollableList Controller methods available,
// use: "ControlP5.printPublicMethodsFor(ScrollableList.class);"
// to print the entire list of the 165 functions to the console.
import controlP5.*;
import java.util.*;
import processing.serial.*;

ControlP5 cp5;
Serial myPort;
Textlabel outputWindowLine1, outputWindowLine2;
String portName;

void setup(){

  size(500, 431);

  // Use a font larger than the default "6px".
  ControlFont font = new ControlFont(createFont("Verdana", 14));

  // ALL CAPS is the default font for the control. Why?
  Label.setUpperCaseDefault(false);

  // Configure a controlP5 ScrollableList.
  cp5 = new ControlP5(this);
  cp5.addScrollableList("dropdown")
    .setPosition(0, 50)
    .setSize(500, 400)
    .setBarHeight(50)
    .setItemHeight(30)
    .setFont(font)
    .addItems(GetSerialPortList())
    .setType(ScrollableList.LIST)
    .setLabel("--- Click on a port name to try and connect to it ---")
    ;

  outputWindowLine1 = cp5.addTextlabel("outputWindowLine1Label")
    .setText("The list of port names is \"scrollable\".")
    .setPosition(0, 0)
    .setColorValue(0)
    .setFont(font)
    ;

  outputWindowLine2 = cp5.addTextlabel("outputWindowLine2Label")
    .setText("Press any character on the keyboard to reload the list.")
    .setPosition(0, 25)
    .setColorValue(0)
    .setFont(font)
    ;
}

void draw(){
  background(255);
}

// User selects a drop down item. Works like a JavaScript onChange()
// event, except you can click on the same item multiple times, this
// code will execute again on each mouse "click" of the same item.
void dropdown(int n){

  // Create a serial connection and send some
  // chars to the "USB to Serial Converter".
  try{

    // Get the port name.
    portName = Serial.list()[n];

    // If you don't do this, you can't re-connect to the same port again.
    // Re-connecting to the same port gives you a new "myPort ID" and may
    // take a few seconds.
    if(myPort != null){
      myPort.clear();
      myPort.stop();
    }

    // Create a Serial Port connection.
    myPort = new Serial(this, portName, 9600);

    // User message.
    outputWindowLine1.setText("Connected to portName: " + portName);
    outputWindowLine2.setText("myPort = " + myPort);

  }
  catch(Exception e){

    // User message.
    outputWindowLine1.setText("Error connecting to portName: " + portName);
    outputWindowLine2.setText("");

    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
  }
}

// Refresh the dropdown list of serial port names.
void keyPressed(){
  cp5.get(ScrollableList.class, "dropdown").setItems(GetSerialPortList());
  outputWindowLine1.setText("Serial port list refreshed!");
  outputWindowLine2.setText("");
}

// Get a List of the serial port names.
List GetSerialPortList(){
  List serialPortList = Arrays.asList(Serial.list());
  return serialPortList;
}

```


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

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

I have checked my connections and when I bring a magnet close to the hall sensors the voltage on them which is apprx. 4.5V becomes 0V and when I remove the magnet the sensor turns back to 4.5V. This means that all the hall sensors are responding to the magnets which makes my connections alright. I want to get 0 when magnet is brought closer to the sensor and 1 when taken away.

The below code shows 11111111 continuously and does not get affected even if magnet is brought close or taken away. I expected the code to show 00000000 until magnet is brought closer to any sensor. If magnet is brought closer to 1st sensor, it should have shown 10000000 and so forth.

    int load = 7;
    int clockEnablePin = 4;
    int dataIn = 5;
    int clockIn = 6;

    void setup() {
    Serial.begin(9600);
    pinMode(load, OUTPUT);
    pinMode(clockEnablePin, OUTPUT);
    pinMode(clockIn, OUTPUT);
    pinMode(dataIn, INPUT);
    digitalWrite(clockEnablePin, LOW);
    digitalWrite(clockIn, LOW);
    }

    void loop() {

    //Write pulse to load pin
    digitalWrite(load, LOW);
    digitalWrite(load, HIGH);

    //Get data from 74HC165
    byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);

    // Print to serial monitor
    Serial.print("Pin States: \r\n");
    Serial.println(incoming, BIN);
    delay(200);
    }

    uint8_t shiftIn(uint8_t dataIn, uint8_t clockIn, uint8_t bitOrder) {
    uint8_t value = 0;
    uint8_t i;
    for (i = 0; i < 8; ++i)
    {
     digitalWrite(clockIn, HIGH);
     if (bitOrder == LSBFIRST)
     {
      value |= digitalRead(dataIn) << i;
     }
     else
     {
      value |= digitalRead(dataIn) << (7 - i);
     }
     digitalWrite(clockIn, LOW);
    }
    return value;
    }

---

The code as well as the schematic is correct. The problem was with shift register. Thank you everyone for your suggestions.

title: Code to display binary value from 1 hall sensor A3144 connected with shift register 74HC165 when magnet is brought near to it or taken away?

Answers: 2

body: Preset your clockIn and clockEnablePin pins to LOW in the setup function. Do not manipulate them in the loop function. You do not need to use the delayMicroseconds() functions because the 74HC165 is a great deal faster than Arduino read/write functions.

body:
The connections are correct in the above diagram. I have had a terrible experience with non working shift register. Some 74HC165 don't work properly(might be duplicate). So, try buying from a verified store.

Use this code to get HIGH or LOW value as output. You can change them to 1 or 0 as per your requirement.

I used this code and it works perfect with 8 Hall switches connected to one shift register 74HC165. Don't forget to connect your pins with your Arduino Uno as depicted in the code or you can update your code as per your connections of shift register with Arduino Uno. You can increase the number of inputs by adding more shift register 74HC165 to the circuit & also you just need to update the value of number of shift chips in the below code.

    /* How many shift register chips are daisy-chained.
    */
    #define NUMBER_OF_SHIFT_CHIPS   1

    /* Width of data (how many ext lines).
    */
    #define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8

    /* Width of pulse to trigger the shift register to read and latch.
    */
    #define PULSE_WIDTH_USEC   5

    /* Optional delay between shift register reads.
    */
    #define POLL_DELAY_MSEC   1

    /* You will need to change the "int" to "long" If the
     * NUMBER_OF_SHIFT_CHIPS is higher than 2.
    */
    #define BYTES_VAL_T unsigned int

    int ploadPin        = 7;  // Connects to Parallel load pin the 165
    int clockEnablePin  = 4;  // Connects to Clock Enable pin the 165
    int dataPin         = 5; // Connects to the Q7 pin the 165
    int clockPin        = 6; // Connects to the Clock pin the 165

    BYTES_VAL_T pinValues;
    BYTES_VAL_T oldPinValues;

    /* This function is essentially a "shift-in" routine reading the
     * serial Data from the shift register chips and representing
     * the state of those pins in an unsigned integer (or long).
    */
    BYTES_VAL_T read_shift_regs()
    {
        long bitVal;
        BYTES_VAL_T bytesVal = 0;

        /* Trigger a parallel Load to latch the state of the data lines,
        */
        digitalWrite(clockEnablePin, HIGH);
        digitalWrite(ploadPin, LOW);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(ploadPin, HIGH);
        digitalWrite(clockEnablePin, LOW);

        /* Loop to read each bit value from the serial out line
         * of the SN74HC165N.
        */
        for(int i = 0; i < DATA_WIDTH; i++)
        {
            bitVal = digitalRead(dataPin);

            /* Set the corresponding bit in bytesVal.
            */
            bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

            /* Pulse the Clock (rising edge shifts the next bit).
            */
            digitalWrite(clockPin, HIGH);
            delayMicroseconds(PULSE_WIDTH_USEC);
            digitalWrite(clockPin, LOW);
        }

        return(bytesVal);
    }

    /* Dump the list of zones along with their current status.
    */
    void display_pin_values()
    {
        Serial.print("Pin States:\r\n");

        for(int i = 0; i < DATA_WIDTH; i++)
        {
            Serial.print("  Pin-");
            Serial.print(i);
            Serial.print(": ");

            if((pinValues >> i) & 1)
                Serial.print("HIGH");
            else
                Serial.print("LOW");

            Serial.print("\r\n");
        }

        Serial.print("\r\n");
    }

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

        /* Initialize our digital pins...
        */
        pinMode(ploadPin, OUTPUT);
        pinMode(clockEnablePin, OUTPUT);
        pinMode(clockPin, OUTPUT);
        pinMode(dataPin, INPUT);

        digitalWrite(clockPin, LOW);
        digitalWrite(ploadPin, HIGH);

        /* Read in and display the pin states at startup.
        */
        pinValues = read_shift_regs();
        display_pin_values();
        oldPinValues = pinValues;
    }

    void loop()
    {
        /* Read the state of all zones.
        */
        pinValues = read_shift_regs();

        /* If there was a chage in state, display which ones changed.
        */
        if(pinValues != oldPinValues)
        {
            Serial.print("*Pin value change detected*\r\n");
            display_pin_values();
            oldPinValues = pinValues;
        }

        delay(POLL_DELAY_MSEC);
    }


body:
[![enter image description here][1]][1]I just started working with Arduinos on Tinkercad in a school assignment and have non preexisting electrical knowledge. Tried to read up on the subject and I think I've gotten some of the facts straight but i still can't get them to communicate when using the optocoupler. It works fine with the code if i connect them to each other directly with the tx/rx pins.
```
//Sender code
char string[5] = {'h', 'e', 'l', 'l', 'o'};
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  Serial.write(string,5);
  delay(1000);
}
```
```
//Reciving arduino code

char text[10];

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

void loop()
{
  Serial.readBytes(text,5);
  Serial.println(text);
  delay(1000);
}
```
My guess is I've done something wrong connecting the receiving Arduino but i can't find whats wrong. Any feedback is immensely appreciated!

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

title: Serial communication with 2 Arduinos and one optocoupler

Answers: 2

body:
Here is a diagram that I would suggest you to to try it out. The transmitting Arduino pin will turn the internal infrared LED on or off depend on the whether the pin is at LOW or HIGH. When the infrared LED is on, it will turn on the phototransistor, and produce a LOW at the receiving arduino pin.

The LED is optional but it gives you a visual clue on whether you are receiving the data or not.

<!-- 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/WxIkN.png)

<!-- End schematic -->

body:
EDIT: Even though the OP may be long gone, I wanted to finish what I started with my response.
[![enter image description here][1]][1]

This circuit works fine for me using two UNOs (and one UNO and a serial board for a PC). It is, essentially, the same as in the first response by @hcheung. I used 4.7K and 10K for R2/R4 and they worked fine. I tested it using this program running on both UNOs.

        #include <SoftwareSerial.h>

    SoftwareSerial SSerial(4,5); // RX, TX

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

    }

    void loop() {
      // display whatever comes in through software serial on the Arduino monitor
      if(SSerial.available()) {
        Serial.write(SSerial.read());
      }
      // send whatever comes in on serial to software serial
      if(Serial.available()) {
        SSerial.write(Serial.read());
      }
    }

Note that the programs use software serial and both Tx and Rx are isolated.

If you use this for the hardware serial port on two UNOs, take care. First, don't hang things on 0/1 until after the program loads and how much you can get away with is unclear to me.

Second, if you use a simple program like this, on both UNOs, you will quickly see that the first bytes sent will ping-pong and causes problems.

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

    void loop() {
      if(Serial.available()){
        Serial.write(Serial.read());
      }
    }

I note that the OP was only using one end.

Personally, I found several junk box 4N25s that would not work - that took me a bit of time to figure out. Finally, I don't see the point of optically isolating two UNOs as I did for example, but that is another story.

______ older msg is below _______________________________

A typical way of doing this is illustrated here .
[![enter image description here][2]][2]

// *** I am leaving my original text below, but I want to back off of this a bit. I spent a few hours on this today and I am not happy with the results or my understanding. If I come to any worthwhile conclusions, I will post them. I do like getting to the bottom of things, but I was premature. It is simply not working the way I would expect when I get into 9600 speed and I need to investigate further. Yes, I am accounting for the UNO 0&1 involvement with USB.

// ***

The advantage is that you will not be inverting the line on the receiving end. The choice of R1 and R2 is important. I used R1=330 and R2=1K and transmission was fine at 9600 using a junk box 4N25. My testing was with a single UNO and after uploading a simple program.

See https://github.com/pepaslabs/Electronics/wiki/Designing-a-TTL-Serial-Opto-isolator for a good discussion on exactly this topic.

PS: As mentioned you should have submitted a schematic and you could have used a meter to see what voltages you were getting - those are good steps to understanding things.

  [1]: https://i.stack.imgur.com/vjQ5K.jpg
  [2]: https://i.stack.imgur.com/62BiQ.jpg


body:
I'm making a sort of state machine with 3 states, and I'm actually doing some tests by configuring at the start of my code state=3. I've wired on each pin 2 leds, for a total of 12 leds. I'm using elapsedMillis library (https://github.com/pfeerick/elapsedMillis)  to make things a bit more easy and I'm stuck at 3rd state. At the 3rd state I would like that each couple of leds will stay on for 90ms, than goes of for 1ms. elapsedMillis library allows one to do something like that:

elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs

    #include "elapsedMillis.h"

    const int pin0 = 12;
    const int pin1 = 11;
    const int pin2 = 10;
    const int pin3 = 9;
    const int pin4 = 8;
    const int pin5 = 7;

    unsigned long t1 = 500;
    unsigned long waiting = 1000;
    unsigned long t2 = 100;
    unsigned long t3 = 50;
    unsigned long t4 = 1;
    unsigned long t5 = 90;
    unsigned long startTime = 0;

    int ledArray[] = {pin0, pin1, pin2, pin3, pin4, pin5};
    int j, k, a, c = 0;
    int state = 3;

    elapsedMillis elapsedTime;
    elapsedMillis timer1;
    elapsedMillis timer2;
    elapsedMillis timer3;
    elapsedMillis timer4;
    elapsedMillis timer5;

    void setup() {
      pinMode(pin0, OUTPUT);
      pinMode(pin1, OUTPUT);
      pinMode(pin2, OUTPUT);
      pinMode(pin3, OUTPUT);
      pinMode(pin4, OUTPUT);
      pinMode(pin5, OUTPUT);
      Serial.begin(9600);

    }

    void loop() {

      switch (state) {

        case 0:
          // led accesi in modo scalare allo stato 0
          while (k <= 5)
          {

            if (timer1 >= t1)
            {
              if (j <= 5)
              {
                digitalWrite(ledArray[j], HIGH);
                j++;
                timer1 = 0;

              }
              k++;
              elapsedTime = 0;
            }

          }
          // attendo 1s poi vado allo stato 1

          if (elapsedTime >= waiting) {
            state = 1;
            elapsedTime = 0;
          }

          break;

        case 1:

          // led spenti rapidamente in modo scalare

          while (k >= 0) {
            if (timer2 >= t2)
            {
              if (j >= 0)
              {
                digitalWrite(ledArray[j], LOW);
                j--;
                timer2 = 0;
              }
              k--;
              elapsedTime = 0;
            }
          }
          // attendo 0.1s
          if (elapsedTime >= t2) {
            state = 2;

          }
          break;

        case 2:

          // led fade per 3 volte
          if (c <= 3)
          {
            while (a <= 255)
            {
              if (timer3 >= t3) {
                analogWrite(ledArray[3], a);
                timer3 = 0;
                a += 10;

              }

            }
            while (a >= 0)
            {
              if (timer3 >= t3) {
                analogWrite(ledArray[3], a);
                timer3 = 0;
                a -= 10;
              }
            }
            c++;
          }
          else {
            state = 3;
          }

          break;

        case 3:

          if (timer4 >= t5) {
            if (j <= 5) {
              digitalWrite(ledArray[j], HIGH);
              j++;
              timer4 = 0;
              if (timer5 >= t4) {
                  digitalWrite(ledArray[j], LOW);
                  timer5 = 0;
                }
              }
            }
          break;
      }
    }

---

    case 3:

      for (int ii = 0; ii <= 5; ii++)
      {
        digitalWrite(ledArray[ii], ( (ii == j) && (timer4 < 90)));
      }

      if (timer4 > 91) {
        j += backwardScan ? -1 : 1 ;
        if (j > 5) {
          backwardScan = 1;
          j = 5;
        }
        if (j < 0) {
          backwardScan = 0;
          j = 0;
        }
        timer4 = 0;
      }
      break;

For the state3 I would like an effect like the "KITT" knight rider car in 80s TV show. Each couple of 2 leds should move back and forth... in shortly: first 2 leds stays on 90ms and 1ms off, then second 2 leds on 90ms and 1ms off.. etc...

It seems like the leds in the array goes on all together.

title: Question about ElapsedMillis() library and state machines

Answers: 1

body:
You need to maintain more state information to have 'case 3' work like a KITT-style back and forth LED scanner. I think you can do it with three state variables, `j` for which pair of LEDs is active, `backwardsScan` to say which way to go, and `timer4` to remember where in the 91ms cycle we are with the active pair of LEDs.

Try something like this:

    ...
    int backwardsScan = 0;
    ...

    case 3: // KITT-like scanning back and forth 90us/1us
      // j remembers which LED pair is active
      // backwardsScan remembers which direction to scan
      // timer4 handles the 90ms/1ms on-off cycle.
      //
      // reset output leds appropriately for (j,timer4):
      for(int ii=0;ii<=5;ii++){
        digitalWrite(ledArray[ii],( (ii==j) && (timer4 <90)));
      }
      // update KITT state based on (j,backwardsScan,timer4)
      if(timer4 > 91){
         j += backwardsScan? -1 : 1 ;
         if (j > 5){
            backwardsScan = 1;
            j = 5;
            }
         if (j < 0) {
            backwardsScan = 0;
            j = 0;
            }
         timer4 = 0;
         }

      break;

The main trick with state machines is being able to fully determine the complete state from what you are keeping track of--It should be memoryless, in that the state variable(s) fully specify the state, and you don't need to remember the prior state.  If you find you need to know more than one thing, you might need a vector or set of state variables.  One could map the whole led-backwardsScan-timer4 state vector into a single state variable (timer4, modulo 182ms for example,) but that would be unnecessarily complicated. Recognizing all the variables required to fully specify the state is important.


body:
I'm trying to connect an Arduino Nano Every and Processing via serial. I'm getting no errors on either side, and the arduino serial functionality works otherwise on the arduino IDE.

I'm connecting on the right port, and the port is busy while the processing code is running (as you would expect), however, the variable inside the arduino is not updating. In fact, the whole if serial.available statement is not executing.

Arduino:

```lang-cpp
int var;

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

void loop() {
  if (Serial.available()) {
    var = Serial.read();
  }
  Serial.println(var);
  delay(100);
}
```
Processing:

```lang-java
import processing.serial.*;

// The serial port
Serial myPort;

void setup() {
  // List all the available serial ports:
  printArray(Serial.list());
  // open port
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  try {
    myPort.write(1);
  }
  catch(Exception e) {
    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
  }
  delay(50);
}
```

PS. Asked moderator to close previous post on topic.

title: Serial connection between Processing and Arduino doesn't work

Answers: 2

body:
Most Arduinos (and the Nano Every is one of them) will reset when the serial connection is reopened (You close it by closing the processing sketch and open it again with the Serial Monitor). Variables are stored in RAM, which gets wiped and reinitialized after a reset or power loss.

So this basically happens: You send the data from processing over serial to the Arduino. The Arduino puts the received value into the variable in its RAM. Then you close the processing sketch, effectively closing the serial connection. Then you open the Serial Monitor, which opens the serial connection again. This triggers the reset of the Arduino and wipes the previous variable value from the RAM. You will only ever see the initial variable value (zero) this way.

You can prevent a reset over serial if you connect a capacitor between the Reset pin and ground.

Though I would say this is probably not what you really should do. Instead just read the data from serial inside your processing sketch. After you have read it you can print it.

---

As a side note: The reset-on-serial-reconnect functionality is used for programming the Arduino over serial (through the USB cable). The new program is written into the program memory (flash) by the bootloader. The bootloader runs directly after a reset/power-on. So to upload a new program to the Arduino the Arduino IDE has to first reset the Arduino to talk to the bootloader.

body:
If you open your Processing sketch and send the data to the Arduino without opening the serial monitor in the Arduino IDE, the data gets through. Here is your Arduino sketch which is slightly modified. Disclaimer - tested on an Uno.

There are other options for serial monitors which may work (varies by OS), as well as other options to interface between Processing and Arduino, such as [Firmata][1]

```c++
int var;

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
}

void loop() {
  if (Serial.available()) {
    var = Serial.read();
    digitalWrite(2, HIGH);
    delay(50);
    digitalWrite(2, LOW);
  }
  Serial.println(var);
  delay(100);
}
```

**EDIT**

Arduino IDE version 1.0.6.2 with `Processing` 2.2-ish would let you open the serial monitor in the Arduino IDE and "monitor" the data. Since you can't do this anymore, what about making your `Processing` app open 2 windows. One window could be a home brewed serial monitor, and the other the sketch you are working on. Using `Processing` version 3.5.3, it is only [15 lines of code][2] to open up 2 windows. I'm sure the actual implementation of such a system would be a good challenge.

  [1]: https://www.arduino.cc/en/reference/firmata
  [2]: https://stackoverflow.com/a/32992545/10292145


body:
Hi I'm trying to get the RFM69 rf module to work on an arduino uno.

Here is my code:

```#include <RFM69.h>
#include <SPI.h>
// Addresses for this node. CHANGE THESE FOR EACH NODE!
#define NETWORKID     0   // Must be the same for all nodes (0 to 255)
#define MYNODEID      2   // My node ID (0 to 255)
#define TONODEID      1   // Destination node ID (0 to 254, 255 = broadcast)
// RFM69 frequency, uncomment the frequency of your module:
//#define FREQUENCY   RF69_433MHZ
#define FREQUENCY     RF69_915MHZ

// Create a library object for our RFM69HCW module:
RFM69 radio;

void setup()
{
  // Open a serial port so we can send keystrokes to the module:
  Serial.begin(9600);
  Serial.print("Node ");
  Serial.print(MYNODEID,DEC);
  Serial.println(" ready");

   if(radio.initialize(FREQUENCY, MYNODEID, NETWORKID))
     Serial.println("Initialized Successful");

  radio.setHighPower(); // Always use this for RFM69HCW
}

void loop()
{
     bool test = radio.sendWithRetry(TONODEID, "Hi", 2);
    Serial.println(test);
}
```
which outputs

```
Node 2 Ready
Initialization Successful
Node 2 Ready
Initialization Successful
```
which means that the sendWithRetry function is not returning a bool as expected, but I can't figure out why.
I am using the code given in the github
https://github.com/sparkfun/RFM69HCW_Breakout/blob/master/Libraries/Arduino/RFM69/RFM69.cpp which shows the nitty gritty of the RF69 Module and its arduino code.

I hooked up the hardware according to
https://learn.sparkfun.com/tutorials/rfm69hcw-hookup-guide/all
and checked all the wiring, so the I concluded that the problem was software based.

Any ideas why this isn't working?

title: RFM69 Sending Failure

Answers: 0


body:
I bought a hm19 (link 2), 2 hc05s, an Arduino micro, and an Arduino due. I'm trying to turn an led on and off using my phone over Bluetooth. I verified that all modules work by themselves. So here's my issue.

First is that the Hc05 (link 1 below) modules simply do not respond to any AT command at all but work for serial communication. I have tried shorting a pin to send it into AT mode but that just made it impossible for me to communicate with it since I cannot get any input on my desktop using an USB cable from the Bluetooth module which brings me to the second issue.

Second is that I can send inputs from the serial monitor from my desktop to my phone but i cannot send data from my phone to my desktop. I have tracked this issue down to being in between the Bluetooth module and the Arduino. I have strong reasons to believe that DSD tech (hm19) have mislabled their bluetooth module because plugging rx into rx, tx to tx, gnd to gnd, and vcc to 5v works while flipping the rx and tx pins gives me a weird format.

Is there anyway I can fix the second issue preferably so i can diagnose the first one myself?

Link 1: https://www.amazon.com/gp/product/B07C1PXM44/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

Link 2: https://www.amazon.com/gp/product/B07MBLVHH8/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1

Connecting RX to TX and TX to RX
[![enter image description here][1]][1]
Connecting RX to RX and TX to TX
[![enter image description here][2]][2]

All inputs were "1" and were sent from my desktop to my phone
here's the code run using an Arduino DUE with the TX0 and RX0 pins

    char inputByte;
    void setup() {
     Serial.begin(9600);
     pinMode(13,OUTPUT);

    }

    void loop() {
    while(Serial.available()>0){
      inputByte= Serial.read();
      Serial.println(inputByte);
      if (inputByte=='Z'){
      digitalWrite(13,HIGH);
      }
      else if (inputByte=='z'){
      digitalWrite(13,LOW);
      }
      }
    }

  [1]: https://i.stack.imgur.com/APxk9.jpg
  [2]: https://i.stack.imgur.com/Cag1p.jpg

title: Issues with the TX pins for recieving data from the HM19 and HC05

Answers: 0


body:
In my project I have to control a stepper motor using arduino and microstep driver, can I control the motor so that it can move 90 degrees then stop for like a second and then move again and repeat that sequence.
Can I do that please help I'm stuck in the code and I couldn't figure it out.

title: Stepper motor with microstep driver

Answers: 1

body:
Of course you can! There are some excellent libraries to control steppers, e.g. [Stepper](https://www.arduino.cc/en/Reference/Stepper) or [AccelStepper](http://www.airspayce.com/mikem/arduino/AccelStepper/).

What you describe sounds like basic task that is covered in examples of these libraries. For example, this one: https://github.com/arduino-libraries/Stepper/blob/master/examples/stepper_oneRevolution/stepper_oneRevolution.ino. You only need to set correct pins and tweak delays and rotation angles (if your stepper has 200 steps per revolution, then 90 degrees is 50 steps).