text:
How to find the MAC address using NodeMCU when connecting to WiFi?
I am new to using NodeMCU and IOT. I am testing the NodeMCU (ESP8266) so that I can connect with pubnub server. Have taken code from github but there is error saying
'mac was not declared in this scope'

and some other error too.
Can anyone tell whats the problem?
This code is from pubnub github page - https://github.com/pubnub/arduino
Code:-
#include <Ethernet.h>

#include <PubNub.h>

void setup() {
    /* For debugging, set to speed of your choice */
    Serial.begin(9600);

    /* If you use some other HW, you need to do some other
       initialization of it here... */
    Ethernet.begin();

    /* Start the Pubnub library by giving it a publish and subscribe
       keys */
    PubNub.begin(publisher key, subscriber key);
}

void loop() {
    /* Maintain DHCP lease. For other HW, you may need to do
       something else here, or maybe nothing at all. */
    Ethernet.maintain();

    /* Publish message. You could use `auto` here... */
    PubNonSubClient *pclient = PubNub.publish(pubchannel, "\"message\"");
    if (!pclient) return;
    PublishCracker cheez;
    cheez.read_and_parse(pclient);
    /** You're mostly interested in `outcome()`, and,
        if it's "failed", then `description()`.
    */
    Serial.print("Outcome: "); Serial.print(cheez.outcome());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.outcome()));
    Serial.print("description: "); Serial.println(cheez.description());
    Serial.print("timestamp: "); Serial.println(cheez.timestamp());
    Serial.print("state: "); Serial.print(cheez.state());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.state()));
    pclient->stop();

    /* Wait for news. */
    PubSubClient *sclient = PubNub.subscribe(subchannel);
    if (!sclient) return; // error
    String msg;
    SubscribeCracker ritz(sclient);
    while (!ritz.finished()) {
        ritz.get(msg);
        if (msg.length() > 0) {
            Serial.print("Received: "); Serial.println(msg);
        }
    }
    sclient->stop();

    delay(1000);
}

error :-
  sketch_jan17a:28:47: error: 'pubchannel' was not declared in this scope

     PubNonSubClient *pclient = PubNub.publish(pubchannel, "\"message\"");

                                               ^

sketch_jan17a:44:46: error: 'subchannel' was not declared in this scope

     PubSubClient *sclient = PubNub.subscribe(subchannel);

                                              ^

Multiple libraries were found for "Ethernet.h"
 Used: C:\Users\sultan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\libraries\Ethernet
 Not used: C:\Program Files (x86)\Arduino\libraries\Ethernet
exit status 1
'mac' was not declared in this scope

I don't believe you have an Wiznet Ethernet module connected to your NodeMcu. Ethernet library is for the Wiznet chips for wired LAN. You want to use the WiFi of the NodeMcu. For that the esp8266 core has the ESP8266WiFi library. It has the same API as other WiFi libraries based on Arduino WiFi library. So you can modify the WiFi examples of the Arduino pubnub library. Only change #include <WiFi101.h> to #include <ESP8266WiFi.h>. (And remove #include <SPI.h>.)

yes no error after doing it. But now it says "Error compiling for board NodeMCU 1.0 (ESP-12E Module)."  Mine is node mcu 2 but can;t find it in the board setting. what to do?

if it is ""Error compiling..." then there is some compilation error

no worries, got it


text:
Error compiling a sketch to class/library conversion
I'm trying to take a sketch and turn it into a class for a library I'm making, but I'm getting compile issues.
This is not a duplicate as suggested. as i explain in the comment and my answer. Thank you all for your help - as I am new to creating libraries - figured I would attempt something correctly for a change :)
Here is the original sketch: Gotten here
#include <ros.h>
#include <sensor_msgs/BatteryState.h>

#define K           0.00472199
#define CELLS       6
#define MAX_CELLS   12

double cell_const[MAX_CELLS] =
{
  1.0000,
  2.1915,
  2.6970,
  4.1111,
  4.7333,
  6.6000,
  6.6000,
  7.8293,
  8.4667,
  9.2353,
  11.0000,
  11.0000
};

ros::NodeHandle nh;
sensor_msgs::BatteryState batt_state;
ros::Publisher batteryState("battery_state", &batt_state);

void setup()
{
  // Initialize the ROS node.
  nh.initNode();
  nh.advertise(batteryState);

  // Populate battery parameters.
  batt_state.design_capacity = 2200;      // mAh
  batt_state.power_supply_status = 2;     // discharging
  batt_state.power_supply_health = 0;     // unknown
  batt_state.power_supply_technology = 3; // LiPo
  batt_state.present = 1;                 // battery present

  batt_state.location = "Crawler";        // unit location
  batt_state.serial_number = "ABC_0001";  // unit serial number
  batt_state.cell_voltage = new float[CELLS];

}

void loop()
{
  // Battery status.
  double battVoltage = 0.0;
  double prevVoltage = 0.0;

  // Populate battery state message.
  for (int i = 0; i < CELLS; i++)
  {
     // Read raw voltage from analog pin.
    double cellVoltage = analogRead(i) * K;

    // Scale reading to full voltage.
    cellVoltage *= cell_const[i];
    double tmp = cellVoltage;

    // Isolate current cell voltage.
    cellVoltage -= prevVoltage;
    battVoltage += cellVoltage;
    prevVoltage = tmp;

    // Set current cell voltage to message.
    batt_state.cell_voltage[i] = (float)cellVoltage;

    // Check if battery is attached.
    if (batt_state.cell_voltage[i] >= 2.0)
      batt_state.present = 1;
    else
      batt_state.present = 0;
  }

  // Update battery health.
  batt_state.voltage = (float)battVoltage;

  if (batt_state.voltage > CELLS * 4.2)
    batt_state.power_supply_health = 4; // overvoltage
  else if (batt_state.voltage < CELLS * 3.0)
    batt_state.power_supply_health = 3; // dead
  else
    batt_state.power_supply_health = 1; // good

  // Publish data to ROSSERIAL.
  batteryState.publish( &batt_state );
  nh.spinOnce();
  delay(1000);

}

Here is my header file:
#ifndef battery_h
#define battery_h

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include <ros.h>
#include <sensor_msgs/BatteryState.h>

#define K           0.00472199
#define CELLS       6
#define MAX_CELLS   12
#define CRITICAL    0.30

double cell_const[MAX_CELLS] =
{
  1.0000, 2.1915, 2.6970, 4.1111,
  4.7333, 6.6000, 6.6000, 7.8293,
  8.4667, 9.2353, 11.0000, 11.0000
};

ros::NodeHandle nh;
sensor_msgs::BatteryState batt_state;

ros::Publisher batteryState("/battery/info", &batt_state);

class Battery
{
  private:
   void updateBatteryHealth();
   double battVoltage;
   double prevVoltage;
  public:
    void begin();
    void updateBatteryState();
};

#endif

This is my cpp file:
#include "battery.h"

void Battery::begin(){

  // Launch ROS node and set parameters.
  nh.initNode();

  // Setup publishers/subscribers.
  nh.advertise(batteryState);

  // Populate battery parameters.
  batt_state.design_capacity          = 2200;  // mAh
  batt_state.power_supply_status      = 2;     // discharging
  batt_state.power_supply_health      = 0;     // unknown
  batt_state.power_supply_technology  = 3;     // LiPo
  batt_state.present                  = 1;     // battery present

  batt_state.location      = "Crawler";        // unit location
  batt_state.serial_number = "ABC_0001";       // unit serial number

  batt_state.cell_voltage = new float[CELLS];  // individual cell health

}

void Battery::updateBatteryState() {
  battVoltage = 0.0;
  prevVoltage = 0.0;
  // Reset Power Supply Health.
  batt_state.power_supply_health = 0;
    // Populate battery state message.
  for (int i = 0; i < CELLS; i++)
  {
    // Read raw voltage from analog pin.
    double cellVoltage = analogRead(i) * K;

    // Scale reading to full voltage.
    cellVoltage *= cell_const[i];
    double tmp = cellVoltage;

    // Isolate current cell voltage.
    cellVoltage -= prevVoltage;
    battVoltage += cellVoltage;
    prevVoltage = tmp;

    // Set current cell voltage to message.
    batt_state.cell_voltage[i] = (float)cellVoltage;

    // Check if battery is attached.
    if (batt_state.cell_voltage[i] >= 2.0)
    {
      if (batt_state.cell_voltage[i] <= 3.2)
        batt_state.power_supply_health = 5; // Unspecified failure.
      batt_state.present = 1;
    }
    else{
      batt_state.present = 0;
    }
  }
};

void Battery::updateBatteryHealth() {
    // Update battery health.
  if (batt_state.present)
  {
    batt_state.voltage = (float)battVoltage;
    float volt = batt_state.voltage;
    float low  = 3.0 * CELLS;
    float high = 4.2 * CELLS;
    batt_state.percentage = constrain((volt - low) / (high - low), 0.0, 1.0);
  }
  else
  {
    batt_state.voltage = 0.0;
    batt_state.percentage = 0.0;
  }

  // Update power supply health if not failed.
  if (batt_state.power_supply_health == 0 && batt_state.present)
  {
    if (batt_state.voltage > CELLS * 4.2)
      batt_state.power_supply_health = 4; // overvoltage
    else if (batt_state.voltage < CELLS * 3.0)
      batt_state.power_supply_health = 3; // dead
    else
      batt_state.power_supply_health = 1; // good
  }
   // Publish data to ROSSERIAL.
  batteryState.publish( &batt_state );
  nh.spinOnce();
};

this is the error im getting simply by calling the Battery::begin function in my main class:
libraries/KBROSRobot/KBROSRobot.cpp.o (symbol from plugin): In function `KBROSRobot::KBROSRobot()':
(.text+0x0): multiple definition of `nh'
sketch/rob_test.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries/KBROSRobot/KBROSRobot.cpp.o (symbol from plugin): In function `KBROSRobot::KBROSRobot()':
(.text+0x0): multiple definition of `batt_state'
sketch/rob_test.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries/KBROSRobot/KBROSRobot.cpp.o (symbol from plugin): In function `KBROSRobot::KBROSRobot()':
(.text+0x0): multiple definition of `batteryState'
sketch/rob_test.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries/KBROSRobot/KBROSRobot.cpp.o (symbol from plugin): In function `KBROSRobot::KBROSRobot()':
(.text+0x0): multiple definition of `cell_const'
sketch/rob_test.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
/var/folders/m4/lq13_j5j23qc0l2wp8dqmt4h0000gp/T//cczNyzqC.ltrans1.ltrans.o: In function `begin':
/Users/keith.brown/Documents/Arduino/libraries/KBROSRobot/KBROSRobot.cpp:9: undefined reference to `Battery::begin()'
collect2: error: ld returned 1 exit status
exit status 1

Possible duplicate of Multiple definition error when linking firmware.elf

@KIIV, how is this a duplicate of that? that was a mess with header guards and file extensions. here the linker can't find a function which exists in the source code. please explain if you see something I don't see

@Juraj In the answer I tried to cover how to declare something in the header and define it in the source file so it won't results into multiple definitions.

@KIIV, sorry, yes, but the answer by tttapa covers that too. but the last error is perhaps something else undefined reference to Battery::begin()

@Juraj True true. Undefined Battery::begin is weird. I can't see anything in the question that explains why it's failing

@Juraj and KIIV check below to my answer. I had each class in named folders /sensors/battery... drive/motors... /utility/statusLights... each other class had to be placed in the utility folder... so the Battery class wasn't compiling.

Don't define your global variables in the header file. Only declare them, using external linkage. Then define them in the implementation file
Header (.h):
extern ros::NodeHandle nh;

Implementation (.cpp):
ros::NodeHandle nh;

Try to keep usage of global variables to a minimum. If you don't need them outside of the library, declare them in the implementation file and mark them static.
Or even better: encapsulate them inside of the class.

The reason that you are getting these errors is because each implementation file is compiled separately. You have two implementation files that include your header file, so the header file is compiled twice. This results in two global variables with the name nh. During the linking stage, the linker tries to combine the compiled version of both KBROSRobot.cpp and rob_test.ino.cpp (these are called object files, .o). The linker has no way of knowing which variable is which, because they are both defined twice, so it throws an error.
Multiple declarations are fine, because it just tells the compiler "there is a definition somewhere", and the linker will find that definition eventually. That's why the declaration with external linkage solves your problem.
Multiple definitions are not allowed (in most cases).

i will give the above a try - do you think i converted the sketch ok? should i do something else differently?

@rcpilotp51 You have to do the same for all definitions in the header file. I haven't looked at the rest of your code in detail.

im getting an error about the extern: warning: 'batteryState' initialized and declared 'extern'  extern ros::Publisher batteryState("/battery/info", &batt_state);

@rcpilotp51 remove the initializer in the header file (i.e. just use extern ros::Publisher batteryState;). Do add the initializer in the implementation file.

ok got rid of the multiple errors, niw just left with : var/folders/m4/lq13_j5j23qc0l2wp8dqmt4h0000gp/T//ccB5PTqK.ltrans0.ltrans.o: In function begin':
/Users/keith.brown/Documents/Arduino/libraries/KBROSRobot/KBROSRobot.cpp:9: undefined reference to Battery::begin()' collect2: error: ld returned 1 exit status exit status 1

if the above variable are 'extern' should i jkust define them in the private var section? @tttapa

@rcpilotp51 You didn't define void Battery::begin() in your implementation file. No, class and member variables should not be marked extern.

its defined in the Battery class .h and implemented as Battery::begin() in the cpp  file void begin(); in the header.  @tttapa

@rcpilotp51 The compiler doesn't seem to agree. Check your semicolons and brackets. Also keep in mind that it uses the version in your libraries folder.

I'm editing directly in the libraries folder. as far as i can tell its there.... any other suggestions? @tttapa

I also found that all other classes in the library have to be in the utility folder or the wont compile. Answering my comment above. Thank you @tttapa for your help.
Now I just have to intelligently program the robot, which is unfortunately subjective... ;)

library folder structure specs: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification

source: Stack Exchange

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

sort: votes


text:
Short battery life concern with Arduino Pro Mini
I am using an Arduino Pro Mini with a L293D H-bridge driver to drive a bi-directional motor. The system takes input from a capacitive sensor which is constantly looking for a trigger. I don't have any low power optimization in the code. I was using Arduino UNO before which lasted for one day with a 9V battery. I switched to Arduino Pro Mini for longer battery life but the battery lasted for one day in that case as well.
Can anyone please help me understand what could be the possible reason for higher power consumption than normal for Arduino Pro Mini?
Thanks in advance for your help!
Niloy

Are you powering the motor and driver from the 9V battery too? Does the Pro Mini still have it's power LED soldered on?

Please provide entire circuit schema and source code. I can't help you without knowing more details.

a 9V battery consists of six AAAA cells .... that is way less power than AA cells

The battery life was the same (1 day).  So why did you say "higher power consumption"?  2) Arduino processors usually run at 3.3V or 5V.  So there is a energy eating linear power regulator wasting power by eating the voltage difference on most Arduino boards.  3) Those square 9 volt batteries do not contain many mA/hours.  4) Extending the life of portable battery operated devices is not an easy task.  It gets harder and harder for each additional morsel of time.

@jose can u c  Yes, I am powering the motor and driver from the 9V battery too. Yes, the Pro Mini have a power LED which is on all the time.

@Filip Franik I believe I understood the problem. Thank you so much for offering your help. :)

@jsotola thanks for the information. I did not know specifics of 9V battery.

@st2000 I meant that I expected Arduino Pro Mini to last way longer than Arduino UNO with similar setup. Thanks for pointing out about the linear regulator and low mA/h of a 9V battery. I started researching using those references and I realized how my 9V battery was draining so quickly. Thanks a lot again.

A standard alkaline 9V battery has a capacity rating of around 500mAh (milliAmp-hours). That rating is probably based on a current consumption of around 10mA. However, if the current consumption is higher, the apparent capacity drops. At 100mA, most 9V batteries have a capacity rating of about 300mAh, and at 500mA current draw, the apparent capacity drops down to 170mAh.
This means, if you are drawing 100mA of current constantly, a 9V battery will last (0.3Ah / 0.1A) = 3h.
A typical Arduino pro mini will draw current at around 20mA.
500mAh / 20mA = 20h

This is not even counting the motor and driver.
A standard alkaline AA battery has a nominal voltage of 1.5V and a capacity of about 2000mAh, and 3 of them will provide a voltage between 4.5V (fully charged 1.5V x 3) and 2.7V (fully discharged 0.9V x 3)
With that voltage range, you could operate the Arduino pro mini in 3.3V mode, with no linear regulator, at a reduced clock frequency, such as 8Mhz, which will reduce power consumption even more.
Being able to put the chip in power-saving mode, and periodically check for necessary activity will help even more.
See http://www.gammon.com.au/power for a description of many practical methods.
That said, the Arduino is going to be a pretty small contributor to power draw compared to a DC motor, so your overall run time will heavily depend on how often the motor is turned on.

Thank you so so much! I am new to Arduino Programming and little experience about batteries. Your response is very helpful to me. I will switch to AA batteries and I will follow other practical methods.

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


text:
Issue with analog readings on Arduino atMega with YL-69 moisture sensors
I've connected four YL-69 sensors to my arduino at-mega2560 on analogs pins 0-3. This is combined with four DHT11 sensors that are in digital pins.
The DHT work fine, and I can work one YL-69 moisture sensor that gives correct readings.
But when I connected the others, the readings are just 100% for all. I'm sure I must have made a mistake in my code somewhere, just can't spot it.
I've posted the sketch I use below, and the results from serial monitor.
(BTW i added a short delay of 10ms between each reading thinking that may be the issue, but it had no effect).
Serial Monitor:

Sketch:
#include "DHT.h"
#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library

File dataFile;

#define DHTPIN0 10     // what pin we're connected to
#define DHTPIN1 11     // what pin we're connected to
#define DHTPIN2 12     // what pin we're connected to
#define DHTPIN3 13     // what pin we're connected to

// Uncomment whatever type you're using!

#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// How to connect sensors
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht0(DHTPIN0, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);

int output_value ;
int output_value1 ;
int output_value2 ;
int output_value3 ;
int output_value4 ;

unsigned long time;

void setup() {
 Serial.begin(9600);
 while (!Serial)
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
 Serial.println("DHTxx test!");
 dht0.begin();
 dht1.begin();
 dht2.begin();
 dht3.begin();
 delay(2000);
}

uint16_t line = 1;

void loop() {
 // Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h0 = dht0.readHumidity();
 float h1 = dht1.readHumidity();
 float h2 = dht2.readHumidity();
 float h3 = dht3.readHumidity();
 float t0 = dht0.readTemperature();
 float t1 = dht1.readTemperature();
 float t2 = dht2.readTemperature();
 float t3 = dht3.readTemperature();

 dataFile = SD.open("DHT11Log.csv", FILE_WRITE);
 //  if any result below is NaN (not a number) then something went wrong!
 if (dataFile) {
   output_value= analogRead(A0);
   delay(10);
   output_value1= analogRead(A1);
   delay(10);
   output_value2= analogRead(A2);
   delay(10);
   output_value3= analogRead(A3);
   delay(10);
   output_value4= analogRead(A4);
   output_value = map(output_value,1024,0,0,100);
   output_value1 = map(output_value,1024,0,0,100);
   output_value2 = map(output_value,1024,0,0,100);
   output_value3 = map(output_value,1024,0,0,100);
   output_value4 = map(output_value,1024,0,0,100);
   Serial.print("Moisture : ");
   Serial.print(output_value);
   Serial.println("%");
   Serial.print(output_value1);
   Serial.println("%");
   Serial.print(output_value2);
   Serial.println("%");
   Serial.print(output_value3);
   Serial.println("%");
   Serial.print(output_value4);
   Serial.println("%");
   Serial.print("Humidity  : sensor 0, ");
   Serial.println(h0);
   Serial.print("  sensor 1, ");
   Serial.println(h1);
   Serial.print("  sensor 2, ");
   Serial.println(h2);
   Serial.print("  sensor 3, ");
   Serial.println(h3);
   Serial.println(" %\t");
   Serial.print("temperature  : sensor 0, ");
   Serial.println(t0);
   Serial.print("  sensor 1, ");
   Serial.println(t1);
   Serial.print("  sensor 2, ");
   Serial.println(t2);
   Serial.print("  sensor 3, ");
   Serial.println(t3);
   Serial.println(" %\t");
   dataFile.print(line++);
   dataFile.print(":    Temperature = ");
   dataFile.print(",");
   dataFile.print(t0);
   dataFile.print(",");
   dataFile.print(t1);
   dataFile.print(",");
   dataFile.print(t2);
   dataFile.print(",");
   dataFile.print(t3);
   dataFile.print(",");
   dataFile.print("°C,    Humidity = ");
   dataFile.print(",");
   dataFile.print(h0);
   dataFile.print(",");
   dataFile.print(h1);
   dataFile.print(",");
   dataFile.print(h2);
   dataFile.print(",");
   dataFile.print(h3);
   dataFile.print(",");
   dataFile.println("%");
   dataFile.print(",");
   dataFile.print("Moisture : ");
   dataFile.print(",");
   dataFile.print(output_value);
   dataFile.print(",");
   dataFile.print(output_value1);
   dataFile.print(",");
   dataFile.print(output_value2);
   dataFile.print(",");
   dataFile.print(output_value3);
   dataFile.print(",");
   dataFile.print(output_value4);
   dataFile.close();

  }
  // if the file didn't open, print an error:
  else
    Serial.println("error opening DHT11Log.txt");
    delay(10000);
}

Those YL-69 moisture "sensors" are not reliable at all. There is a voltage on the metals in the soil. They will maybe work for hours or days or weeks, and then you have to look for something better.

I think that the problem is here:
output_value = map(output_value,1024,0,0,100);
output_value1 = map(output_value,1024,0,0,100);
output_value2 = map(output_value,1024,0,0,100);
output_value3 = map(output_value,1024,0,0,100);
output_value4 = map(output_value,1024,0,0,100);

replace them with:
output_value = map(output_value,1024,0,0,100);
output_value1 = map(output_value1,1024,0,0,100);
output_value2 = map(output_value2,1024,0,0,100);
output_value3 = map(output_value3,1024,0,0,100);
output_value4 = map(output_value4,1024,0,0,100);

Thank you so much! I didn't spot that at all, I will update my sketch tomorrow, i'll post back here if it all works ok.                                                                                Thanks again

@Paddy You’re welcome! Please, If I helped you, remember to mark question as resolved. :)

Tested it there, and it works a treat. I'll now mark this as solved. Cheers!


text:
How to generate 4 square signals using Arduino Mega 2560 below 30hz using timers
I would like to generate 4 square signals of different frequencies below 30 hz using timers of Arduino Mega.
I was able to generate 30.6hz signals but I have no idea how to generate lower frequencies.
Any help would be appreciated.

Yes, you can use PWM with timer.  Take a look to this guide: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

How low? lower than 1Hz? Is it without pwm, just a 50% square wave? How accurate should be frequency be? Should the signal be very steady or is a software (with or without interrupt) also possible. I assume there is a library that can use all the timers of the arduino mega, but I could not find it yet. Please also tell us what your project is for.

30.6 Hz is the slowest you can get from an 8 bit timer. For lower frequencies, you will have to use 16 bit timers.


text:
Fail using OTA - Network ports
I was using OTA to upload sketches to ESP ports I have ( around 10 ESPs).
For some time now - I'm having some kind of a trouble I would like to share, since I've read some similar topics, but all didn't work.
The problem is when trying to select Network Port as I did before, sometimes I see all, sometimes part, sometimes only one or two, and sometimes after selecting to one I want to upload to, it fails since it disapears from that list by the time it starts the upload.
I'm using IDE 1.8.8 on a Linux machine - same issue,
Tried on mac, using both 1.8.8 and even downgraded to 1.8.7 - and I'm getting the same issue.
What might be the cause for that ?

it could be the wifi AP or router - my wifi router for example does not handle ESP8266's very well at all (WRT32X) - it's a known issue with this router. One way to fix it on the WRT32X is to turn off WMM

@JaromandaX I have a Xiami R3 router. Do you know if it applies as well ? what is WMM ?

no idea, I just presented a similar issue ... WMM is wifi multimedia mode or something like that

nevermind, I thought the latest dev version of arduino/esp8266 fixed this issue - but OTA still fails most of the time

@JaromandaX as a workaround ( mostly works, but still..) after a reset command ( which is an MQTT predefined command), the particular device pops back into that list.


text:
Code not working
Basically, it's an Arduino project in which I am stuck. I using inputs as two sensors and LED as output, for further details and concept video had been attached. I did the wiring and everything correctly but there is something wrong with the code which I couldn't figure out. Here is the code.
I had attached a video for proper understanding.
https://youtu.be/ZQg_xbGQNE0
const int s1 = 22;     // sensor one
const int s2 =  23;      // sensor two
int pot_pin = A0;
int list[15] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46}; //leds pin
//if u want change the time delay
int timer = 1000;

bool flag1 = false;
void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(s1, INPUT);
  pinMode(s1, INPUT);
  // initialize the LED pin as an output:
  for (int thisPin = 0; thisPin < 15; thisPin++) {
    pinMode(list[thisPin], OUTPUT);
  }

}

void loop() {
  // read the state of the pushbutton value:
  int output = analogRead(pot_pin);
  int led_value = map(output, 0, 1023, 0, 255);

  if (digitalRead(s1) == HIGH) {
    for (int thisPin = 0; thisPin < 15; thisPin++) {
      if (digitalRead(s2) == LOW) {
        // turn the pin on:
        analogWrite(list[thisPin], led_value);
        //digitalWrite(thisPin, HIGH);
        delay(timer);
      }
      else {

        for (int de = 14; thisPin < 15; thisPin++) {
          analogWrite(list[thisPin], led_value);
          analogWrite(list[de], led_value);
          //digitalWrite(thisPin, HIGH);
          //digitalWrite(de, HIGH);
          delay(timer);
          de--;
        }

      }
    }
    flag1 = true;

  }
  if (digitalRead(s2) == HIGH and flag1 == true) {
    for (int thisPin = 0; thisPin < 15; thisPin++) {
      if (digitalRead(s1) == LOW) {

        digitalWrite(list[thisPin], LOW);
        delay(timer);
      }
      else {
        for (int de = 14; thisPin < 15; thisPin++) {
          digitalWrite(list[thisPin], LOW);
          digitalWrite(list[de], LOW);
          delay(timer);
          de--;
        }

      }

    }
    flag1 = false;
  }
}

What exactly is now working? Show us schema of your hardware. Are you using ArduinoUno?

Arduino mega, the second case is not working, you can have a look into the video.

Is the video what it should be? Is the sketch only for CASE-2?

Please edit your post and replace the title with one that gives more information about the question being asked.

the code is for all the cases

how do you know that it is not working? .... you said nothing about what you expect to happen ..... you also said nothing about what actually happens

You might have seen the video, you may see different cases been mentioned now according to me the code I made is ok, now the second case according to video is not working further all the cases are working, now I myself not getting that why it is not working accordingly, and i what i want is all the cases should run in a single program.


text:
RFID RC522 reads the previous tag serial with different tag
I hope someone can help me.
I'm using a Mega with RC522, and this happens:
I use a tag, it reads the right serial (e.g. AA:BB:CC:DD). Then I use a second tag and sometimes it gives me the correct value (e.g. 00:11:22:33), but often it gives me the previous tag serial (AA:BB:CC:DD). After a few attempts, it finally gives me the right one (00:11:22:33).
I really can't find what's wrong and i'd be grateful if someone could shed some light.
Here's the code:
if (RC522.PICC_IsNewCardPresent())  {
           RC522.PICC_ReadCardSerial();
          String strID = "";
        for (byte i = 0; i < 4; i++) {
          strID +=
            (RC522.uid.uidByte[i] < 0x10 ? "0" : "") +
            String(RC522.uid.uidByte[i], HEX) +
            (i != 3 ? ":" : "");
        }
        String seriale = strID;

        if (seriale != "0"){

          for(h=0; h<=numerogiocatori -1;h++){

            if (seriale == seriali[h]) {
              lcd.clear();
              lcd.print("Saldo Gioc. ") ;
              lcd.print(h + 1) ;
              Serial.print("tag ID: ");
              Serial.print(seriale);
              Serial.print(" .    ");
              lcd.setCursor(0, 1);
              lcd.print(totaledenaro[h]) ;
              delay(1500);
              lcd.clear();
              lcd.print("Importo");
              lcd.setCursor(0, 1);
            }
          }
        }}}

Thanks to anyone willing to help!

I tried at the beginning but wasn't working, thank you for improving it.

Dario, would you mind sharing your conditional IF solution?

One rule for writing safety code is :
The return value of non-void functions must be checked by each calling function, and the validity of parameters must be checked inside each function.
You forgot to check if the tag has been readed i.e the return value of PICC_ReadCardSerial. If it fails the value is not updated, that is the reason why the value is still the old one.
Using the following code (from the ReadNUID example of the MFRC522 library https://github.com/miguelbalboa/rfid/blob/master/examples/ReadNUID/ReadNUID.ino)
// Init array that will store new NUID
byte nuidPICC[4];

void loop() {

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    Serial.println(F("NUID has NOT been readed."));

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }

    Serial.println(F("The NUID tag is:"));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

void printHex(byte *buffer, byte bufferSize) {
      for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
  }
}

the result is

A new card has been detected.
  The NUID tag is:
  97 C6 6E 09
NUID has NOT been readed.
  Card read previously.
  A new card has been detected.
  The NUID tag is:
  CC 16 5D 83
  A new card has been detected.
  The NUID tag is:
  97 C6 6E 09
NUID has NOT been readed.
  Card read previously.
  A new card has been detected.
  The NUID tag is:
  CC 16 5D 83

Sometimes when the second tag is used the tag is not read and the uid remains the same.

Thanks man. I did solve it applying a conditional IF to the ReadCardSerial, but your version is much better!

"One rule for writing safety code is" +1.


text:
How to make Variable Frequency Drive (VFD) Inverter using Arduino
I have a VSI SPWM Inverter Project, but my supervisor ask me to upgrade it to a VFD Inverter. I've found a code about how to generate 3 phase SPWM in this article, and i've tried the code. The output signal is great. But i need to improve the code to VFD code with range frequency 0-100 Hz. I've found a reference about VFD from this one and tried to modify the SPWM code, but failed. Could anyone help me to improve the code, please? Here is the SPWM code..
int i=0;
int j=0;
int k=0;
int x=0;
int y=0;
int z=0;
int OK=0;
int OK1=0;
int OK2=0;
int OK3=0;
int OK4=0;
int sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,
44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99,
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,
148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193,
195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,
228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246,
247,247,247,248,248,248,248,249,249,249,249,249,255,255,255,255,249,249,249,249,249,248,
248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,
233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,
202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160,
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,
103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30,
27,24,22,19,17,15,12,10,7,5,2,1};

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

pinMode(4, OUTPUT);
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);

cli();// stop interrupts
TCCR0A=0;//reset the value
TCCR0B=0;//reset the value
TCNT0=0;//reset the value
//0b allow me to write bits in binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //no prescaler

TCCR2A=0;//reset the value
TCCR2B=0;//reset the value
TCNT2=0;//reset the value
//0b allow me to write bits in binary
TCCR2A=0b10100001;//phase correct pwm mode
TCCR2B=0b00000001; //no prescaler

TCCR3A=0;//reset the value
TCCR3B=0;//reset the value
TCNT3=0;//reset the value
//0b allow me to write bits in binary
TCCR3A=0b10100001;//phase correct pwm mode
TCCR3B=0b00000001; //no prescaler
TCCR1A=0;//reset the value
TCCR1B=0;//reset the value
TCNT1=0;//reset the value
OCR1A=509;// compare match value
TCCR1B=0b00001001; //WGM12 bit is 1 for CTC mode and no prescaler

TIMSK1 |=(1 << OCIE1A);// enable interrupts

sei();//stop interrupts
}
ISR(TIMER1_COMPA_vect){// interrupt when timer 1 match with OCR1A value
if(i>313 && OK==0){// final value from vector for pin 13
i=0;// go to first value of vector
OK=1;//enable pin 4
}
if(i>313 && OK==1){// final value from vector for pin 4
i=0;//go to firs value of vector
OK=0;//enable pin 13
}
x=sinPWM[i];// x take the value from vector corresponding to position i(i is zero indexed)
i=i+1;// go to the next position
if(OK==0){
OCR0B=0;//make pin 4 0
OCR0A=x;//enable pin 13 to corresponding duty cycle
}
if(OK==1){
OCR0A=0;//make pin 13 0
OCR0B=x;//enable pin 4 to corresponding duty cycle
}

if ((i==209) || OK1==1){//if i equal with 209 enable second signal
OK1=1;//mantain if function
if(j>313 && OK2==0){// final value from vector for pin 10
j=0;// go to first value of vector
OK2=1;//enable pin 9
}
if(j>313 && OK2==1){// final value from vector for pin 9
j=0;//go to firs value of vector
OK2=0;//enable pin 10
}
y=sinPWM[j];// y take the value from vector corresponding to position j(j is zero indexed)
j=j+1;// go to the next position
if(OK2==0){
OCR2B=0;//make pin 9 0
OCR2A=y;//enable pin 10 to corresponding duty cycle
}
if(OK2==1){
OCR2A=0;//make pin 10 0
OCR2B=y;//enable pin 9 to corresponding duty cycle
}
}
if ((j==209) || OK3==1){//if j equal with 209 enable third signal
OK3=1;//mantain if function
if(k>313 && OK4==0){// final value from vector for pin 5
k=0;// go to first value of vector
OK4=1;//enable pin 2
}
if(k>313 && OK4==1){// final value from vector for pin 2
k=0;//go to firs value of vector
OK4=0;//enable pin 5
}
z=sinPWM[k];// z take the value from vector corresponding to position k(k is zero indexed)
k=k+1;// go to the next position
if(OK4==0){
OCR3B=0;//make pin 2 0
OCR3A=z;//enable pin 5 to corresponding duty cycle
}
if(OK4==1){
OCR3A=0;//make pin 5 0
OCR3B=z;//enable pin 2 to corresponding duty cycle
}
}
}
void loop() {

}

Most, like I, in this corner of stackexchange.com (Arduinos) are likely thinking you mean Vacuum Florescent Display.  Not Variable Frequency Drive. You may get more responses if you explain this in your title.

Thanks for your reply, i'll edit the title..

i have sum code please visit my https://forum.arduino.cc/index.php?topic=645080.msg4363292#msg4363292


text:
How do I turn on/off led with the same IR button?
I'm trying to make the led switch on/off by the same case(): code.
But its not working could anyone help me?

Have you tried checking the current state and acting accordingly?

It would be good if you add your sketch if you have one.
In principle, you have to create a boolean variable which keeps the current state of the LED. When you get an IR command, inside the IR button handler invert the state of the boolean. Than change the LED accordingly.
Something like:
Const/Global variable:
#define LED_PIN 8

int ledState;

In setup:
 ledState = FALSE;
 digitalWrite(LED_PIN, ledState);

In side loop:
case IR_BUTTON_CHANGE_LED:
   ledState = ledState == LOW ? HIGH : LOW; // Invert state
   digitalWrite(LED_PIN, ledState); // Set LED according to state
   break;


text:
How to rotate two or more steppers simultaneously by giving ARRAY of angles?
The following is the code that I am trying but the problem is steppers rotate one after each other. However, I want to rotate steppers simultaneously.
This code for two stepper motors.
int pulse=3;

int direc=4;

int pulse1=5;

int direc1=6;

int i,j,k;

float angle[5][3]={{360,360,3},{7,8,9},{11,12,13},{14,15,16},{17,18,19}};

float steps;

void motor1(){

  for(k = 0; k<=4; k++){

    Serial.println(angle[k][0]);

    steps = (1600*angle[k][0])/360;

    Serial.println(steps);

    for(j = 0; j<=steps;j++){

      digitalWrite(3,HIGH);

      delayMicroseconds(100);

      digitalWrite(3,LOW);

      delayMicroseconds(100);
        }

}

}

void motor2(){

  for(k = 0; k<=4; k++){

    Serial.println(angle[k][1]);

    steps = (1600*angle[k][1])/360;

    Serial.println(steps);

    for(j = 0; j<=steps;j++){

      digitalWrite(5,HIGH);

      delayMicroseconds(100);

      digitalWrite(5,LOW);

      delayMicroseconds(100);

        }

}

}

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

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop(){
    motor1();

    motor2();

   //while(1);
}

Hello and welcome. Your question needs more detail and might be closed. Please read [ask] and How to ask a good question for Arduino Stack Exchange

So you are outputing the steps to monitor, but how are you controlling your steppers? what controllers are you using? what is your schematic?

(Half a year ago, but what the hey...)
Your motor1() and motor2() are currently written as:
do until done:
  take a step
  wait
end

, so yeah, one finishes before the next one even starts.
Each motor function needs to do:
if motor's current-position is not the final position,
   if it is time to take a step,
      take a step
      calc when to take another step
      calc motor's new current-position
   end
end

Now call your motor functions as frequently as possible. Notice that either if test can cause the function to return without doing anything - that is intentional.
This is non-blocking programming - coding in such a way that nothing keeps control while waiting for something to happen (clock ticks, in this case), it just does something, or does nothing, right now, if there is or is not something to do right now, but it does release the processor (the function returns immediately) whether it did something or not.
I call these functions "maybe-do" functions:

maybe step a motor (if, and only if, a certain interval has passed, step the motor);
maybe light an LED (if, and only if, the button is being pressed and it wasn't pressed the last time we looked, turn on the LED;
maybe read the terminal keyboard (if there is at least one character in the Serial input buffer, collect a character).

Then your loop() function should do nothing but call each maybe-do function, in sequence, as fast as possible. And it follows from the above that your maybe-do functions need to execute as quickly as possible and do no more than is necessary to (1) decide whether to act, and if so, (2) to carry out its action, and (3) in either case, return immediately.

And - Bang-Boom! - right there is a half-semester course in block-free programming without interrupts!

This is a valuable reference answer, that could be useful for sooo many questions.

I can't understand clearly what you want, but maybe you want to drive your two servos simultaneously. To do this, after you determine the angle of servos, you can drive them without delay between their commands.
Like you use above, you can't drive them at the same time, there are loops and they have to be completed before the next instruction is run so your servos wait each other.
Here is a good example code for you:
#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo

float angle[5][3]={{360,360,3},{7,8,9},{11,12,13},{14,15,16},{17,18,19}};

void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
  for(k = 0; k<=4; k++){
    pos = (1600*angle[k][0])/360; //Calculate the angle which you want

    //The most impportant part, there shouldn't be a delay between them
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'

    delay(1000);                       // waits 1000ms for the servo to reach the position
  }
}

Thank you sir for your answer but i want to rotate motor 1 for this loop,
pos = (1600angle[k][0])/360; and motor 2 for this loop  pos = (1600angle[k][1])/360;

Then, add pos2= (1600angle[k][1])/360 below pos = (1600angle[k][0])/360. And, change the myservo2 command to myservo2.write(pos2). So your first motor rotates x degree while your second motor rotates y degree without any delay.


text:
Coin recognition using Coin Accepter in Arduino Uno
I'm working on a vending machine project and I'm using CH-926 and an Arduino Uno for that. I've already set the coin accepter for different number of coins and respective output pulses. I'm unable to get the desired output for this. With every coin I insert the output varies even when I put the same coin.
int pulse = 0;

void coin_value() {
  pulse = pulse + 1;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  Serial.println("Ready to be collected");
}

void loop() {
  if (digitalRead(2) == HIGH) {
    attachInterrupt(0, coin_value, FALLING);

    if (pulse == 3) {
      Serial.println("50");
    }
    if (pulse == 2) {
      Serial.println("20");
    }
    if (pulse == 1) {
      Serial.println("10");
    }
  }

  pulse = 0;
}

For each coin there are different specific pulses which helps to recognize the coin.int pulse=0;.
So when I insert coin 1 then the output should be 10, for coin 2 the output should be 20 and for coin 3 the output should be 50.

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

However he was asked on Stack Overflow to ask the question here, which he did.

what does this actually mean? I'm unable to get the desired output for this ..... what do you expect? .... what do you actually get?

@NickGammon The question should be only on one site. If found a better site for it, he should delete the old one.

@gre_gor  I've flagged the SO post for deletion. However Sumit Kumar could help by self-deleting, if the system lets him.

Your code is faulty in a number of respects.

pulse should be declared volatile, i.e.
volatile int pulse = 0;

That is because it can change in an interrupt service routine.

You should only do attachInterrupt once, in setup.

Your code to display the count will be timing dependent. What if you happen to display the count after one pulse has arrived, but before they all have? Then you set the count back to zero.

Better would be something like this, untested however:
volatile int pulse = 0;

void coin_value() {
  pulse = pulse + 1;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  Serial.println("Ready to be collected");
  attachInterrupt(0, coin_value, FALLING);
}

void loop() {

  if (pulse > 0)
    {
    delay (1000);   // let the other pulses arrive
    if (pulse == 3) {
      Serial.println("50");
    }
    if (pulse == 2) {
      Serial.println("20");
    }
    if (pulse == 1) {
      Serial.println("10");
    }
    pulse = 0;  // clear for next time
  }

}

(Edited to add)
Even then your test for 1/2/3 might fail if the coin acceptor sends the pulses twice. Then you might get 2/4/6 pulses. You probably need to display something if there is another count, eg.
  if (pulse == 3) {
    Serial.println("50");
  }
  else if (pulse == 2) {
    Serial.println("20");
  }
  else if (pulse == 1) {
    Serial.println("10");
  }
  else {
     Serial.print ("Unexpected number of pulses: ");
     Serial.println (pulse);
  }

First of all you said

So when I insert coin 1 then the output should be 10, for coin 2 the
  output should be 20 and for coin 3 the output should be 50.

Yes coin 3 would be 50 but it does not means that coin 3 has 3 impulses(he will have 5 impulse), I recommend to you always use this formula:

Coin_Value = Pulse_Count * Denomination.

I don't recommend to you use 3 Impulses from coin acceptor Because you don't have 15 cent and 30 cent coins. In most cases Coin acceptors have:

1/4 1/2 1 2 4 5 8 10 impulses

Generally I prefer to save all data in variables
So lets get to variables for code
const int coinIntPin = 2;          //interruptPin 0 is digital pin 2
volatile boolean newCoin = false;
volatile int pulseCount;          //counts pulse for
volatile int timeOut = 0;         //counts timeout after last coin inserted
float coinType;
float denomination = 0.50;         // in my case 1 Pulse = 0.50 $
int max_impulse = 4;              // this is the maximum impulses I use
volatile long timeFPulse = 0;     // to determine when First impulse was interrupted
volatile long interval;           // to determine interval from first and last impulse

And always use attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); It is recommended by Arduino  as below:
void setup(){
    Serial.begin(9600);
    attachInterrupt(digitalPinToInterrupt(coinIntPin), coinInserted, FALLING);

}

And I will give you rest of code:
void coinInserted(){
    newCoin = true;
    timeFPulse = millis();
    pulseCount++;
}

void loop()
{

     while (newCoin == true)
    {
            interval = millis() - timeFPulse;  //interval from first and last impulse
            if(pulseCount == max_impulse || (interval > 500 && pulseCount > 0)) //Max_impulse will avoid any extra impulses for any reason.
            {
// This is the reason why I recommended to you use 1 2 4 5 10 20 Impulses instead of 3 7 9 etc...
              coinType = pulseCount * denomination;
              newCoin = false;
              sendData();
              delay(5);

             }

    }
}

void sendData(){
  Serial.print("Do what ever you want. Inserted Money = ");
  Serial.println(coinType);
  coinType = 0;  // remove if you want to save Coins and print sum of inserted coins
  pulseCount = 0;  // remove if you want to save impulses determined by coin acceptor
}

And Finally look at ( interval > 500 ) If you have different max impulses you must change 500 to (impulse length * Max impulses + 100); so if you have max 10 impulses you must enter 1100 and not 500 (in my case);


text:
How can i stop a loop after 1 minute?
This is my first Arduino project, and I would like to stop my servo's loop after 1 minute. Can someone help me?
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);
}

void loop()
{
  myservo.write(45);
  delay(1000);
  myservo.write(125);
  delay(1000);
}

Thanks!

You are asking the wrong question. The loop function runs forever. How many times do you want the servo to move between those values? If that is for example 30 times, then you can put a for-statement in the setup function and use a empty loop function.

what do you do when you want to boil eggs for one minute? .... do the same in the program

Thank you so much!!!! that was exactly what i needed!! i also tried before to run a servo and a stepper at the same time and that was exactly what happened the stepper was blocking the servo and i wasn't understanding why!! thank you so much for the explanation, it truly helped!!

Whose explanation are you thanking? The answer boxes are not for comments, I'll migrate your answer to a comment under your question. I'm glad one of us helped you. :)

As suggested by Jot in a comment, one way would be to simply do what you want (which takes roughly two seconds per iteration) in a loop, like this:
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);

  for (int i = 0; i < 30; i++)
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000);
    }
}

void loop()
{
}

The problem with that is it relies upon knowing how long your delays are, and assumes that the myservo.write() functions take no time. A better method is:
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);

  unsigned long start = millis ();

  while (millis () - start <= 60000)  // for 60 seconds
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000);
    }
}

void loop()
{
}

This uses the millis() function call to find out how much time has elapsed, and wait for 60000 milliseconds (60 seconds).

I've put the code into setup because that is where you put things you want done once.

To answer your question as asked, add this to loop():
if (millis() >= 60000) exit(0);

However, I would suggest you seriously consider Jot's comment: simply
stopping loop() from running is most of the time not what you may
want to do.

Another way to handle this is to use a variable endTime to tell the loop when to stop moving the servo:
unsigned long endTime;

void setup()
{
  myservo.attach(5);
  endTime = millis() + 60000;
}

void loop()
{
  if (millis() <= endTime) {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000);
  } else {
    //code to run once the servo is done running
  }
}

This code will move the servo back and forth for about 1 minute, then stop. It's set up so you can do something else once the loop has completed.
Note that by using a variable endTime and leaving your servo code in the loop() function, you can have your program start doing something else once the motor motion is completed. You can also add code to set up a new value in EndTime, at which point the servo will start moving back and forth again.
Also note that using delay is bad practice. When you do that you bring the arduino to a full stop while the delay code is running. You can't be doing something else. Say, for example, you wanted to move 2 different servos at different time intervals.
Instead of using delays you should track the end time for each phase of your loop, and execute your code once that end time passes.

This code will move the servo for one minute every 49.7 days.

Yeah, I suppose that's true, if you Arduino plugged in that long. (I mostly work on 64 bit platforms now, where an unsigned long is 64 bits, long enough that the above code would only run once in my or my grandchildren's lifetime.) It would be pretty easy to add a bool to the code above to turn it off completely when not in use.


text:
Detect DS18B20 sensors and correctly assign them to relays
I'm relatively new to microcontroller programming, so please excuse me if my question is dumb. I've created a simple device that allows me to control 4 relays based on temperature. I bought four DS18B20 sensors, 4 relay board and Wemos D1. Everything works fine - I'm able to read the temperature of each sensor based on their address.
But I started wondering how I can replace a single DS18B20 without recompiling the code; currently this isn't possible. After searching a bit I found a couple of questions about the order of 1Wire sensors. For example, this one. I have no control over the order in which sensors are detected.
Is there a best practice when it comes to configuring DS18B20 sensors?
I would like to set up my device. By set up, I mean that I would like to assign a specific DS18B20 to a specific relay.
I imagine it like so:

Take a new DS18B20, connect it to a device and power the device.
The device should scan for DS18B20 sensors. If it finds one then it should assign it to relay 1 and store its address in EEPROM.
Then unplug the device from power, plug the second DS18B20 and power the device again.
The device again scans for DS18B20 sensors. It finds 2 devices. The first one is known (the address was stored in EEPROM) so it is skipped. The second is assigned to relay 2 and the address is stored in EEPROM.
The same procedure is applied to the third and fourth sensors.
If four sensors are connected (the addresses are from EEPROM), then the device can run.
When the user wants to replace a non-functioning sensor, the same procedure can be used.

As I wrote, I'm new to microcontroller programming, so if there is a better way please let me know. I'm mostly learning by following examples and tutorials, so if anyone knows of an example showing such a configuration procedure I'll be grateful for any materials.

How many ds18b20 sensor are you going to break? There is indeed no control over the order when you use byIndex. Every ds18b20 has a unique identifier, you can store that in eeprom. That is the only way to be sure to read the temperature from a specific sensor. You can do what you wrote, but you could also use a button to scan for a new sensor. The more complex you make the code, the more chance for a bug. It is easier when there is a display or a serial port or a webinterface.

personally, I'd go with a web interface for configuration - though, if it's easy enough to connect to a PC if you need to replace a DS18B20, then a simple serial interface would suffice

@Jot my idea is to build a device that I can give to my friends and put on GitHub so everyone can use. Idea is to remove step where the user must read DS18B20 addresses and put them into code. I want to use the same method to set up a new device or replace a broken sensor.

@JaromandaX I'm planning to add web interface, but I was plannign to use it only to show readings and relays statuses. I wasn't thinking about using it to setup sensors. I must look into this :)

If you can easily disconnect the sensors one at a time then your way is fine.
If you can't easily disconnect them then you can maybe use readings to set the order, although this depends on the environment. If, for example, all the sensors are generally room temperature then you have the code check the temp of all the sensors at start up. If it finds one sensor at less than 0C then assign that sensor to the least-recently-replaced relay index (also stored in EEPROM).
Then when you want to put a sensor into the working set, hold an ice cube on it for a few seconds before powering up the controller. There are other combinations of this technique you can imagine that use a button or jumper wires to specifically set the index of the out-of-range-at-startup sensor.
Alternately if you have an extra pin on the Arduino, then you can make a procedure where you connect a new sensor to that pin before attaching it to the string. You can make a rule like "if you ever see a sensor on pin X then remember its device ID and assign it to relay Y". There are lots of UI ways to pick Y. Note that you do not even need any extra hardware for this.


text:
Lowest latency triggering multiple Arduinos using NRF24L01+
I'm building a rig with multiple DSLR cameras.  I need to synchronize the cameras to take a photos with lowest possible latency.  The only constraint is we want the ability to address each camera individually if needed.
I've seen in other posts:
Choosing wireless tech for lowest possible lag
NRF24L01+ seems to be a good choice.  An answer on that post states:
sending a 32 byte payload...283µs on 2Mbps mode.
Given the  esp8266 has a clock speed of 80MHz/160MHz I'm not too concerned about the lag involved in processing the signals.
Most multi-cast information out there is for sensor arrays, so multiple out single in.  I'm after resources on single out multiple in for NRF24L01+ networks.
Added bonus of esp8266 is that non timing critical tasks such as logging and error checking could be performed over wifi so NRF24L01+ would be purely for one -way triggering.
If each camera had it's own esp8266 based board with attached NRF24L01+ hardware and was given an address in software from 0-255, is it possible to broadcast a single 32 byte packet of data where each bit would represent a camera address and have all receiver devices receive the same signal, do an AND against it's address and trigger if needed?

From another answer to a similar question they mentioned the NRF24LE1 module.  Basically a programmable NRF24L01+.  I wonder why this never took off?  Seems super useful for simple triggering/data collection.

Yes, that should work, however also think about the following issues:

What if one (or more) camera does not receive the signal? You might need to add an ACK signal, but of course this will reduce the latency (a lot). If you don't want an ACK signal
To prevent an ACK, you can send the signal many times (and cameras who already processed the signal not react for a fixed amount of time). Of course it will received by the other cameras (at least) 283 us later.
If reliability is important, you could try using a less high speed, the latency will go up, but reality too.

Also you probably can fire the cameras even faster, if you first send the address bytes (4 bytes). And than send the shoot signal which can be in principle 1 bit. You can use one of the bits for that in case 128 cameras would suffice. Or send one byte extra which contains the signal to shoot.

Thanks!  So to get this working from a hardware POV all I need to do is make sure all receiver NRF24L01+ are using the same channel/address (not my software address) and disable auto acknowledge?  Good idea about splitting address and command.

Also, since configuring each receiver over wifi is not timing critical, are there any other simpler interfaces such as 433/900MHz?

I never worked with 433/900 MHz. I think you probably can better send the 'shoot' signal multiple times instead of a (complicated) ACK protocol. I think the protocol itself will not change if you use another frequency.


text:
Arduino Micro draws too much power from iPhone. How can I change that?
I'm building a USB keyboard that has two buttons - space and enter. The plan is to use this USB keyboard (with an Apple Lightning connector to USB) with the built in iOS switch control. I bought the lightning to USB dongle, and hooked it up to a normal keyboard, and it works fine.
Next, I took an Arduino Micro (ATMEGA32u4) and programed it to act as a keyboard with the two keys I need (space and enter). On a PC, it works just fine, but when I hook it up to my iPhone, I get the message:
Arduino Micro: The connected device requires too much power.

I've done quite a bit of research on this, and I found this post. In a nutshell, that post said that when you connect a device to an iOS device, one of the first things it does is tell the iDevice how much current it could potentially draw. This number (about 200mA for the Arduino Micro) is what decides whether the iDevice will support the device or not, when in truth, the device will not come close to its max current draw, at least not in my case.
I hooked up a meter to the normal keyboard, and it draws just over 4mA. When I hook up the Arduino, it is drawing almost 40mA. While the Arduino is drawing much more than the keyboard, it should be okay, because when I plugged in a flash drive, it was drawing 50mA, but the iPhone didn't complain.
There is my story, here is my question:
Is there any way to change it so that the Arduino Micro doesn't request so much power? In other words, is there a way to reset the value that is causing the iPhone to not use the device?

do not use an arduino for this ..... take apart a regular USB keyboard ..... discard the switches and install a couple of push buttons to replace the two keys

Did you get this to work? I am working on a keyboard I need to connect to iPhone and I can't get it to work. Please help

The power consumption is part of the exchange with the PC when it is plugged in. You can change that. Find the file USBCore.h in your Arduino install directory. In my case (under Linux) it was:
./hardware/arduino/avr/cores/arduino/USBCore.h

Inside that file, at around line 269 (depending on the distribution) you should see these lines:
#define D_CONFIG(_totalLength,_interfaces) \
    { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(500) }

On the right of the second line is the requested power consumption in milliamps (currently 500). Change that to (say) 100:
That is, change USB_CONFIG_POWER_MA(500) to USB_CONFIG_POWER_MA(100).
Save and recompile.
You may find that the bootloader initially requests 500 mA even with this change (as it initially runs the bootloader). However when the sketch starts it should re-establish a USB connection and only request 100 mA. To fix that you would need to recompile the bootloader with the same fix, and reinstall it, a somewhat more complex task.
Another possible approach would be to tweak the fuses so that it doesn't run the bootloader, if you have finished debugging your code. Only do that if you are confident with playing with the fuses.

Majenko beat me to it while I was testing my answer. :)

I have a simple Arduino keyboard and it works great on all other devices then iPhone. Did you guys get this to work on iPhone?

@ErikAndershed This sounds like a new question to me. Feel free to make one.

Yes, but it requires manual modification of the Arduino core software.

Find the file USBCore.h within your AVR boards installation (it could be in the data storage folder wherever that is on your OS, or within the actual IDE software)
Look for the line #define D_CONFIG(_totalLength,_interfaces) \
The next line has the power setting.  Change USB_CONFIG_POWER_MA(500) to what you require (for example USB_CONFIG_POWER_MA(50)

I did test with 50. But it do not work. When I plug it in to my iPhone with a adapter (from usb-c to lighting and it’s supports both power and data). My Arduino chip don’t wake up. Did you get this to work?


text:
Splitting a Serial.readString into string array
I am working on a project where I need to split incoming data from serial (time to be exact, so aa:bb:cc) and need to split it into aa then bb and cc, ideally into  an array. I know I could use readStringUntil and make 3 separate variables, but I would prefer it is in arrays. Here is the code that supposed to do this. It works with predefined string (eg. 00:00:00), but with reading serial it says that the initializer fails to determine size of "input".
Thank you in advance for helpful suggestions.
Here is the code:
void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
  while(!Serial.available()){} //wait until Serial data received
  String vst = Serial.readString();
  char buf[vst.length()+1];
  vst.toCharArray(buf, vst.length());
  char input[] = vst;
  char delic[] = ":";
  char* sub = strtok(input, delic);
  while(sub != NULL){
   Serial.println(ptr);
    sub = strtok(NULL, delic);
  }

}

Instead of vst.toCharArray() (which copies the data) just directly use vst.c_str() which is a char array already. You don't need buf

Your main problem is this:
char buf[vst.length()+1];

You can't initialize an array with the return value of a function.
Instead you would need to use alloca to perform the same function:
char *buf = alloca(vst.length()+1);

However, the way you're doing it is wasteful. You don't need buf at all.  You can directly access and manipulate the internal char buffer inside the string using vst.c_str();.
For convenience you can point buf at it:
char *buf = vst.c_str();

To complete the picture here's a routine that takes the string and gives you three integers. Since we expect a specific format with only 3 sections to it we don't really need a loop.
// Use readStringUntil to avoid the 1 second delay.
String vst = Serial.readStringUntil('\n');
char *buf = vst.c_str();

char *hourString = strtok(buf, ":");
char *minuteString = strtok(NULL, ":");
char *secondString = strtok(NULL, ":");

if (secondString != NULL) { // Will only be true if we have two colons
    int hours = atoi(hourString);
    int minutes = atoi(minuteString);
    int seconds = atoi(secondString);

    Serial.print("It is ");
    Serial.print(minutes);
    Serial.print(" and ");
    Serial.print(seconds);
    Serial.print(" seconds past ");
    Serial.println(hours);
} else {
    Serial.println("Invalid time format");
}


text:
Memory problem? dtostr() and strcat()
I've been dealing with this for quite a while now and searching all over for answers.. I'm trying to put together two floats into a char*. Separated by a ",".
This is the code I'm working with.

#include <DHT.h>
#include <RH_ASK.h>
#include <SPI.h>

#define DHT_pin 10
#define DHT_type DHT11

DHT dht(DHT_pin, DHT_type);
RH_ASK rf_driver;

float temp;
float hum;

char ch_temp[6];
char ch_hum[6];
char msg_out[12] = "";

void setup() {
  Serial.begin (115200);
  dht.begin();
  if (rf_driver.init()) {
    Serial.println("Radio transmitter initiated..");
  }
  else {
    Serial.println("Radio transmitter failed to initiate..");
    delay(1000);
  }
}
void loop() {
  temp = dht.readTemperature();
  hum = dht.readHumidity();

  dtostrf(temp, 5, 2, ch_temp);
  dtostrf(hum, 5, 2, ch_hum);
  strcat(msg_out, ch_temp);
  strcat(msg_out, ",");
  strcat(msg_out, ch_hum);

  Serial.println(msg_out);
  rf_driver.send((uint8_t *)msg_out, strlen(msg_out));
  rf_driver.waitPacketSent();
  Serial.print("data sent, package size: ");
  Serial.println(strlen(msg_out));
  delay(1000);

}

I have tried using different sizes of the char arrays, but it just goes crazy.
This is the console output:
Radio transmitter initiated..
22.00,34.00
data sent, package size: 11
⸮⸮Radio transmitter initiated..
22.00,34.00
data sent, package size: 11

So on its way through the second loop it fails, restarts and then on its third way through it just gives up... What am I doing wrong? :( Really sad over this.

If you remove the RF and sensor code, the sketch seems to work correctly when printing to the serial monitor. The first time you call rf_driver.send() in the loop, it works. The second time through the loop, and every time there after, it starts printing corrupted data. I don't have a definitive answer why this is happening, but I do have a way to "fix" it (or a "hack", if you prefer to call it that).
Using memset() to set the msg_out char array to all 0's before you use it seems to fix the problem. Here is a test sketch which is very similar to yours. I'm using some hard coded values for the sensor data because I don't have one of those sensors.
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;

float temp;
float hum;

char ch_temp[6];
char ch_hum[6];
char msg_out[12] = "";

void setup() {
  Serial.begin (9600);
  if (rf_driver.init()) {
    Serial.println("Radio transmitter initiated..");
  }
  else {
    Serial.println("Radio transmitter failed to initiate..");
    delay(1000);
  }
}
void loop() {

  // Set the buffer contents to all 0's
  memset(msg_out, 0, sizeof(msg_out));

  temp = 12.345;
  hum = 67.890;

  dtostrf(temp, 5, 2, ch_temp);
  dtostrf(hum, 5, 2, ch_hum);
  strcat(msg_out, ch_temp);
  strcat(msg_out, ",");
  strcat(msg_out, ch_hum);

  Serial.println(msg_out);
  rf_driver.send((uint8_t *)msg_out, strlen(msg_out));
  rf_driver.waitPacketSent();
  Serial.print("data sent, package size: ");
  Serial.println(strlen(msg_out));
  delay(1000);
}

Your sketch is running on borrowed memory.
Every time the loop function runs, the new text is concatenated with strcat() and the buffer msg_out is never reset.
The first time 12 bytes are used, the second time 23 bytes, the third time 34, and so on.
The solution in the answer by @VE7JRO will fix that, but I prefer not to use dtostrf() and transmit binary data.
I see a few other problems as well:

Why do you want to transmit readable ASCII text? Use binary data.
Either float numbers or integers. For example an integer in 1/100 degrees.
The RadioHead library uses a lot of memory. You can not use an other
library that uses also a lot of memory.
The value of '5' for dtostrf is the minimal width,
so you better make those arrays for example 20 bytes.
I prefer to put those arrays temporarily on the stack.
The DHT11 is not accurate. If you upgrade to a better sensor with I2C,
then you need more libraries (the Wire library and a sensor library),
and you might run out of sram memory.
When using the RadioHead library in default mode, it uses three
pins (rxPin=11, txPin=12, pttPin=10). You better add a comment
section to your sketch to explain that you should not use those pins.
You have already a
conflict at pin 10 for the DHT sensor and the pttPin.

The old and no longer supported VirtualWire library has some bugs, but it uses less memory than the RadioHead library. The RadioHead library is not optimized for Arduino boards like the Uno, Nano, Leonardo, and others. It even requires to #include <SPI.h> because that is often used with transceivers, even if the SPI bus is not used in RH_ASK mode (thanks to @VE7JRO for explaining this in the comment below).

The easiest way to transmit more than one value is with an array.
float myData[2];
myData[0] = 23.456;
myData[1] = 68.0;
rf_driver.send((uint8_t *)myData, sizeof(myData));

The sizeof() is not a real function, it tells the compiler to fill in the size of that variable. Since a float is 4 bytes, you may also fill in the number 8 for the second parameter.
There are a number of solutions for the receiver. I like to use a pointer to the buffer, but perhaps it is easier to copy them to variables with memcpy().
float myData[2];
if (driver.recv(buf, &buflen)) {
  if (buflen == sizeof(myData) {  // extra check
    memcpy((uint_8 *)myData, buf, sizeof(myData));
    Serial.println(myData[0]);
    Serial.println(myData[1]);
  }
}

When the receiver is also an Arduino board, then there should be no problem. When the receiver is something else, you might have to check if the byte-order is the same. The float variable of a Arduino is 100% IEEE compatible, but sometimes it is easier to use only integers.
The most common way is to use a struct. A struct is a package that can hold all sorts of data (integers, bytes, arrays, and so on). The receiver and the sender should have exactly the same definition for the struct of course.

The OP may not be using SPI. If you remove the include from the sketch, it won't compile. There is a sketch called ask_transmitter.pde that comes with the library. It has a comment beside the SPI include that says: "Not actually used but needed to compile". I am new to Arduino, so I do not understand this comment. If none of the RadioHead files use any functions, etc. in the SPI library, why won't it compile with out it? I second your recommendation to get rid of the "low quality" sensor the OP is using. For $1.28USD, you can get a decent I2C Bosch sensor like a BMP280 at Aliexpress.com.

@VE7JRO right, I knew it but forgot about it.

Thank you for this thorough answer! Really appriciate it!

@Jot Thank you for this thorough answer! Really appriciate it!! The reason i convert into Char* is to put it together, so i can send it in one package and then later on an other MCU easily take it apart and display seperatly. How would i go about putting to floats together and being able to seperate them later?
I've tried VirtualWire, but in this case this is alll this MCU is supposed to do. But I will keep this in mind in the future! Thanks!
This project is for a school assignment, but i've noticed the DHT11 is not very reliable..
Thanks for pointing out the PIN conflict!!

@Love.Berg I have added an extra section to transmit an array. It is transmitted as binary data and received as binary data, so all you have to do is to take the binary data from the float variables, transmit them and in the receiver put the binary data back into float variables.

I'm late to the party, but here goes:
msg_out[] is a global but it doesn't need to be. Move it's definition and initialization into loop(), the only function that uses it. Then it gets initialized at every entry to loop() rather than just once at program initialization. There's no need to clear the array; just zeroing the zero-th element of it initializes it for character-string use, and that is what your initializer now does (but only once).


text:
Stuttering motor using rf module
I am trying to control a stepper motor wirelessly with a button. I am using the 433MHZ rf module and the Virtualwire library.
the code is working, the receiver is getting the messages from the transmitter, but the problem is the messages are sent extremely slowly, so much so that the receiver circuit led is flashing on and off instead of staying in the on position when the transmitter button is being held down. consequently the stepper motor stutters instead of moving smoothly.
I have tried increasing the bits per second in the vw_setup but that just makes the led flash faster and not stay in the on position. it also makes the stepper vibrate more.
how do i make the stepper motor move smoothly wirelessly over rf?
transmitter code:
#include <VirtualWire.h>

int currentlocation = 0;
const int stepPin = 3;
const int dirPin = 4;
int active = 0;
const int ledPin =  13;      // the number of the LED pin
String dataout;
char buffer[1];
int idx=0;

class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }

    Button(int pin1, int state1, int speed1, int direction1)
    {
      pin = pin1;
      state = state1;
      Speed = speed1;
      Direction = direction1;
    }

    Show(){
      if (digitalRead(pin) == HIGH){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,30,0,0);
Button Movebutton1(5,0,500,HIGH);
Button Movebutton2(6,0,500,LOW);
Button buttons[2] = {button, button2};
Button Movebuttons[2] = {Movebutton1, Movebutton2};

void setup() {
      vw_setup(2000);
      pinMode(stepPin,OUTPUT);
      pinMode(dirPin,OUTPUT);
      pinMode(ledPin, OUTPUT);
      Serial.begin(9600);      // open the serial port at 9600 bps:
}

void loop() {
    for (int i = 0; i<2; i++){
      buttons[i].Show();
      if (digitalRead(buttons[i].pin) == HIGH){
      sprintf(buffer, "%d", i);
      vw_send((uint8_t *)buffer, strlen(buffer));
      vw_wait_tx();
      }
    }
    for (int i = 0; i<2; i++){
      Movebuttons[i].Show();
      if (digitalRead(Movebuttons[i].pin) == HIGH){
      sprintf(buffer, "%d", i+2);
      vw_send((uint8_t *)buffer, strlen(buffer));
      vw_wait_tx();
      }
    }
}

Receiver code:
#include <VirtualWire.h>

char message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
int last_idx = 0;

int currentlocation = 0;
const int stepPin = 3;
const int dirPin = 4;
int active = 0;
const int ledPin =  13;      // the number of the LED pin
String str_out;

class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }

    Button(int pin1, int state1, int speed1, int direction1)
    {
      pin = pin1;
      state = state1;
      Speed = speed1;
      Direction = direction1;
    }

    Relocate(){
      int shift = location - currentlocation;
      if (shift > 0){
        Direction = 0;
      }
      else{
        Direction = 1;
      }
      shift = abs(shift);
      digitalWrite(dirPin,Direction); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for(int x = 0; x < shift; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }
      currentlocation = location;
    }

    Move(){
      digitalWrite(dirPin,Direction); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(Speed);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(Speed);
      if (Direction == 1){
      currentlocation -= 1;
      }
      else{
      currentlocation += 1;
      }
    }

    Show(){
      if (vw_get_message(message, &messageLength)){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,30,0,0);
Button Movebutton1(5,0,500,HIGH);
Button Movebutton2(6,0,500,LOW);
Button buttons[2] = {button, button2};
Button Movebuttons[2] = {Movebutton1, Movebutton2};

void setup() {
        // Initialize the IO and ISR
      vw_setup(2000); // Bits per sec
      vw_rx_start(); // Start the receiver
      Serial.begin(9600);
      pinMode(stepPin,OUTPUT);
      pinMode(dirPin,OUTPUT);
      pinMode(ledPin, OUTPUT);
}

void loop() {

      // Set buffer to size of expected message
    if (vw_get_message(message, &messageLength)){
    int idx=atoi(message);
    Serial.print(message);
    for (int i = 0; i<2; i++){
      buttons[i].Show();
      if (idx == i){
      buttons[i].Relocate();
      active = i;
      //buttons[i].location = currentlocation;
      }
    }
    for (int i = 2; i<4; i++){
      Movebuttons[i-2].Show();
      if (idx == i){
      Movebuttons[i-2].Move();
      buttons[active].location = currentlocation;
      }
    }
    }
}

how do i make the motor move smoothly when i hold down the button wirelessly, how do i stop the jittering

what is the relationship between the button and the motor?  .... please provide a clear description of the expected behavior of the device

the button will cause the receiver to
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(Speed);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(Speed);

in other words, step the motor once

when held in the on position it will continue to step

press the button, hold for 5 seconds, then release ..... that requires only two messages to be sent 5 seconds apart ..... why should the motor stutter in between the two messages?

no i want to hold the button and send the on position while im holding it down

the motor is stuttering because the messages are stuttering

read my comment again and think about it

every  time the loop function executes it checks to see if the button is on right? so if the message is not getting there on time it assumes that it is off

A transmit bit rate of 2000 is normal. Some 433MHz modules can do 4kHz, but that should be specified and such specifications are hard to find. There is overhead, I'm guessing that one value takes about 20 to 25 pulses. By law the transmitter may only be busy for 10%. That results into sending data 8 times per second. Just 8 per second, no more.

What is the incoming message interval (1/incomingMessageFrequency)? Whenever the receiving end receives a "button-down" message, it should activate the motor for that interval + long enough to receive or not receive the next "button-down". Once the next "button-down" should have been received and has been missed, that's when you should stop the motor.


text:
Can not print an element in an specific position in an arduino list
I have been tying to print an element of a list with the same position as the number that it matches, but it does not seem to work and I can not figure out if I made some mistake, could someone please check it? Would really appreciate it
Here are the list declarations:
int names[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
char *users[10] = {"Josh", "John", "Frederic", "Mikael", "Anthony", "Charles", "Tom", "Chris", "Steve", "Mike" };

And this is what I am using to print the name I need:
if (attempt != FINGERPRINT_OK) return -1;
Serial.println ("Belongs to: ");
if(data.fingerID == names[n]) {
    Serial.print(users[n]);
}

Complete Code:
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);

Adafruit_Fingerprint data = Adafruit_Fingerprint(&mySerial);

int names[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
char *users[10] = {"Josh", "John", "Frederic", "Mikael", "Anthony",
"Charles", "Tom", "Chris", "Steve", "Mike" };

void setup() {
  Serial.begin(9600);
  while (!Serial);
  data.begin(57600);
  if (data.verifyPassword()) {
    Serial.println("Verified connection");
}

  else {
    Serial.println("Unnable to verify connection, try again");
 while (1) { delay(2500); }
}

data.getTemplateCount();
Serial.print("Waiting for one of the:"); Serial.print(data.templateCount);
Serial.println("valid fingerprint");

int getFingerprintIDez();
uint8_t getFingerprintID();

void loop() {
for ( int i = 0; i <= 10; i++) {
 uint8_t attempt = data.getImage();
  switch (attempt) {
   case FINGERPRINT_OK:
   break;
 delay (2500);
}

attempt = data.image2Tz();
  switch (attempt) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
    break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
    return attempt;
  }

 attempt = data.fingerFastSearch();
   if (attempt == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
    digitalWrite (13, HIGH);
    delay(1250 );
    digitalWrite (13, LOW);
  }
   else if (attempt == FINGERPRINT_NOTFOUND) {
   Serial.println("Did not find a match");
   digitalWrite (12, HIGH);
   delay(1250 );
   digitalWrite (12, LOW);
   return attempt;
  }
Serial.print("Found ID #"); Serial.print(data.fingerID);
Serial.print(" with confidence of "); Serial.println(data.confidence);
return data.fingerID;

uint8_t attempt2 = data.getImage();
if (attempt2 != FINGERPRINT_OK) return -1;
 attempt2 = data.image2Tz();
if (attempt2 != FINGERPRINT_OK) return -1;
 attempt2 = data.fingerFastSearch();
if (attempt2 != FINGERPRINT_OK) return -1;
 Serial.println ("Presents: ");
if(data.fingerID == names[n]) {
    Serial.print(users[n]);
}
}
}

I am attempting that when a fingerprint is verified, the position that matches it out of all the stored fingerprints gets stored as n so I can later print the name asigned to that same position. Thanks

What's 'n' ? Please provide complete and minimalistic example that we can debug.

What does your actual output look like? What do you want it to look like instead?

Right now I can not upload it because "'n' was not declared in this scope", last time I tried it was not able to recognize the name associated with each fingerprint value, so the names were not printed

How about you completely delete
int names[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

and instead do:
if (attempt != FINGERPRINT_OK)
    return -1;

Serial.println ("Belongs to: ");
Serial.print(users[data.fingerID - 1]);


text:
Trying to Pulse an LED a number of times outside of the main loop
I'm trying to call a class function that pulses an LED "x" number of times. I dont want to use a delay, which is making it tough. If I use a while loop, the LED just stays on. I attach an interrupt in the begin() - the location of the LED is pin 13
I call the following in my sketch like so:
robot.lights.pulse(0, 2000, 10000, 500); //in milliseconds

My goal is to pulse an LED "x" number of time at a rate I specify.
This is the code inside my class:
void StatusLights::begin(int locations[]){
    for(int i=0;i<NUM_OF_LIGHTS;i++){
        this->locations[i]=locations[i];
        pinMode(locations[i], OUTPUT);
    }

    ledState = LOW;
    previousMillis = 0;
    attachInterrupt(0, pulse_ISR, RISING);

};

void StatusLights::pulse(int location, int interval, int count, int speed){

    int value = 0;
    previousMillis = 0;
    // while(pulsecnt<count) {
        long currentMillis = millis();

        if (currentMillis - previousMillis >= interval) {
            previousMillis = currentMillis;
            value = 128+127*cos(2*PI/interval*(speed-currentMillis));
            analogWrite(locations[location], value);
        };
    // };
    // digitalWrite(locations[location], LOW);
};

void pulse_ISR() {
    pulsecnt++;
};

This is my first time using interrupts to do this - I'm really trying to avoid delays since i need other things to happen synchronously.
Thanks!

Can you show us a working sketch? The currentMillis should be unsigned long. What kind of pulse do you want, because the human eye has a log scale and you use cosine. How often do you want an update of the led brightness?

@Jot added a function call to my question. thanks

A working sketch is something we can try without guessing what the rest of the code is. I would like to see the rest of the sketch because I think you have made it too complex. Start with the 'update' function to keep it going as @JRobert writes in his answer. That is a common way to deal with it in arduino projects.

then no i dont without the other five classes

Is your previousMillis a long as well? That's not okay. This is what I mean with guessing.

You'll want a class function that decides whether 'now' is time to toggle the LED or not, and returns quickly. Call that function frequently from your loop function. Your other class functions will set or clear the whether the LED is to toggled, and when, how many times, etc.  Something like the following pseudo-code, which is not a full solution but should point out the essential details:
void loop()
{
   myStatusLights.updateLED();     // call this frequently
}

...

void StatusLights::updateLED(){
  if LED_active,
     if it's time to change the LED,
        toggle LED on/off;
        if LED has toggled enough times,
           set LED_active false;
        endif;
     endif;
  endif;
}

Update:

How would you refresh the count, track the pulses, and refresh the state of the status light without any parameters?

Some class-function must initiate the blinking, whether it knows to do it itself or it is called from your application code. That class-function will need to set those parameters. If it to be called from outside, perhaps the outside sets them - ? Only you know that. Either way, they will need to get saved in the class' private data so the .updateLED() function can have access to them. The .updateLED() function will, for instance, count down the remaining number blinks to display, keep track of whether the LED is on or off, actually turn it on or off, etc., until there is nothing left for it to do. In that case it just quickly returns without doing anything.

I'm not sure I follow toggle LED on/off;         if LED has toggled enough times,            set LED_active false;         endif; this is exactly what im trying to do, but I'm not sure of the syntax - sorry the sudo code isn't helping in this case. @JRobert

I understand that you want to repeatedly call an update function - How would you refresh the count, track the pulses, and refresh the state of the status light without any parameters? what if i want to set pulse the led 10 times in the setup function?

@rcpilotp51, you have a class for status lights and you don't know where to hold the status of lights? http://www.cplusplus.com/doc/tutorial/classes/

@Juraj i actually have an array of light locations "pins" and want to call them based on other events and want them to do different things...do i need an update function for each?

You don't need a function for each; one update function can loop through the array of LEDs and apply the same logic to each one, based on each particular LED's class variables' values. Each LED will needs its own set of e.g., pin#, blink-interval, blinks-remaining, etc.


text:
I want to connect my NodeMcu v3 to gsm
I have a problem with connecting my NodeMcu to gsm. I have bought GSM module but I have sim code on card which I can't and that is the problem because I've found some libraries but there are no examples how to connect with pin code. I have found one but not compatible with NodeMcu. How can I do It?

put the card in a phone and remove the PIN

It's not possible


text:
How to publish on Pubnub with node mcu?
I have node mcu and i am trying to use the example code on github and pubnub blog to send a msg as an experiment but can't figure how to do it.
I used 2 example as the reference - https://github.com/pubnub/arduino , https://www.pubnub.com/docs/arduino/data-streams-publish-and-subscribe
Here is the code which i have. I modified it to include some libraries and my API keys. Let me know what changes i have to make:-
#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP

#include <PubNub.h>

String pubkey = "pub-c-4a9cdbca-5688-4939-a852-f63cf6743980";
String subkey = "sub-c-9b4067ae-1293-11e9-b4a6-026d6924b094";
String pubchannel = "sec-c-NmFiYjliODYtMTc3Mi00YTMzLTg0OWMtMDgzYjE2ZGUwNzg5";

void setup() {
    /* For debugging, set to speed of your choice */
    Serial.begin(9600);

    /* Start the Pubnub library by giving it a publish and subscribe
       keys */
    PubNub.begin(pubkey, subkey);
}

void loop() {
    /* Maintain DHCP lease. For other HW, you may need to do
       something else here, or maybe nothing at all. */

    /* Publish message. You could use `auto` here... */
    PubNonSubClient *pclient = PubNub.publish(pubchannel, "\"message\"");
    if (!pclient) return;
    PublishCracker cheez;
    cheez.read_and_parse(pclient);
    /** You're mostly interested in `outcome()`, and,
        if it's "failed", then `description()`.
    */
    Serial.print("Outcome: "); Serial.print(cheez.outcome());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.outcome()));
    Serial.print("description: "); Serial.println(cheez.description());
    Serial.print("timestamp: "); Serial.println(cheez.timestamp());
    Serial.print("state: "); Serial.print(cheez.state());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.state()));
    pclient->stop();

    /* Wait for news. */
    PubSubClient *sclient = PubNub.subscribe(subchannel);
    if (!sclient) return; // error
    String msg;
    SubscribeCracker ritz(sclient);
    while (!ritz.finished()) {
        ritz.get(msg);
        if (msg.length() > 0) {
            Serial.print("Received: "); Serial.println(msg);
        }
    }
    sclient->stop();

    delay(1000);
}

Error:-
node_mcu_test_code:44:46: error: 'subchannel' was not declared in this scope

     PubSubClient *sclient = PubNub.subscribe(subchannel);

                                              ^

node_mcu_test_code:54:14: error: 'class PubSubClient' has no member named 'stop'

     sclient->stop();

              ^

exit status 1
no matching function for call to 'PubNub::begin(String&, String&)'

what is subchannel? pubchannel?

#include <PubSubClient.h> - why? no demos or docs say you need to do that

 is what I found in old forums so put that just for checking. I specified sub and pubchannel with API key. If not like that then how to do it.

is what I found in old forums so put that just for checking don't add things without knowing what they do

also, you've not declared a variable called subchannel ... yet you try to use a variable called subchannel ... which is why you get an error that states error: 'subchannel' was not declared in this scope - because it isn't

How to declare subchannel? Is it the name of channel which we subscribe or secret API key? I tried putting subchannel name but didn't work. My channel name is 'Demo Keyset'

The pubchannel and subchannel can be defined as any values in char variable.
Here is the right code:-
const char * pubkey = "publisher key from your account";
const char * subkey = "subscriber key from your account";

const char * pubchannel = "demo_keyset";
const char * subchannel = "demo_keyset";

Then the error disappeared