text:
How do I accelerate a stepper motor?
I have a 24BYJ48 stepper motor, and my main problem is that I cannot make it rotate quickly. I've understood that in order to achieve a high speed I must start at a low speed and carefully accelerate the motor to higher speeds.
I'm not quite sure how this acceleration ought to be done, though. My idea is to execute the control sequences with an initially long delay, and then decrease the delay between each time I change the sequence for the motor wires.
Below is the code I have written. When I run it, the motor accelerates for a little while, but then all of a sudden it just stops and makes a high pitched noise.

https://www.tigoe.com/pcomp/code/circuits/motors/stepper-motors/

The Arduino manual says, that delay() function only excepts long ints > Arduino Delay.

You didn't include your entire program. Try including it as text, not an image. Stack exchange has some syntax highlighting. Also, as auoa said, delay() only excepts long integers. Use delayMicroseconds() instead for more precision (which still uses integers).

The basic technique is as you have tried to program. The time between stepper pulses if shortened to cause acceleration and lengthened to slow down. A real solution typically uses an "S" curve type acceleration profile that starts out with a slow ramp up to the acceleration rate and then as it nears the target rotational speed it slows the acceleration rate back down.
In your case you may be getting to the point where you have increased the acceleration rate too much and the motor cannot respond to it.

Does this mean the motor cannot go any faster? If so, are there any workarounds?

@user2157416 - I cannot answer your question for you. I do not know your motor characteristics nor am I in a position to unthread your code to see where any weakness that may exist. For all I know maybe the fact that you set the four output drive signals one at a time could be a problem, you may have a completely unsuitable power supply or even a stepper driver circuit that is not up to the job.

Also the motor driver circuit can be a problem, does it handle the dead time for you? If not your code can produce short circuits within a half bridge. Or the motor current is not sufficient for high speeds.

@auoa - the upper and lower drivers are not being distinctly controlled by software, so software can neither cause nor prevent short circuits.  Dead time from the perspective of what is being controlled would merely be time when there is no applied voltage difference across a coil from the drivers, however there may still be current flow through the catch diodes from inductive decay.

There are several things going on here....

If the load is purely inertial and small (~ equal to the rotor inertia), acceleration typical takes only a few steps.  No need to drag it out.  Try some different delays after the first step.
If you have a torque load (you are pushing something), at some point, during acceleration, the currents in the winding will be too small to generate the required torque.  As the speed increases, the currents will decay, due to the motor back-EMF and inductance (which is very significant).
Sometimes, at particular speeds, step motors exhibit a dynamic instability.  That is, they start hunting...faster, slower, faster, slower, etc. until the motor "stalls".  The resistance seen by the motor, looking back into the drive circuit, is the key factor here.  At high speeds, the current regulator runs out of voltage and the effective resistance drops...leading to instability.

So, do some experimentation!!!


text:
Arduino analog input protection
I've been trying to find a way to protect my analog input pins (for my Leonardo and Pro micro). I'm basically using them to read an amplified piezo sensor output. The output is amplified because I'm using it to measure very low signal (heartbeats). The normal level of the sensor's output doesn't exceed 5V when I'm measuring heartbeats or chest movement, yet when the sensor is moved rapidly or lifted, I can expect a 5v+ voltage. I know that Arduino has an internal protection circuit, will that be enough to combat those sudden movements?
If not, I've read so many posts on this topic including this one:
http://www.thebox.myzen.co.uk/Tutorial/Protection.html
If I wanted to add a series resistor by itself as a protection will it be okay? and also, my signal is already small, wouldn't adding a resistor attenuate it even more?
Most circuits seem to have a capacitor, my signal is DC shifted upward (to avoid -ve voltage), wouldn't the capacitor remove that offset?
Finally, if I wanted to use the diode clamping diodes, which diodes should I use (i.e 1n4148 or 1n4002)?
Thank you

If you amplified with an op-amp powered by 5V then it couldn't produce more than 5V output, so that could be your solution. Failing that, I think a resistor and a couple of clamping diodes would help. The resistor is to current-limit the clamping diodes. I don't think you need to worry about dropping the voltage through the resistor because the analog input is high impedance.
I think the 1n4148 would be OK, I don't see why not.

With an Opamp, the output swings between -4V and 4V (approximately) if my supply is 5V right?. I tried building a buffer with 5V (single supply) to have the output swinging from 0 to 5V but I seem to loose the signal below my DC offset point (1.8V). The buffer takes it's input from the original heartbeat amplifier (AD620) which I'm powering with -5V and +5V and it produces a DC offset of 1.8V (Vref is set by another Op Amp voltage divider.

I've built the clamping diodes circuit as in the following link: http://www.thebox.myzen.co.uk/Tutorial/Protection_files/shapeimage_10.png .... I used 22R, my diodes were 1N4148. I got better results! my signal now ranges from -840mV to 5V. Do you think the -840mV will burn my board?

Well ... you are exceeding the maximum (negative) ratings of -0.5V on an input pin by 380 mV.

I think you should be able to get an op-amp to do it by offsetting the input to the op-amp and powering by only 0 to 5V.

To be honest, you are taking me out of my area of expertise here. I Googled "op amp offset voltage" and found quite a few hits, some nearby on Electronics Stack Exchange. I think you should be able to configure your op-amp to give you a voltage range that won't exceed your processor specs. My advice is to not exceed the absolute maximum ratings of the processor, however you achieve that.

Actually, I remeasured the range again and it's between -840mV and 4.16V so this is around 5V range but shifted downward abit. I tried changing the diodes to 1n4002 and this didn't fix it. I also tried changing the supply and that -840mV is still there. How can I shift it upward a bit?

This is not really an Arduino question - it's an electronics question, so I suggest you ask on Electronics Stack Exchange.

Unless you supply the opamp with a negative voltage, it can't produce a negative voltage.

Use a voltage comparator to buffer the input to analog pin . E.g.  a cheap lm 393. If the signal is less than 5v in buffer circuit is stored and passed on- if its more than 5v voltage comparator blocks the , more than 5v, voltage to the arduino.

I know that Arduino has an internal protection circuit, will that be enough to combat those sudden movements?

it depends on the voltage your pin may see. the pins have clamp diodes - the reset pins may not have, so check the datasheet for sure.
those diodes can sustain some current - check the datasheet on that - absent of it, I wouldn't go more than 10ma on those little guys.
So if you think you may see 10v on the input, put a resistor of (10v-5v) / 10ma = 500ohm resistor there, where 5v is (assumed by me) the supply voltage of your mcu - use the real figure if yours has a different supply voltage.
the same calculation with the negative rail /ground.
and pick the larger of the two.
I typically put a 1k resistor there. I try to be careful if i need to go over 10k, especially for adc pins.
a safer route is to use a (dual directional) buffer. but it does add cost + board real estate.

The datasheet says nothing about the maximum current these diodes can sustain (yes, it's a shame), but application note AVR182 states “It is not recommended that the clamping diodes are conducting more than maximum 1mA”.

source: Stack Exchange

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

sort: votes


text:
Arduino + UltraSensor - Serial Monitor outputs garbage
I am trying to connect an HC-SR04 to an Arduino Uno. For some reason when I run this code on the serial monitor it outputs wingdings in a straight line.
ýòøÿüÿøÿüòý÷ÿ÷ýÿÿúýÿýúÿòý÷ûòûòû÷ýÿøÿøÿÿ÷øÿüÿýÿþòøòÿÿøûüÿøÿÿòÿ÷ûòûòý÷ûòûòûöû÷ÿûû÷øÿÿ÷øÿüÿýÿüÿûöþÿý÷ûòûòûòûöûûûûûò
No breaks. Sometimes when I hit "Tools/Fix Encoding and Reload" it outputs
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
followed by more wingdings.
I've looked up some similar problems and it was usually fixed by changing baud to a lower value, 9600. I've done this and I still have problems.
Would you be able to help me out? I'd very much appreciate it.
const int trigpin = 9;
const int echopin = 10;
long duration;
int distance;

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

void loop() {
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  digitalWrite(trigpin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigpin,LOW);
  duration = pulseIn(echopin,HIGH);
  distance = duration *0.034/2;
  Serial.print("Distance: ");
  Serial.println(distance);
}

Updated Solution
After testing out Joel's code to check the sensor was in order, I found some new sample code which worked to display the distance in inches of a HC SR04 sensor.
  // set up variables

  int trigpin = 13; // Trig Pin is connected to Arduino pin 13
  int echopin = 11; // Echo Pin is connected to Arduino pin 11

 // now we need variables which we will be measuring

 float pingtime; // Time for ping to travel to target and return
 float targetdistance; // Distance from sensor to target
 float speedofsound = 776.5; // speed of sound in miles per hour at 77 F

void setup() {
Serial.begin(9600); // Turn on serial port
pinMode(trigpin, OUTPUT); // Sensor trig pin is output
pinMode(echopin, INPUT); // input is reading from echopin

 }

void loop() {
digitalWrite(trigpin, LOW);// set trigger pin low
delayMicroseconds(2000); // pause to let signal settle
digitalWrite(trigpin, HIGH); // set trigger pin high
delayMicroseconds(15); // pause in high state, make sure sensor reads
digitalWrite(trigpin, LOW); // bring trigpin back low

   pingtime = pulseIn(echopin, HIGH);

// sensor will be sending HIGH pulse, measure pingtime at echopin
// ping time is in microseconds

  pingtime = pingtime / 1000000.;

// converts pingtime to seconds because speed of sound is miles per hour

  pingtime = pingtime / 3600.;
  // converts pingtime to hours
  // add a period to make float to avoid int mistakes

  targetdistance = speedofsound * pingtime;
  // calculates distance in miles traveled by ping

  targetdistance = targetdistance / 2;
  // accounts for roundtrip of ping to target

  targetdistance = targetdistance * 63360.;
  // convert target distance to inches
  // 63360 inches in a mile

Serial.print("The distance to the target is:");
Serial.print(targetdistance);
Serial.println("inches");
delay(1000);

 }

since it seems that the problem did have to do with the serial monitor settings, can you please select my answer as correct.

Check your serial monitor baud rate. This is definitely caused by your serial monitor being set to a different baud rate than the 9600 you specified in the program.
If it had anything to do with the sensor than this line would be printed properly Serial.print("Distance: ");
Once you change the serial monitor baud rate then try running this program, to verify that it is set properly:
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hi There!");
}

If that prints "Hi There!" then the program your serial monitor is set up properly.

I tested your code and I get an infinite loop of "Hi There"s which I will take as a good sign. :D

@AngelaAlbrecht that is a good sign, try your original code again without changing any settings and see if it prints "Distance: "

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


text:
Trying to explain strange behavior when attempting to do a digitalWrite to A7 on a pro mini?
I found out the hard way today that pins A6 and A7 can't be used as digital outputs, but what actually happens when you try to read/write them?
Some backstory: I soldered some headers onto a pro mini and I wanted to test all the pins to make sure they worked.  I wrote a test program that looped through all the digital pins once a second and toggled their state by doing digitalWrite(pin, !digitalRead(pin);.  I then added a second loop that went through pins A0 through A7 and toggled them using the same method.  After adding this loop the built in LED on pin 13 no longer toggled on and off every second.  Instead, it would just flicker on very briefly.  At first I assumed there was something wrong with my loops (like an off by one) that was causing the strange issue, so I completely rewrote things to use a single loop that iterated over all the pins.  The resulting code should have been effectively equivalent to the original code, but now pin 13's LED was toggling properly again.
After much experimenting, I was able to whittle it down to the following test program to demonstrate the strangeness:
#define WORKS_CORRECTLY 1

int pins[] = {13, A7};
#define SIZE (sizeof(pins) / sizeof(int))

void setup()
{
#if WORKS_CORRECTLY
    for(int i = 0; i < SIZE; i++)
        pinMode(pins[i], OUTPUT);
#else
        pinMode(pins[0], OUTPUT);
        pinMode(pins[1], OUTPUT);
#endif
}

void loop()
{
    for(int i = 0; i < SIZE; i++)
    {
        int pin = pins[i];
        int pinVal = digitalRead(pin);
        digitalWrite(pin, !pinVal);
    }

    delay(1000);
}

With WORKS_CORRECTLY defined as 1 the LED on pin 13 toggles state every second like one would expect.  With it defined as 0, however, it just flickers on very briefly every second.  This is very perplexing.
Ultimately the answer is to not try to do a digitalWrite to A7 (or A6), but I'd like to understand why the behavior changes based on seemingly irrelevant differences.

You are calling digitalWrite and digitalRead with argument A7.
According to the file pins_arduino.h, A7 is defined as 21.
Now, the two functions are defined in file wiring_digital.c, and the most important part is that both of them use this code to understand which register and bit to use:
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);

Those two are not functions, but macros defined in the Arduino.h file:
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )

This means that the positions are stored into two arrays, and each pin data is an offset from the beginning of the array itself. This means that the pin 0 is located on register digital_pin_to_port_PGM[0] (i.e. port D) and is in position digital_pin_to_bit_mask_PGM[0] (so bit 0).
Now, what about pin A7? It is in index 21. But look at the file pins_arduino.h:
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
    PD, /* 0 */
    PD,
    PD,
    PD,
    PD,
    PD,
    PD,
    PD,
    PB, /* 8 */
    PB,
    PB,
    PB,
    PB,
    PB,
    PC, /* 14 */
    PC,
    PC,
    PC,
    PC,
    PC,
};

The array has only 20 entries! This means that A6 and A7 are NOT mapped. When you call the function with A7, you are going beyond the array limits.
This means that this leads to an unexpected behavior. In THIS particular case you are getting
PORT: _BV(1) = 2 which is equal to PB in Arduino.h
BIT: NOT_ON_TIMER = 0

So when you read from bit A7 you will check for
*portInputRegister(port) & bit
*portInputRegister(PB) & 0 => 0 for every value

so it always reads a LOW.
When you write any value to it, you will apply this code:
if (val == LOW) {
    *out &= ~bit;
} else {
    *out |= bit;
}

bit is always 0, so if you write a LOW value you will write a HIGH on ALL bits connected to port B, otherwise you will write a LOW on all of them.
Different is the access to A6: the port will be mapped to 0, which is translated to NOT_A_PORT when detecting the register. This will try to read and write from/to register #0, which I admit I do not know what means. According to the ATMEGA328P register map it is reserved, so better not to use it.
Short answer: never try to use values which are not supposed to be used, otherwise you will incur in strange behaviors

Somehow it didn't occur to me to go digging through the arduino source code.  Thanks for the very quick and detailed answer and apologies for the delay in accepting it.

@Pak Well, you accepted it so.. thank you to take the time to come back and accept it ;)

pins A6 and A7 can't be used as digital outputs.

that shows the importance of reading the datasheet (and the schematic): there is no port / pins associated ADC6/A6 and ADC7/A7 on the chip. Absolutely no way of writing to / reading from those pins digitally.
None whatsoever.


text:
Load website from SD card
I combined two different codes together, a captive portal and a SD card web server. I encountered a problem which the website in the SD card doesn't load when I connect to the AP (captive portal page). Here's the code..

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "./DNSServer.h"
#include <SPI.h>
#include <SD.h>

const byte        DNS_PORT = 53;          // Capture DNS requests on port 53
IPAddress         apIP(10, 10, 10, 1);    // Private network for server
DNSServer         dnsServer;              // Create the DNS object
ESP8266WebServer server(80);
String responseHTML = ""
                      "<title>Captive Portal Test</title>"
                      "
TEST
";

static bool hasSD = false;
File uploadFile;

void returnOK() {
  server.send(200, "text/plain", "");
}

void returnFail(String msg) {
  server.send(500, "text/plain", msg + "\r\n");
}

bool loadFromSdCard(String path) {
  String dataType = "text/plain";
  if (path.endsWith("/")) path += "index.htm";
  if (path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if (path.endsWith(".htm")) dataType = "text/html";
  else if (path.endsWith(".css")) dataType = "text/css";
  else if (path.endsWith(".js")) dataType = "application/javascript";
  else if (path.endsWith(".png")) dataType = "image/png";
  else if (path.endsWith(".gif")) dataType = "image/gif";
  else if (path.endsWith(".jpg")) dataType = "image/jpeg";
  else if (path.endsWith(".ico")) dataType = "image/x-icon";
  else if (path.endsWith(".xml")) dataType = "text/xml";
  else if (path.endsWith(".pdf")) dataType = "application/pdf";
  else if (path.endsWith(".zip")) dataType = "application/zip";

  File dataFile = SD.open(path.c_str());
  if (dataFile.isDirectory()) {
    path += "/index.htm";
    dataType = "text/html";
    dataFile = SD.open(path.c_str());
  }

  if (!dataFile)
    return false;

  if (server.hasArg("download")) dataType = "application/octet-stream";

  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  }

  dataFile.close();
  return true;
}

void handleNotFound() {
  if (hasSD && loadFromSdCard(server.uri())) return;
  String message = "SDCARD Not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup(void) {
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("TEST AP");
  dnsServer.start(DNS_PORT, "*", apIP);
  //PROBLEM HERE/
  server.onNotFound([]() {
  server.send(200, "text/html", responseHTML);
  });

  server.begin();

  if (SD.begin(SS)) {
    hasSD = true;
  }
}

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

This code replies the responseHTML as a Captive portal page which immediately pop-ups when I connect to the AP:
server.onNotFound([]() {
      server.send(200, "text/html", responseHTML);
      });

The void handleNotFound() handles the page/file request from the browser but if I use it on
server.onNotFound(handleNotFound);

The captive portal won't show but if I type the server address 10.10.10.1, the page will appear. What seems to be the problem?

Remove hasSD && from void handleNotFound() function.
It worked for me.

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


text:
Getting a fixed sampling rate
Ultimately I will move to a generic smart phone for this project.
I'm trying to read 4 analog inputs from my arduino with a fixed 1kHz sampling frequency. I managed to set it to read at 1kHz as you can see from this link. The issue is that after running several times, I kept testing the frequency by logging the time vector (produced by arduino to the serial terminal) into Excel and calculating the frequency using the equation:
f=1/(t2-t1)
I did this to the entire column and I noticed that I got a changing frequency. The difference is very big as it ranges from 780 to 1kHz. I need a fixed sampling rate because we will be applying some digital signal processing on the obtained results which obviously requires a sampling frequency. My code to send the data is the following:
#define INTERVAL_LENGTH_US 1000UL

unsigned long previousMicros;
#define FASTADC 1
int recValue;
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
double t = 0;

void setup() {
  int start ;
  int i ;

#if FASTADC
  // set prescale to 16
  sbi(ADCSRA, ADPS2) ;
  cbi(ADCSRA, ADPS1) ;
  cbi(ADCSRA, ADPS0) ;
#endif

  Serial.begin(115200) ;
}

void loop()
{
  unsigned long currentMicros = micros();

  if ((currentMicros - previousMicros) >= INTERVAL_LENGTH_US)
  {
    previousMicros += INTERVAL_LENGTH_US;

    int val_a0 = analogRead(A0);
    int val_a1 = analogRead(A1);
    int val_a2 = analogRead(A2);
    int val_a3 = analogRead(A3);

    Serial.print(((double)currentMicros) / 1000000UL, 5 ); // 1 is the number of decimals to print
    Serial.print('\t');

    Serial.print(val_a0);
    Serial.print(',');

    Serial.print(val_a1);
    Serial.print(',');

    Serial.print(val_a2);
    Serial.print(',');

    Serial.println(val_a3);

  }
}

Ps. I'm using Arduino Leonardo,
What's the right way to achieve this?
EDIT: Thank you all for responding, As you wanted to see my data, here is a screenshot of the results after I applied Jwpat7's suggestion:

The yello shaded column is the frequency calculated, the first and 2nd columns represent T1 and T1 (T2 is just T1 shifted upward for easier calculation). You may notice the Matlab section, this is because I'm trying to plot the data in Matlab and use it to do the digital signal processing after transmission. The plotting will be real time (I'll the data in txt files for later processing). the bottom potion of the figure shows the frequency after a while....as you can see it's decreasing!
Thanks

Comments are not for extended discussion; this conversation has been moved to chat. Comments are also not for answering the question, or for discussing various possible solutions. Comments on questions are for clarifying the question. That's it!

You can speed your code up slightly by sending a raw microseconds reading, rather than divided by 1000000.  Floating arithmetic on typical Arduino devices can be quite slow.  Also, the time to complete a divide is data dependent, so may occasionally but not always delay the next reading; that is, the division may contribute to jitter as well as overall slowness.
Another source of jitter is polling via loop() rather than within loop [see below].  However, this probably adds only a few microseconds of jitter.
To test if too-slow serial transmission is the problem, you could comment out all the serial printing and substitute Serial.println(currentMicros).  (Or print the low two bytes of currentMicros.)

Polling within loop(): You could replace
currentMicros = micros();
if ((currentMicros - previousMicros) >= INTERVAL_LENGTH_US)
  {  ...  }

with
while (micros() - previousMicros < INTERVAL_LENGTH_US) ;
currentMicros = micros();
...

This would tighten up the busy-wait loop and reduce jitter slightly.
If small changes as suggested above don't resolve the problem, you might need to use timer-based interrupts interrupt flags to schedule trigger ADC reads (as suggested in comments) and/or create a circular data buffer to make data collection timing less dependent upon data transmisssion timing.
The ADC Auto Trigger Source Selections table in the ATmega32U4 datasheet shows the ADTS bit settings to select various ADC trigger sources, which include Free Running, Analog Comparator, External Interrupt Request 0, and various Compare/Overflow/Capture events on Timer/Counter0, 1, and 4.  Your ADC conversion-complete interrupt handler would need to read the ADC, store the reading, set a volatile flag to tell loop() which reading is ready, and set the ADC input selection to the next channel.

Edit 2: Where the code spends its time: In the version that busy-waits until the next time-mark arrives, and then takes four ADC readings, the process sits and waits for the ADC to complete.  With a standard prescale factor of 128 and 104 μs per conversion, about 416 microseconds are spent waiting on ADC completion.  If FASTADC is set and prescale is 16, giving 13 μs per conversion, then only 52 μs is spent in that waiting.  In brief: use of a standard prescale factor will probably use up too much time if the polling method is used.  But if readings are timer-interrupt triggered and ADC-ready-interrupt stored, a standard prescale factor is ok.
Serial transmission rate estimates:
As noted in Edgar Bonet's  answer, 115200 bps isn't fast enough to support 1000 Ascii-data serial transmissions per second.  If working at 100% throughput, that data rate allows only 115 bits per transmission, or 11.5 characters, which typically is not enough for the data you are sending.  Additionally, sending the time in Ascii adds another digit to the transmission length at the 10 second and 100 second marks, which may account for the observed slowdown in the "Test ARDUINO" scenario.  (The slowdown in the Matlab case may be due to rapidly-growing memory use by Matlab; possibly try less-accurate plotting, or narrower plots, or a specialized graphing program.)
Edgar Bonet's code sidesteps this problem by transmitting data from a fixed-length (8 byte) buffer in binary, hence the 80000 bps figure he mentions (8 bytes times 10 bits per serially-transmitted byte times 1000 readings per second).  Since ADC readings are timer-triggered, there probably is no real need to transmit a clock time with each reading, but resynchronization (in case of lost bytes) might need to be added somehow.  Possibly one could send a 9-byte packet at each transmission; the extra byte would allow use of a protocol to support error-detection, resynchronization, periodic clock readings, and packet counts.

I am not suggesting timer-based interrupts, I suggest using the “auto-trigger” feature of the ADC to trigger the conversion on a timer event. This is based on the timer interrupt flag (i.e. event detection) and works irrespective of whether the corresponding interrupt is enabled or not.

@EdgarBonet, I've edited accordingly

@jwpat7 That's what I was doing, I did the time and voltage conversions in Matlab since I will be plotting my data there. I added the division to Arduino's sketch just when I was trying to find out if the jitter was from Arduino's side or from Matlab. I edited the question to explain what I mean on the screenshot. As you can see the freuqency starts high then it gets lower. Even worse, the readings from Matlab (when I read via Matlab the second time) have lower frequency with the same decreasing frequency after a while.

I know Matlab is not the best option, I'm open to use a faster multi channel plotting tool if it's going to help but I need to make sure that the results are solid from Arduino's side first.

@Isra, I edited answer.  Note, if you don't have a convenient way to read binary data, to have enough serial throughput for Ascii data, slow things down to 800 or 750 Hz instead of 1000, or try some of the higher bps settings like 250000 or 1000000.  However, general purpose PCs may have trouble keeping up at those rates.

I tried to go for 250000 baudrate and 500us sampling period (to get 500 Hz frequency) but the results were in the range of 900 Hz and also alternating between different values in this range. I've used Leonardo and Pro Micro to test several times, each trial gave me different results. It's strange that the frequency is in the 900HZ range when I set it for a lower range, what am I missing?

@Isra, 500 μs = 2000 Hz :)

That was a horrible mistake! I changed to 2000us and I managed to get a consistent 500 Hz! I guess it's not meant to handle that high frequencies. We'll have to see if 500Hz will give us similar results after the data is processed, if yes, then we can work with the 500 Hz. Now I need to plot it but when I send it to Matlab, data is mixed and I get so many different frequencies. Any suggestions to a fast and good tool to realtime plot and log the 4 channels in an excel or txt file?

Expanding on my previous comments...
The only reliable way to get jitter-free ADC readings is to trigger the
ADC from a timer event. I wrote the following program to demonstrate the
approach on an Arduino Uno, as I don't have a Leonardo. The program
configures the Timer 1 to set it's overflow flag every 250 µs, and
the ADC to start a conversion on every rising edge of that flag. When
the conversion is done (104 µs after it started), the ADC ISR
fires. It's job is to retrieve the conversion result and set the analog
multiplexer for the next channel, switching round-robin through channels
0 – 3.
volatile int adc_readings[4];
volatile bool fresh_readings;

// Used only to clear the interrupt flag.
EMPTY_INTERRUPT(TIMER1_OVF_vect);

ISR(ADC_vect)
{
    static int buffered_readings[3];
    static uint8_t channel;

    if (channel == 3) {

        // "Export" the 4 readings at once.
        adc_readings[0] = buffered_readings[0];
        adc_readings[1] = buffered_readings[1];
        adc_readings[2] = buffered_readings[2];
        adc_readings[3] = ADC;
        fresh_readings = true;
    } else {

        // Buffer the current reading.
        buffered_readings[channel] = ADC;
    }

    // Move to next channel.
    channel = (channel + 1) % 4;
    ADMUX = _BV(REFS0)  // reference = Vcc
          | channel;
}

void setup()
{
    Serial.begin(115200);
    Serial.write("================");

    // Configure the ADC.
    ADMUX  = _BV(REFS0)   // reference = Vcc
           | 0;           // channel = ADC0
    ADCSRA = _BV(ADEN)    // enable
           | _BV(ADSC)    // start first conversion
           | _BV(ADPS0)   // prescaler = 128
           | _BV(ADPS1)   // ...ditto
           | _BV(ADPS2);  // ...ditto
    ADCSRB = _BV(ADTS1)   // trigger source = Timer 1 overflow
           | _BV(ADTS2);  // ...ditto

    // Wait for and discard the first conversion.
    loop_until_bit_is_set(ADCSRA, ADIF);

    // Configure Timer 1 to overflow every 250 us.
    TCCR1A = _BV(WGM11);  // fast PWM mode, TOP = ICR1
    TCCR1B = _BV(WGM12)   // ...ditto
           | _BV(WGM13)   // ...ditto
           | _BV(CS11);   // clock at F_CPU/8
    TIMSK1 = _BV(TOIE1);  // enable overflow interrupt
    ICR1   = 500 - 1;     // period = 500 * 8 CPU cycles

    // Set the ADC to auto-trigger mode.
    ADCSRA |= _BV(ADATE)  // enable auto-trigger
            | _BV(ADIF)   // clear interrupt flag
            | _BV(ADIE);  // enable interrupt
}

void loop()
{
    if (fresh_readings) {
        Serial.write((uint8_t *) adc_readings, sizeof adc_readings);
        fresh_readings = false;
    }
}

A few comments:

The Timer 1 overflow interrupt is used only to clear the timer
interrupt flag. The flag has to be cleared because the ADC is
triggered only on it's rising edges. Alternatively, that flag could be
cleared inside the ADC ISR.
I choose to deliver the readings by batches of 4 (one reading per
channel), thus the buffering inside the ADC ISR. The choice is
arbitrary: you can, if you wish, transmit the readings as they come,
as suggested in jwpat's answer.
The very first ADC reading is manually started and discarded. The only
reason for this is that it takes longer (200 µs instead of 104).
The readings are transmitted in binary form at 115.2 kb/s. The
effective data rate is 80 kb/s. ASCII would not be possible at
this sampling rate.
The loop() function must never take more than 1 ms to execute,
otherwise there is a risk of it reading the adc_readings array while
the ADC ISR is updating it, which would lead to corrupt data.
Protecting the access to adc_readings inside a cli()/sei() pair
would not significantly ease the timing constraint, as a long loop()
execution would still carry the risk of loosing data points.

From a cursory look at the ATmega32u4
datasheet
it would seem it's Timer 1 is identical to the one of the ATmega328P
powering the Uno, save for an extra PWM channel. The ADC, however, is
slightly different, but similar enough to make porting fairly
straightforward. At least the setting of ADMUX would be different.

I have an Uno so I can try this and see how it works. I have little eperience in programming Atmega in the low level but I understood what you meant. One poit which wasn't clear to me though is 4th comment. I know that the ADC's readings re 10 bits each, so I will need to transmit 2 bytes for every reading and then reconstruct them in the recieving end (in my case Matlab). wouldn't that be slower than sending ASCII?

What you have here is a classic case of the measurement of something changing what is measured.
I guessed when I saw your question that the time taken to transmit all that serial data would be longer than 1 ms. If you really were transmitting at 115200 baud then it would take 2.6 ms to send the 30-odd bytes you are sending (30 * 1/11520). Since you are using a Leonardo you get USB speeds which is faster but not infinitely fast.
I could reproduce your figures when I tested.
However if I comment out the two lines that send the time:
//    Serial.print(((double)currentMicros) / 1000000UL, 5 ); // 1 is the number of decimals to print
//    Serial.print('\t');

And also measure separately by toggling pin 13:
  if ((currentMicros - previousMicros) >= INTERVAL_LENGTH_US)
  {
    previousMicros += INTERVAL_LENGTH_US;
    digitalWrite (13, !digitalRead (13));

Then I found that pin 13 toggled at exactly once per millisecond.
(edit) Close enough. See screenshot of logic analyser (0.996 ms):

That is only 4 µs out - within error range for the processor clock.
Uncommenting the timing code I found that for a short time I still got 1 ms per iteration, but it then dropped down to around 1.04 ms (ie. 960 Hz). I am guessing that some USB buffer, somewhere, is filling up and not being emptied as fast as you would like.
Here is the screenshot of the code which sends the time (1.0366 ms):

Notice how simply sending the number of microseconds has increased the time between toggling the pin?

It is helpful to have extra hardware to hand to measure things like this. Using the processor that you are trying to time, to send its own timing information is fraught with problems.

Can you explain to me how you found the frequency by toggling a digital pin? is there a counter that does that?.....I tried to go for 250000 baudrate and 500us sampling period (to get 500 Hz frequency) but the results were in the range of 900 Hz and also alternating between different values in this range. I've used Leonardo and Pro Micro to test several times, each trial gave me different results.

See amended answer.

I tried to go for 250000 baudrate - on the Leonardo and Micro the baud rate does not affect the results. I set the baudrate to 1 (lol) and still got the same results. The Serial output goes to the USB interface, bypassing the concept of baud rates.


text:
What's the difference between all the Software Serial Libraries? Which one is Arduino Nano compatible?
There are so many Software Serial Libraries, what are the differences between them? And which one I can use with my Arduino Nano? For my Arduino Nano I need one Hardware Serial Port and one Software Serial port at a baud rate of 115200.
On which pins of the Arduino Nano can I use the software serial library?

This answer lists the 4 basic choices:

HardwareSerial, always the best.  Simply use the pre-defined Serial variable.  On some Arduinos, there are extra HardwareSerial ports, called Serial1, Serial2, etc.  The Nano only has Serial.
AltSoftSerial, the best of the software serial libraries.  Only one instance is allowed, and it is must be used on one of the Input Capture pins (pins 8 & 9 for a Nano).
My NeoSWSerial is next best.  It works on any two pins, but only at baud rates 9600, 19200 and 38400.
SoftwareSerial is the worst choice.  It works on any two pins, but it is very inefficient.  It disables interrupts for the entire time that a character is being sent OR received, and it cannot do both at the same time (unlike all the other serial choices).  This can interfere with other parts of your sketch, other device communications, or with libraries.

If you could use a slower baud rate, you could keep your debug prints on Serial, and use either AltSoftSerial or NeoSWSerial for your device.
But if you have to use 115200, the only reliable choice is Serial.  Although AltSoftSerial and SoftwareSerial allow that baud rate, they may not send/receive data correctly.
In that case, I would suggest using Serial for the 115200 device.  Unless you are using an ISP, you will have to disconnect the device to upload a new sketch over USB.
Then use AltSoftSerial or NeoSWSerial for your debug statements.  You will need a TTL Serial-to-USB adapter (aka FTDI) on the selected pins to connect to the PC.  The Serial Monitor or other Terminal emulator program will read from that USB COM port.

Is AltSoftSerial the same in performance and characteristics, only that NeoICSerial adds the functionality of what? I did not understand completely. https://github.com/SlashDevin/NeoICSerial

@WilliamRoy, NeoICSerial just adds the attachInterrupt method to AltSoftSerial.  You probably don't need to handle each character in an interrupt.  Just use AltSoftSerial and call the available() and then read() if a char is ready.  Similarly, NeoHWSerial adds the same attachInterrupt feature to the core HardwareSerial.  However, NeoSWSerial is a completely different class (it also has the attachInterrupt feature).

"SoftwareSerial is the worst choice". I think at the moment it is improved, based on Neo. If you have time please update the answer. Thanks!

both NeoSW and AltSoft cannot work with ISR vectors.


text:
I2C conflict with 2 Arduinos
I am having some trouble connecting my 2 Arduino Nanos with I2C. I have this configuration:

SLAVE RECEIVER Nano with DS1037 and LCD 16x02 on I2C.
MASTER SENDER Nano.

After some time the Slave receiver prints on LCD random chars. If I delete the line including library RTCLIB.h everything is working except the clock. Thanks for your help :D
Slave receiver code:

#include <Wire.h> // include la libreria
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"

LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
RTC_DS1307 RTC; // orologio

static int anno;
static int giorno;
static int mese;
static int ora;
static int secondo;
static int oldSecondo;
static int minuto;
static int oldMinuto;
static int annoGrezzo;

//------------------------------------------------------------
// si ripete la union esattamente come nel master
union Scomp_float {
  // si utilizza union x float
  float temp;
  char byte_s[4];
} S_float;

//------------------------------------------------------------
// si ripete la union esattamente come nel master
union Scomp_long {
  // si utilizza union x long
  long int mioL;
  char byte_sL[4];
} S_long;

//------------------------------------------------------------
//------------- byte temporanei
byte f0 = 0;   // contengono i byte ricevuti
byte f1 = 0;
byte f2 = 0;
byte f3 = 0;
byte a1 = 0;
//-------------------------------------------------------------
//  qui si introdurranno i dati ricomposti dopo la trasmissione
long int masterLong = 0;
float masterFloat = 0;
int masterInt = 0;
//--------------------------------------------------------------

void setup() {
  Wire.begin(80);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  lcd.begin(16, 2);
  Serial.begin(9600);           // start serial for output
  //-----------orologio-----------
  RTC.begin();
  // Check to see if the RTC is keeping time.  If it is, load the time from your computer.
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // This will reflect the time that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  //-----------fine orologio-----------
}

void loop() {
  //------------------------------------------------------------
  //   si stampano i dati ricevuti e ricomposti
  //------------------------------------------------------------
  Serial.println(masterInt);
  Serial.println(masterLong);
  Serial.println(masterFloat);
  DateTime now = RTC.now();
  annoGrezzo = now.year();
  anno = ( annoGrezzo % 100);
  mese = now.month();
  giorno = now.day();
  ora = now.hour();
  minuto = now.minute();
  lcd.setCursor(0, 0);
  lcd.print(masterInt);
  lcd.setCursor(0, 1);
  lcd.print(giorno);
  lcd.print(" ");
  lcd.print(mese);
  lcd.print(" ");
  lcd.print(anno);
  lcd.print("  ");
  lcd.print(ora);
  lcd.print(":");
  lcd.print(minuto);
  delay (1000);
  lcd.clear();
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  if (howMany == 2) {
    // se in arrivo due dati
    f0 = Wire.read(); // si leggono i byte
    f1 = Wire.read();
    masterInt =  ((f1 << 8) | f0); // si ricompone l'intero
  }

  if (howMany == 4) {
    // se in arrivo 4 dati è un float
    f0 = Wire.read();  // si leggono i byte
    f1 = Wire.read();
    f2 = Wire.read();
    f3 = Wire.read();
    S_float.byte_s[0] = f0; // si ricompone il float
    S_float.byte_s[1] = f1;
    S_float.byte_s[2] = f2;
    S_float.byte_s[3] = f3;
    masterFloat = S_float.temp;
  }

  if (howMany == 5) {
    // se in arrivo 5 dati è un long
    f0 = Wire.read();  // si leggono i byte
    f1 = Wire.read();
    f2 = Wire.read();
    f3 = Wire.read();
    a1 = Wire.read();
    S_long.byte_sL[0] = f0;  // si ricompone  il long
    S_long.byte_sL[1] = f1;
    S_long.byte_sL[2] = f2;
    S_long.byte_sL[3] = f3;
    masterLong = S_long.mioL;
  }
}

Master sender code:
#include <Wire.h> // si include la libreria
//---------------------------------------------------------
union Scomp_float {
 // si utilizza union x float
  float temp;
  char byte_s[4];
} S_float;
//---------------------------------------------------------
union Scomp_long {
 // si utilizza union x long
  long int mioL;
  char byte_sL[4];
} S_long;
//----------------------------------------------------------
// questi sono i dati che si vogliono trasmettere

long int mioLong = 1234567801;
float mioFlo = 123456.29;
//----------------------------------------------------------
byte a1 = 0; // conterrà i primo byte dell'int
byte a2 = 0; // conterrà il secondo byte dell'int
byte f0 = 0; // contengono i byte del float e del long
byte f1 = 0;
byte f2 = 0;
byte f3 = 0;
void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}

void loop() {
  int mioInt = random(30000);
 //-----------------------------------------------------
 // trasmissione di un intero ( 2 byte)
 //-----------------------------------------------------
  a1 = byte(mioInt); // si scompone l'intero in 2 byte
  a2 = byte( mioInt >> 8);
  Wire.beginTransmission(80); // inzia trasmissione allo slave

  Wire.write(a1); //  invia un byte
  Wire.write(a2); //  invia il secondo byte
  Wire.endTransmission(); // stop transmitting
 //------------------------------------------------------
  delay (2000);
 //------------------------------------------------------
 // trasmissione intero lungo 4 byte
 //------------------------------------------------------
  S_long.mioL = mioLong; // si scompone in 4 byte
  f0 = S_long.byte_sL[0];
  f1 = S_long.byte_sL[1];
  f2 = S_long.byte_sL[2];
  f3 = S_long.byte_sL[3];
  a1 = 1; // si trasmette questo byte solo per segnalare allo slave
 // che stiamo trasmettendo un long
 // una volta scomposto si trasmettono i 4 byte
  Wire.beginTransmission(80); // trasmette allo slave
  Wire.write(f0); // invia un byte
  Wire.write(f1); //
  Wire.write(f2); //
  Wire.write(f3); //  invia un byte
  Wire.write(a1);
  Wire.endTransmission(); // fine trasmissione
 //-------------------------------------------------------
  delay (2000);
 //------------------------------------------------------
 //   trasmissione di un float 4 byte
 //------------------------------------------------------
  S_float.temp = mioFlo; // si scompone in 4 byte
  f0 = S_float.byte_s[0];
  f1 = S_float.byte_s[1];
  f2 = S_float.byte_s[2];
  f3 = S_float.byte_s[3];
 // una volta scomposto si trasmettono i 4 byte
  Wire.beginTransmission(80); // trasmette allo slave
  Wire.write(f0); // invia un byte
  Wire.write(f1); //
  Wire.write(f2); //
  Wire.write(f3); //  invia un byte
  Wire.endTransmission(); // fine trasmissione
 //-------------------------------------------------------
  delay (3000);
}
//------ end master ----------------------------------

AFAIK you can't control the I2C if you are a slave. Slaves only respond to the master's requests, but can't start a communication (for instance to an LCD). If you want your slave to control the LCD screen, then make another I2C bus where the slave is the master, or make the master control the LCD directly. Or connect the master and the slave through another bus (SPI, serial, ...) and make the slave the new master...

can help me to convert this to Master reader/Slave receiver i try without result... tnks for help

I don't know what your problem is, but you have a gross error at LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);. You have a I2C LCD display but you are using it as an old-style LCD initialization.
The correct initialization for and I2C LCD display is
LiquidCrystal(0x2A, 16, 2);

You have to replace 0x2A for the correct I2C address of your LCD display (Use a I2C scanner sketch for that).
You have to check the API of your libraries first (usually a .h file):
    /**
     * Constructor
     *
     * @param lcd_addr  I2C slave address of the LCD display. Most likely printed on the
     *                  LCD circuit board, or look in the supplied LCD documentation.
     * @param lcd_cols  Number of columns your LCD display has.
     * @param lcd_rows  Number of rows your LCD display has.
     * @param charsize  The size in dots that the display has, use LCD_5x10DOTS or LCD_5x8DOTS.
     */
LiquidCrystal_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t charsize = LCD_5x8DOTS);

i partially resolve the problem with this:
MASTER RECEIVER code:
#include <Wire.h> // include la libreria
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"

LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
RTC_DS1307 RTC; // orologio

static int anno;
static int giorno;
static int mese;
static int ora;
static int secondo;
static int oldSecondo;
static int minuto;
static int oldMinuto;
static int annoGrezzo;
char t[10] = {};
char v[10] = {};

void setup()
{
  Wire.begin();                // join i2c bus with address #4
  lcd.begin(16, 2);
  Serial.begin(9600);           // start serial for output

  //-----------orologio-----------

  RTC.begin();
  // Check to see if the RTC is keeping time.  If it is, load the time from your computer.
  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
    // This will reflect the time that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  //-----------fine orologio-----------

}

void loop()
{
  //------------------------------------------------------------
  //   si stampano i dati ricevuti e ricomposti
  //------------------------------------------------------------
  Wire.requestFrom(8, 3);
  int i = 0;
  while (Wire.available()) {
    t[i] = Wire.read(); // every character that arrives it put in order in the empty array "t"
    i = i + 1;
  }
  Wire.requestFrom(8, 3);
  i=0;
  while (Wire.available()) {
    v[i] = Wire.read(); // every character that arrives it put in order in the empty array "v"
    i = i + 1;
  }
  Serial.println(t);
  DateTime now = RTC.now();
  annoGrezzo = now.year();
  anno = ( annoGrezzo % 100);
  mese = now.month();
  giorno = now.day();
  ora = now.hour();
  minuto = now.minute();
  lcd.setCursor(0, 0);
  lcd.print(t);
  lcd.setCursor(5,0)
  lcd.print(v);
  lcd.setCursor(0, 1);
  lcd.print(giorno);
  lcd.print(" ");
  lcd.print(mese);
  lcd.print(" ");
  lcd.print(anno);
  lcd.print("  ");
  lcd.print(ora);
  lcd.print(":");
  lcd.print(minuto);

  delay (1000);
  lcd.clear();
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()

SLAVE SENDER code:
#include <Wire.h> // si include la libreria
//---------------------------------------------------------

char t[10];
char v[10];

void setup()
{
  Wire.begin(8); // join i2c bus (address optional for master)
   Wire.onRequest(requestEvent);
}

void loop()
{
 int mioInt = random(30000);
 int mioInt2 = 1234;
  dtostrf(mioInt, 3, 2, t); //convers the float or integer to a string. (floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, empty array);
  dtostrf(mioInt2, 3, 2, v);
//------------------------------------------------------
delay (500);
}
void requestEvent() {
  Wire.write(t);
  Wire.write(v);
}
//------ end slave    ----------------------------------

now my problem is the second variable T im unable to receive it on Master. Thanks for help

Only the Master is now the Master and the RTC, the I2C display and the Slave Nano are all Slaves. That is good, there will be no more collisions on the I2C bus. You have to know what kind of data you want to transmit. A zero terminated string is not the best option. You could send two integers at once or a struct with values. It is easier to send a single data package. In the Slave, the requestEvent() is run from an interrupt. Therefor the variabeles that you send must be made 'volatile' and the if they are used in the loop() the interrupts have to be turned off temporarily in the loop().

how? im a little bit confused, with one no problem its the second the problem.. i understand how it work with one. thanks for reply

For example with "int x[2];". Transmit them with "Wire.write((char *)x, 4);". In the Master, request 4 bytes, use also a "int x[2];" and you could use "Wire.readBytes((char *)x, 4);" to read the data into the integers. The problem you have is not the second one, it is the way you use the two strings. Therefor a single package will be better.

If this solved your problem please accept your own answer, other Stack Exchange considers the question not answered appropriately.

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


text:
I2C wire.h send 2 variables
I am trying to send 2 variables with wire.h. I tried with  this code:
MASTER RECEIVER:

#include <Wire.h> // include la libreria
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"

LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
RTC_DS1307 RTC; // orologio

static int anno;
static int giorno;
static int mese;
static int ora;
static int secondo;
static int oldSecondo;
static int minuto;
static int oldMinuto;
static int annoGrezzo;
char t[10] = {};
char v[10] = {};

void setup() {
  Wire.begin(); // join i2c bus with address #4
  lcd.begin(16, 2);
  Serial.begin(9600); // start serial for output
  //-----------orologio-----------
  RTC.begin();
  // Check to see if the RTC is keeping time. If it is, load the time from your computer.
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // This will reflect the time that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  //-----------fine orologio-----------
}

void loop() {
  //------------------------------------------------------------
  //   si stampano i dati ricevuti e ricomposti
  //------------------------------------------------------------
  Wire.requestFrom(8, 6);
  int i = 0;
  while (Wire.available()) {
    Serial.println(i);
    t[i] = Wire.read();
    // every character that arrives it put in order in the empty array "t"
    i = i + 1;
  }
  Serial.println(t);
  DateTime now = RTC.now();
  annoGrezzo = now.year();
  anno = (annoGrezzo % 100);
  mese = now.month();
  giorno = now.day();
  ora = now.hour();
  minuto = now.minute();
  lcd.setCursor(0, 0);
  lcd.print(t);
  lcd.setCursor(5,0);
  lcd.print(v);
  lcd.setCursor(0, 1);
  lcd.print(giorno);
  lcd.print(" ");
  lcd.print(mese);
  lcd.print(" ");
  lcd.print(anno);
  lcd.print("  ");
  lcd.print(ora);
  lcd.print(":");
  lcd.print(minuto);
  delay (1000);
  lcd.clear();
}

SLAVE SENDER
#include <Wire.h> // si include la libreria
//---------------------------------------------------------
char t[10];
char v[10];

void setup() {
  Wire.begin(8);
  // join i2c bus (address optional for master)
  Wire.onRequest(requestEvent);
}

void loop() {
  int mioInt = random(30000);
  int mioInt2 = 1234;
  dtostrf(mioInt, 3, 2, t);
  //convers the float or integer to a string. (floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, empty array);
  dtostrf(mioInt2, 3, 2, v);
  //------------------------------------------------------
  delay (500);
}

void requestEvent() {
  Wire.write(t);
  Wire.write(v);
}

I'm unable to receive v in my master receiver. Thanks for your help.

I don't have the library to hand, but I think reading requires you to request a certain number of bytes from an I2C address.  You don't appear to be doing that.  Suggest you look more closely at a working I2C library and see how they do it.

You're requesting 6 bytes when you should be requesting 20, if I understand it correctly, and there is no code in the Master to write to v.

I2C is 8-bit data bus, where address can be 7 bit or 10 bit, you can't send more than 8 bit at a time, if you want to send data more than 8 bit you will be sending them like this, for example let you need to send 16 bit data then you send 8 bit LSB first and then 8 bit MSB or vice versa as per your configuration.
Secondly I had noticed you code and noticed many errors, the major one is that you hadn't followed the rules of I2C protocol in your code. I am here attached a link for sample program of arduino i2c slave and master. Go through it you will get your answer.


text:
Arduino IDE Can't find the USB Port
I'm on a Mac, running 10.11.6 and Arduino IDE 1.8.1.
I've been prototyping some projects using an Adafruit Trinket. I was not having any problems, until at some point my Mac stopped recognizing the trinket. Somewhere within that time, I installed the Digispark board and tested that as well. I also may have made some updates to the OS version.
I've tried many things to fix it including installing the FTDI drivers, plugging and unplugging. Restarts etc. Nothing seems to fix the issue. The board shows up in the system profiler, as well as USB Prober, but does not show as an option in the Arduino IDE. I've also tried using several different cables but no dice.
It's really frustrating, I'm not sure what else I can do to track the issue down.

Try changing your USB cable and sometimes cables can be charge only. You can check this by plugging in another board. If you change cables make sure that cable works with at least one other board.

Did you ensure the board is in Bootloader mode? https://learn.adafruit.com/introducing-trinket/starting-the-bootloader

This probably isn't relevant but: There was an issue with the FDTI chip that converts USB to serial.  The manufacturer enabled something in the drivers that prevented the Chinese clone chips from working.  Is it possible you were using an old version of the drivers that didn't have this feature and you have upgraded to the new version?  (That said I would have though Adafruit would have been using real chips, unless its an Adafruit copy)


text:
In ESP-12E NodeMCU, what's the pin number of A0?
In ESP-12E NodeMCU, all digital pins can be called with a number. Here is the list:
static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

So these two lines of code will do the same:
pinMode (D0, INPUT)
pinMode (16, INPUT)

What's the corresponding number for A0 (the only analog input of this board)?

The numeric value of A0 is 17, as defined here.
But unlike on an Arduino board, you can't use it as a digital pin. It only work for pin numbers 0-16.
If you are using as a parameter for analogRead, the numeric values would be 0 and 17, as seen here.
And as such, these three calls would do the same:
analogRead(A0);
analogRead(17);
analogRead(0);


text:
Can I hook up an Analog multiplexer to Arduino Nano?
I want to increase the analogue inputs for the Nano. So is it possible to hook up a CD74HC4067 analog multiplexer to it?
Thanks.

Yes, it is. Just follow one of the myriad of tutorials that show you how, or find plenty of questions on here that deal with it.

This answer discusses that exact chip.

yes. those using IO expanders offers more flexibility.

IO expanders are for digital, not analog.


text:
Reverting Arduino Environment on OS X
I am using Arduino environment 1.6.17 on OS X, 10.12.2. In this version, all of the core avr library stuff is located in ~/Library/Arduino15.
I was told by a vendor that in order for their program to compile properly, I needed to compile it under 1.6.4 of the Arduino environment. I dowloaded that version and compiled the program again. I could tell though, by the compiler messages, that while the IDE may be version 1.6.4, the program was still compiling with version 1.6.17 tools and avr libraries. Is there anyway to change that so I can get it to actually compile with all tools and libs from 1.6.4?

Send the product back for a refund if the vendor isn't going to provide proper support. Arduino IDE 1.6.4 is outdated and it would take very little effort for the vendor to make it compatible with current versions.

I didn't try the total removal of the environment and then a re-installation as JRobert suggested, because I didn't really want to nuke my current environment. But his post did inspire me to try and run the 1.6.4 environment from another user account that had never run any Arduino stuff before. This seemed functionally equivalent to his or her suggestion. That seemed to work. Although, I still have a feeling that there is a more convenient solution out there.

You can be selective about the files you allow an app remover to delete. If you've modified your Preferences.txt, for example, don't delete it, and/or pull a copy of it aside somewhere for safety. But you're on the right track - something isn't being down-graded that needs to be, when you install the older version.

Try removing the Arduino app with AppCleaner or a similar application remover. AppCleaner should find any related files not contained within the app. Let it remove them. Then reinstall Arduino 1.6.4.

When you run a recent-ish version of the Arduino IDE, it installs stuff in ~/Library/Arduino15, unless it already exists. So when you installed 1.6.4, it saw the ~/Library/Arduino15 folder and proceeded without installing its own version of the tools.
The only way to have your 1.6.4 running alongside a newer version is to have copies of the ~/Library/Arduino15 folder, by renaming them when appropriate.


text:
How to select arduino 101 in ubuntu?
How to I select arduino 101? I  have ubuntu.

In that ancient version of the IDE? You don't.
You download and install the latest version of the IDE from the Arduino website, then use the Boards Manager to install the packages that include the Ardiuno 101.

This is ubuntu, most of the software is older... I don't know!

There hasn't been a new package submitted to the repos for decades. You download it manually from Arduino.cc

I had the same problem in Gentoo - also had to instal Arduino manually, but it is easy, so do not worry about it and just do it :)

The default serial port it shows up in for me is ttyACM0
You can set this as your default port, so when you open the IDE with sudo arduino
~ $ vim ~/.arduino/preferences.txt

change the value for serial.port
serial.port=ttyACM0


text:
How to compile a sketch using arduino-builder?
How do you compile a sketch with a custom library path using arduino-builder? The docs are a little light.
I have my source code organized like this:
lib
    SomeLib
        somelib.h
        somelib.cpp
src
    otherfile.h
    main.ino

I've installed Arduino 1.8.1 into /usr/share/arduino.
I'm trying to compile this for an Arduino Uno from my project root directory with:
arduino-builder -compile -hardware /usr/share/arduino/hardware -tools /usr/share/arduino/hardware/tools -libraries lib -fqbn arduino:avr:uno -build-path .build src/main.ino

But it returns this error:
fork/exec {runtime.tools.ctags.path}/ctags: no such file or directory

What's causing this error?

You need to specify the path to the tools-builder folder, which is where ctags is. Try this:
arduino-builder \
  -compile \
  -hardware /usr/share/arduino/hardware \
  -tools /usr/share/arduino/hardware/tools \
  -tools /usr/share/arduino/tools-builder \
  -libraries lib \
  -fqbn arduino:avr:uno \
  -build-path .build \
  src/main.ino

Is there a way to upload the compiled program to the Arduino using arduino-builder or some other cli tool?

This seems to do the trick for me:  avrdude -V -F -C /etc/avrdude.conf -p atmega328p -P /dev/ttyACM0 -c arduino -b 115200 -U flash:w:sketch.hex


text:
Will M2.5 screws fit the holes in the Arduino Uno?
I'm developing a sump pump monitor/alarm setup that will include an Arduino Uno as well as a Raspberry Pi. Both boards will be mounted to a piece of wood. The Arduino is designed to take M3 screws, but the Pi is designed to take M2.5 screws. The difference in these two screw sizes is 0.5mm (2.4mm M2.5 vs 2.9mm M3). Is this going to be close enough to be able to use the smaller M2.5 screws for both? Due to cost reasons, I want to avoid having to purchase two different sizes of screws and standoffs.

With the screw holes for the Arduino being the larger screw holes, the 2.9mm screws would fit, but would probably need a nut and/or a washer to properly secure things. In the end if you intend to be doing more work with Arduinos and Pis in the future that require screwing things together then just go and get proper screw sizes, you won't regret it.

Yep, that sounds like a pretty reasonable approach. I was linked to this http://www.spaenaur.com/pdf/sectionR/R11.pdf by someone else and it looks like the head of the M2.5 is sufficiently wide at 4.5mm to not fall through in any case.


text:
How to set limits in Arduino of pin x to remain in high state?
I am working on this thermostat, which has a predefined int that sets pin high and low. But now I want to give it a particular range on which the pin 7 stays high, let's say from 24°C to 26°C, because I don't want my relay clicking every second.
const int sensor=A1; // Assigning analog pin A1 to variable 'sensor'
int relay=7;
float tempc;  //variable to store temperature in degree Celsius
float vout;  //temporary variable to hold sensor reading

void setup() {
    pinMode(sensor,INPUT); // Configuring pin A1 as input
    Serial.begin(9600);
}

void loop() {
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius
    Serial.print("temprature in C is ");
    Serial.print(tempc);
    Serial.println();
    delay(2000);
    if (tempc <24) {
      digitalWrite(7,HIGH);
      Serial.print("heating is on as " );
    } else {
      digitalWrite(7,LOW);
      Serial.print("heating is off as ");
    }

Do you mean that you want hysteresis so that the relay goes ON when the temperature goes below 24, but only goes OFF again when the temperature goes above 26?

Your question is not very clear, but I assume, like Mark Smith, that you
want your thermostat to be
hysteretic between 24°C and
26°C. The algorithm is as follows:

If the heater is OFF and you get a temperature reading below 24°C,
then you switch it ON.
If the heater is ON and you get a reading above 26°C, then you switch
it OFF.
In any other case, you leave the heater alone: it will stay in
whatever state you have previously set it.

Translated to C++:
// Pinout.
const uint8_t sensor = A1;  // temperature sensor
const uint8_t heater = 7;   // heater connected through a relay

void setup()
{
    pinMode(sensor, INPUT);
    pinMode(heater, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    static bool heaterOn;
    float temperature = 500.0 / 1024 * analogRead(sensor);

    // Switch ON if the temperature drops below 24 Celsius.
    if (!heaterOn && temperature < 24) {
        Serial.println(F("Switching the heater ON."));
        digitalWrite(heater, HIGH);
        heaterOn = true;
    }

    // Switch OFF if the temperature rises above 26 Celsius.
    else if (heaterOn && temperature > 26) {
        Serial.println(F("Switching the heater OFF."));
        digitalWrite(heater, LOW);
        heaterOn = false;
    }
}


text:
Wemos D1R2 - Onewire 2 DS18B20 passing to variable
I'm a beginer, did some tests with Uno and now have a Wemos D1R2 on my hands...
I'm building a sort of weather station that will send data to thingspeak.com and log this data there.
I managed to merge 2 examples together (1 to send data to thingspeak) and (1 to read 1 DS18B20 with onewire)
I now need to connect 2 or more DS18B20 sensors on the same Onewire BUS (I also managed to do this using this tutorial  http://arduino-info.wikispaces.com/Brick-Temperature-DS18B20#mult ), the readings come alright on serial monitor but when I try to send the readings to thingspeak a get a error:

temp_logger_multime_sensors_2:174:  error: statement cannot resolve
  address of overloaded function

Serial.print printTemperature(Probe01);
My piece of code:

/* References
    DHT11 sensor data to Thingspeak using a Wemos D1
    http://www.esp8266learning.com/dht11-sensor-data-to-thingspeak-using-a-wemos-d1.php#codesyntax_3

   Rui Santos - Complete project details at http://randomnerdtutorials.com

   YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
   http://arduino-info.wikispaces.com/Brick-Temperature-DS18B20#mult
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
*/

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// replace with your channel’s thingspeak API key and your SSID and password
String apiKey = "THINGSPEAKKEY";
const char* ssid = "WIFINAME";
const char* password = "WIFIPASSWORD";
const char* server = "api.thingspeak.com";

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 5

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress Probe01 = { 0x28, 0xFF, 0x51, 0xA5, 0x61, 0x16, 0x03, 0x92 }; // probe 1
DeviceAddress Probe02 = { 0x28, 0xFF, 0xF8, 0x99, 0x52, 0x16, 0x04, 0xB1 }; // probe 2

WiFiClient client;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // start serial port to show results
    Serial.begin(115200);
  delay(10);

/*  Serial.begin(9600);
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  */
  // Initialize the Temperature measurement library
  sensors.begin();

 WiFi.begin(ssid, password);

  // Connecting to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected on IP ...");
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
}

/*-----( Declare User-written Functions )-----*/
  void printTemperature(DeviceAddress deviceAddress)
  {
  float tempC = sensors.getTempC(deviceAddress);
     if (tempC == -127.00)
     {
     Serial.print("Error getting temperature  ");
     }
     else
     {
     Serial.print(tempC);
     }
  }// End printTemperature

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(1000);
  Serial.println();
  Serial.print("Number of Devices found on bus = ");
  Serial.println(sensors.getDeviceCount());
  Serial.print("Getting temperatures... ");
  Serial.println();

  // Command all devices on bus to read temperature

  sensors.requestTemperatures();

float StrProbe01 = printTemperature(Probe01); //THINK PROBLEM IS HERE
float StrProbe02 = printTemperature(Probe02); //AND HERE

  Serial.print("Probe 01 temperature is:   ");
    printTemperature(Probe01);  //<-- THIS PART WORKS
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
    printTemperature(Probe02);

  Serial.println();

if (client.connect(server,80)) {
  String postStr = apiKey;
  postStr +="&field1=";
  postStr += float(StrProbe01);
  postStr +="&field2=";
   postStr += float(StrProbe02);
  postStr += "\r\n\r\n";

  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  client.print("Content-Type: application/x-www-form-urlencoded\n");
  client.print("Content-Length: ");
  client.print(postStr.length());
  client.print("\n\n");
  client.print(postStr);

  Serial.print("Temperature: ");
  Serial.print printTemperature(Probe01);
  Serial.print(" degrees Celsius Humidity: ");
  Serial.print printTemperature(Probe02);
  Serial.println("Sending data to Thingspeak");
  }
  client.stop();

  Serial.println("Waiting 20 secs");
  // thingspeak needs at least a 15 sec delay be een updates
  // 20 seconds to be safe
  delay(20000);

}//--(end main loop )---

I suspect I have to transform printTemperature(Probe01); into a variable first (as you can see on the code) and use this variable on postStr += StrProbe01; as  but I tried as you can see on the code and it didnt work either.
* UPDATE * The code below works sending data to thingspeak and serial monitor fine with 1 DS18B20
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// replace with your channel’s thingspeak API key and your SSID and password
String apiKey = "THINGSPEAKKEY";
const char* ssid = "WIFINAME";
const char* password = "WIFIPASSWORD";
const char* server = "api.thingspeak.com";

const char* t2 = "5";
const char* h2 = "12.34";

//START DS18B20 Stuff
// Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5
#define ONE_WIRE_BUS 5  // DS18B20 pin

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

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature DS18B20(&oneWire);
char temperatureCString[6];
char temperatureFString[6];

//END DS18B20 stuff

WiFiClient client;

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

// dht.begin();
DS18B20.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement

WiFi.begin(ssid, password);

// Connecting to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected on IP ...");
// Printing the ESP IP address
Serial.println(WiFi.localIP());
}

void getTemperature() {
  float tempC;
  float tempF;
  do {
    DS18B20.requestTemperatures();
    tempC = DS18B20.getTempCByIndex(0);
    dtostrf(tempC, 2, 2, temperatureCString);
    tempF = DS18B20.getTempFByIndex(0);
    dtostrf(tempF, 3, 2, temperatureFString);
    delay(100);
  } while (tempC == 85.0 || tempC == (-127.0));
}

// runs over and over again
void loop()
{

 getTemperature();

if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(temperatureCString);
postStr +="&field2=";
postStr += String(temperatureFString);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temp in C: ");
Serial.print(temperatureCString);
Serial.print(" and Temp in F: ");
Serial.print(temperatureFString);
Serial.println("Sending data to Thingspeak");
}
client.stop();

Serial.println("Waiting 20 secs");
// thingspeak needs at least a 15 sec delay be een updates
// 20 seconds to be safe
delay(20000);
}

And this code works fine displaying 2 DS18B20 on serial monitor connected to 1 Onewire BUS, but I need to implement (uncomment) the thingspeak part now.
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// replace with your channel’s thingspeak API key and your SSID and password
String apiKey = "THINGSPEAKKEY";
const char* ssid = "WIFINAME";
const char* password = "WIFIPASSWORD";
const char* server = "api.thingspeak.com";

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 5

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress Probe01 = { 0x28, 0xFF, 0x51, 0xA5, 0x61, 0x16, 0x03, 0x92 }; // probe 1
DeviceAddress Probe02 = { 0x28, 0xFF, 0xF8, 0x99, 0x52, 0x16, 0x04, 0xB1 }; // probe 2

WiFiClient client;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // start serial port to show results
    Serial.begin(115200);
  delay(10);

  // Initialize the Temperature measurement library
  sensors.begin();

 WiFi.begin(ssid, password);

  // Connecting to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected on IP ...");
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());

}  //--(end setup )---

/*-----( Declare User-written Functions )-----*/
  void printTemp(DeviceAddress deviceAddress)
  {
  float tempC = sensors.getTempC(deviceAddress);
     if (tempC == -127.00)
     {
     Serial.print("Error getting temperature  ");
     }
     else
     {
     Serial.print(tempC);
     }
  }// End printTemp

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(1000);
  Serial.println();
  Serial.print("Number of Devices found on bus = ");
  Serial.println(sensors.getDeviceCount());
  Serial.print("Getting temperatures... ");
  Serial.println();

  // Command all devices on bus to read temperature

  sensors.requestTemperatures();

  Serial.print("Probe 01 temperature is:   ");
    printTemp(Probe01);
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
    printTemp(Probe02);

  Serial.println();

/*
if (client.connect(server,80)) {
  String postStr = apiKey;
  postStr +="&field1=";
  postStr += float(StrProbe01);
  postStr +="&field2=";
   postStr += float(StrProbe02);
  postStr += "\r\n\r\n";

  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  client.print("Content-Type: application/x-www-form-urlencoded\n");
  client.print("Content-Length: ");
  client.print(postStr.length());
  client.print("\n\n");
  client.print(postStr);

  Serial.print("Temperature: ");
  Serial.print printTemp(Probe01);
  Serial.print(" degrees Celsius Humidity: ");
  Serial.print printTemp(Probe02);
  Serial.println("Sending data to Thingspeak");
  }
  client.stop();

  Serial.println("Waiting 20 secs");
  // thingspeak needs at least a 15 sec delay be een updates
  // 20 seconds to be safe
  delay(20000);
 */
}//--(end main loop )---

It has all to do on generate a sort of variable (perhaps float , im not sure) and passing it as a http string
Thanks

Your question would benefit greatly from a [http://stackoverflow.com/help/mcve](minimal, complete, verifiable example) since the problem you have is with code you haven't posted.  It would also be helpful to post the version (again, minimal, complete and verifiable) which does work.

@MarkSmith sorry about that, I tried to shorten the code but get that would be hard to simulate.. here goes the whole code (I tried to clear as much as possible) the problem start ater sensors.requestTemperatures(); when I add the Thingspeak section

In the code that works, you have:
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(temperatureCString);
postStr +="&field2=";
postStr += String(temperatureFString);
postStr += "\r\n\r\n";

In the code that does not work you are doing this:
if (client.connect(server,80)) {
  String postStr = apiKey;
  postStr +="&field1=";
  postStr += float(StrProbe01);
  postStr +="&field2=";
   postStr += float(StrProbe02);
  postStr += "\r\n\r\n";

Print out the both the Strings that you are sending, postStr, using Serial.print. Compare the result. I'm not at home at the moment to try it out, but I'm sure the float(StrProbe01) and float(StrProbe02) is wrong. Thingspeak explains in their guidelines what the string that you are sending should look like.

Since the DeviceInformation variables are not POD (Plain Old Data) it would be slightly more efficient to use references or pointers, this will avoid copying them around.  If you do that you could make you global variables Probe01 and Probe02 const variables.
In loop around line 98 you call printTemperature() and assign the return value to a float.  Which is very strange, because around line 72 you define void printTemperature().  So I suspect there may be a function in the Dalas library call printTemperature that returns a float and this is the overloaded function it is talking about.
So to fix it change the name of you printTemperature function and change the two lines it is called on.  Hopefully that will fix this problem.

Thanks @Matt , but like I said I am starting in the world of C and arduino, I am barelly merging samples from internet together and trying to make them work :)
I updated my post with 2 codes that I got running, 1 code read ONE temp sensor and display on serial monitor and also send to thingspeak platform
another code is able to read 2 sensors on the same bus, but I cannot manage to get this 2 readings to send over thingspeak

Possibly can use :: notation to qualify printTemperature(), ie to indicate which namespace to use.  See eg namespace and name lookup at cppreference.com

Re your edit : I'd create a new project and put just the commented out code in setup (and the minimum you need to get it working) and then post the problems you have with that.
I can't see any compile problems, but the data you are sending to thingspeak doesn't look right.  I think you are sending:
String postStr = apiKey + "&field1=" + float(StrProbe01) + "&field2=" + float(StrProbe02) + "\r\n\r\n";

And I think you should be sending:
String postStr = "field1=" + float(StrProbe01) + "&field2=" + float(StrProbe02) + "\r\n\r\n";

(Based on this example Arduino.cc)

Try returning float from printTemp:
 /*-----( Declare User-written Functions )-----*/
  float printTemp(DeviceAddress deviceAddress)
  {
  float tempC = sensors.getTempC(deviceAddress);
     if (tempC == -127.00)
     {
     Serial.print("Error getting temperature  ");
     }
     /* else
     {
     Serial.print(tempC);
     }
     */ you won't be needing this
     return tempC;
  }// End printTemp

And call it as you would in your commented example.
I suspect that it is not the most efficient way, but it should work.


text:
Can't get deep sleep to work on Wemos D1 ESP8266
I have a Wemos D1 (ver. 2015-08) and I am trying to get it to sleep. I've managed to get it generally up and running/got wifi to work etc, but the deep sleep function is beyond me.
I tried connecting RX<-D0 pin to RESET, also tried with a 10k resistor. Neither worked. I then tried D2 (listed as gpi016 on the back of the board) to RESET, this also didn't work. Any ideas? It seems like this would be an incredible chip if I could get it to sleep. The example sketch below uploads fine and contacts the serial monitor, up until the point it sleeps.
 /* Deep Sleep - Blink
 *
 * Blinks the onboard LED, sleeps for 10 seconds and repeats
 *
 * Connections:
 * D0 -- RST
 *
 * If you cant reprogram as the ESP is sleeping, disconnect D0 - RST and try again
 */

// sleep for this many seconds
const int sleepSeconds = 5;

void setup() {
  Serial.begin(9600);
  Serial.println("\n\nWake up");
  pinMode(BUILTIN_LED, OUTPUT);
  // Connect D0 to RST to wake up
  pinMode(D0, WAKEUP_PULLUP);
  // LED: LOW = on, HIGH = off
  Serial.println("Start blinking");
  for (int i = 0; i < 20; i++) {
    digitalWrite(BUILTIN_LED, LOW);
    delay(100);
    digitalWrite(BUILTIN_LED, HIGH);
    delay(100);
  }
  Serial.println("Stop blinking");
  Serial.printf("Sleep for %d seconds\n\n", sleepSeconds);
  // convert to microseconds
  ESP.deepSleep(sleepSeconds * 1000000);
  Serial.println("sleep finished");
}

void loop() {
}

Could you say a few more words about what you actually observed, what you expected to observe, and how they differ? Have you tried disconnecting everything from the serial port of the ESP8266, as suggested by one tutorial? (Alas, this makes debugging much more difficult).

definitely you gpio16 (D2), as that is the pin that is used for waking up from deepsleep ... note Serial.println("sleep finished"); won't be executed after the chip wakes up, the ESP will basically shutdown, then reboot after deepsleep period ...

oh, GPIO16 is D0 on the weemos d1 mini  and D2 on the "non" mini ...why can't they just label GPIO16 as GPIO16!!!

One question about your board What board you must to select for the Wemos 2015-08 under arduino 1.8.5?? Wemos D1 R1 or Wemos D1 R2/mini???

If you need deep sleep, you should not use boards with USB-to-serial chips which cannot be sent to sleep. Think about using a "naked" ESP-12f and an external USB UART which only will be connected for programming.

I had to physically connect D0 to RST to wake up the wemos.

the confusion may be that ... D0 on the Weemos d1 mini is gpio16 ... D2 on the Weemos d1 is gpio16

Welcome.  Please be sure to take the tour at https://arduino.stackexchange.com/Tour

In fact it works well using a wire between D2 and RESET pins. But sending the program to the board, you must first disconnect these pin (the arduino IDE will send a hard reset during process, so this D2 reset must not be in place). When the program is uploaded to the ESP and start running, you connect D2 and RESET and it works well, ESP will go to deep sleep mode as expected.
Because it is not a mini, the board has another chip (usb) that will not got to sleep mode, so on my board it will use about 17mA in deep sleep (compare with 80mA in wifi normal operation). Still worth the trick.


text:
decomposition long number to byte and recomposition With FRAM
I'm using Adafruit FRAM for my little project and have some trouble with recomposition of LONG Variable , right now the recomposition on INTEGER it's fine.
Where I'm wrong with recomposition of LONG number? Thank's for reply :D
#include <SPI.h>
#include "Adafruit_FRAM_SPI.h"

/* Example code for the Adafruit SPI FRAM breakout */
uint8_t FRAM_CS = 10;

//Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS);  // use hardware SPI

uint8_t FRAM_SCK = 13;
uint8_t FRAM_MISO = 12;
uint8_t FRAM_MOSI = 11;
//Or use software SPI, any pins!
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS);

uint16_t          addr = 0;

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

void setup(void) {
#ifndef ESP8266
  while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
#endif

  Serial.begin(9600);

  if (fram.begin()) {
    Serial.println("Found SPI FRAM");
  } else {
    Serial.println("No SPI FRAM found ... check your connections\r\n");
    while (1);
  }
  int interoDaScrivere = 12345;
  long longDaScrivere = 123456789;
  byte a1 = 0;
  byte a2 = 0;
  byte intPt0 = 0;
  byte intPt1 = 0;
  byte longPt1 = 0;
  byte longPt2 = 0;
  byte longPt3 = 0;
  byte longPt4 = 0;
  a1 = byte(interoDaScrivere);        // si scompone l'intero in 2 byte
  a2 = byte( interoDaScrivere >> 8);
  //decomposition Long

  byte b1 = (longDaScrivere);
  byte b2 = (longDaScrivere >> 8);
  byte b3 = (longDaScrivere >> 16);
  byte b4 = (longDaScrivere >> 24);

  //--------------

  // Read the first byte
  uint8_t test = fram.read8(0x0);
  Serial.print("Restarted "); Serial.print(test); Serial.println(" times");

  // Test write ++
  fram.writeEnable(true);
  fram.write8(0x0, test + 1);
  fram.writeEnable(false);
  /*
    fram.writeEnable(true);
    fram.write(0x1, (uint8_t *)"FTW!", 5);
    fram.writeEnable(false);
  */

  fram.writeEnable(true);
  fram.write8(1, a1);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(2, a2);
  fram.writeEnable(false);

  fram.writeEnable(true);
  fram.write8(3, b1);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(4, b2);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(5, b3);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(6, b4);
  fram.writeEnable(false);

  //ricomposizione INT
  int interoRicomposto = 0;
  intPt0 = fram.read8(1);
  intPt1 = fram.read8(2);
  //INT RECOMPOSITION
  interoRicomposto =  ((intPt1 << 8) | intPt0);
  // ricomposizione LONG
  long longRicomposto = 0;
  longPt1 = fram.read8(3);
  longPt2 = fram.read8(4);
  longPt3 = fram.read8(5);
  longPt4 = fram.read8(6);
 //LONG RECOMPOSITION
  longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24));

  Serial.print("A1 F0 ");
  Serial.print(a1);
  Serial.print(" ");
  Serial.println(intPt0);
  Serial.print("A2  F1 ");
  Serial.print(a2);
  Serial.print(" ");
  Serial.println(intPt1);
  Serial.println(interoRicomposto);

  Serial.print("b1 LongPt1 ");
  Serial.print(b1);
  Serial.print(" ");
  Serial.println(longPt1);
  Serial.print("b2 LongPt2 ");
  Serial.print(b2);
  Serial.print(" ");
  Serial.println(longPt2);
  Serial.print("b3 LongPt3 ");
  Serial.print(b3);
  Serial.print(" ");
  Serial.println(longPt3);;
  Serial.print("b4 LongPt4 ");
  Serial.print(b4);
  Serial.print(" ");
  Serial.println(longPt4);

  Serial.println(longRicomposto);

}

void loop(void) {

}

Looks like you got your shifting backwards in the recombining?

I'm trying after decomposition in byte to recombine it in a new variable with values readed in the FRAM sorry for my english

Yes, I gathered that. Look at which portion of the long you are storing in address 3 (for example) and then look at where you are placing what you read from address 3 in the recombined long. Looks to me like you take it from the lower 8 bit, then recombine it into the uppermost 8 bits.

I'm trying in this line longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24)); but I'm doing something wrong :S thanks for reply

the arduino write to me 23303 and not 123456789

I am sorry, I think the language difference is getting in the way. I am telling you what is wrong, and you are just replying with what you have already written. I know what you have already written, I have already read it.

excuse me for that, can you write an example code I'm really sorry for boring you

There is one very obvious error in your code: You are writing the long
number LSB (least significant byte) first: the LSB goes at address 3 of
the FRAM, while the MSB (most significant byte) goes at address 6. You
reconstruct the number with
longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24));

which puts longPt1 (contents of address 3) as the most significant
byte. Thus you are swapping all the bytes around.
Then there is a more subtle error. longPt1 is a byte. According to the
C++ promotion rules, it is implicitly promoted to int before any
arithmetic or logical operation. But since an int is 16-bits, you are
shifting all the bits out when you do longPt1 << 24. You should cast
the numbers to a 32-bit type before shifting, as follows:
long longRicomposto = (uint32_t) longPt1 <<  0
                    | (uint32_t) longPt2 <<  8
                    | (uint32_t) longPt3 << 16
                    | (uint32_t) longPt4 << 24;

Note that I am casting to an unsigned type. Shifting a 1 into the bit
sign of an integer is undefined behavior in C++: the shift must be done
with an unsigned type, then, if needed, cast to a signed type.

You should cast the numbers to a 32-bit type before shifting, as follows: it is advisable to use brackets liberally in cases like that. don't leave any doubts there as to how you want the shifts to be done.

There is actual a member function in the Adafruit_FRAM_SPI library that solves all of this. There is no need for all this shifting.
int interoDaScrivere = 12345;
long longDaScrivere = 123456789;

fram.write(1, (const uint8_t*) &interoDaScrivere, sizeof(interoDaScrivere));
fram.write(3, (const uint8_t*) &longDaScrivere, sizeof(longDaScrivere));

And
fram.read(1, (uint8_t*) &interoDaScrivere, sizeof(interoDaScrivere));
fram.read(3, (uint8_t*) &longDaScrivere, sizeof(longDaScrivere));

Cheers!
PS: Might need to adjust depending on the number of bytes/bits and address.

Let's cut out all the rubbish from your code and just look at the relevant lines:
byte b1 = (longDaScrivere);
byte b2 = (longDaScrivere >> 8);
byte b3 = (longDaScrivere >> 16);
byte b4 = (longDaScrivere >> 24);

fram.write8(3, b1);
fram.write8(4, b2);
fram.write8(5, b3);
fram.write8(6, b4);

longPt1 = fram.read8(3);
longPt2 = fram.read8(4);
longPt3 = fram.read8(5);
longPt4 = fram.read8(6);

longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24));

If we now compress it even more by removing the fram stuff and just assigning direct from b? to the equivalent longPt? you can see even better what is happening:
byte b1 = (longDaScrivere);
byte b2 = (longDaScrivere >> 8);
byte b3 = (longDaScrivere >> 16);
byte b4 = (longDaScrivere >> 24);

longPt1 = b1;
longPt2 = b2;
longPt3 = b3;
longPt4 = b4;

longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24));

Now if we remove the b? variables completely and just assign direct to longPt? we get:
longPt1 = (longDaScrivere);
longPt2 = (longDaScrivere >> 8);
longPt3 = (longDaScrivere >> 16);
longPt4 = (longDaScrivere >> 24);

longRicomposto = ((longPt4 << 0) + (longPt3 << 8) + (longPt2 << 16) + (longPt1 << 24));

And if we then split the last line up so it looks more like the first block:
longPt1 = (longDaScrivere);
longPt2 = (longDaScrivere >> 8);
longPt3 = (longDaScrivere >> 16);
longPt4 = (longDaScrivere >> 24);

longRicomposto  = (longPt4 << 0);
longRicomposto |= (longPt3 << 8);
longRicomposto |= (longPt2 << 16);
longRicomposto |= (longPt1 << 24);

Hopefully from that it is now clear where you have gone wrong.
You take the lowest 8 bits (longDaScrivere) and shift them 24 places to the left ((longPt1 << 24)), and apply that to the recombined long.
In effect, you start with 4 bytes ABCD and reverse them through bit shifting - 0 goes to 24, 8 to 16, 16 to 8 and 24 to 0, to end up with DCBA.


text:
Using multiple HC-SR04: sharing trigger pin and interference between HC-SR04
If I have four HC-SR04 detectors, can they share the same trigger pin on the Arduino?  This would reduce the number of pins needed from eight to five.
Even if they can share the same trigger, maybe this doesn't make sense because the emitted signal from one HC-SR04 will give a false reading to the others.  If this will occur, how much time should the sketch pause before triggering/reading the next HC-SR04?
Thanks.

No, you can't really share the same trigger pin, for the reasons you have already worked out. You can, though, share the same echo pin, since you know which HC-SR04 has been activated by the trigger.
To do that you will need a "wired or" circuit. The simplest method is a diode per ECHO signal and a pull-down resistor:

simulate this circuit – Schematic created using CircuitLab


text:
Is there anything special about Arduinos?
I've been using the stm32f4 discovery up until now but I've heard Arduinos are easier to use. While looking for an Arduino Genuino Uno Rev3 board, I was told to get a knock off instead.
Is there any real difference between Arduino boards and other knock off micros with similar specs?
I really just want to get a feel for what it's like using an 'easier' micro. It doesn't need to be extra special.

Are you sure you are looking for an "adruino" ?

"Arduino" is not the name of a microcontroller family. So you are trying to compare apples with oranges (Arduinos are microcontroller boards; most of the Arduino boards are based on Atmels ATmega controllers but there are also ones with an ARM controller)

You might want to look into this if you are interested in knowing the background of Arduino. Namely how it was ""adapted"" from Wiring.

Arduino have "standardized connectorss" for witch are many shields created. Also many manufacturers marks anything vaguely in power range of 5V as "for Arduino", because Arduino is so popular. And that is main reason, that Arduino is so popular. (This and that it came in the righ time with equipement targeting to be easy for beginners and a lot of educational materials).
I am using Arduino just because I start with it and bought a lot of AVR based boards and chips, but I may switch to STM for higher computing power, more pins and better equipement.
And the Arduino IDE is easy for beginners (and popular between them), because it have nearly nothing in it and it can compile+upload code to Arduino with just 2 buttons. (But it is like notepad with few more buttons - if you are a little more advanced you will run away for better editor/IDE/whatever at all cost. I use Vim + Make to create and run Arduino code and avoid the IDE totally, many do that too.)
So if you are used to STM32F4 kit, it would be big step back for you and it would satisfy you only if you need something cheap for work, where your STM is total overkill.

Just as a reference, STM boards are actually cheaper than the genuine arduino boards ;) plain microcontrollers and the smallest compatible boards are even cheaper, but plain microcontrollers are not so user friendly

This is Google Trend of Arduino (blue) versus four other boards: STM32 (red), Teensy (green), MSP430 (yellow) and PIC32 (magenta).
That means lot of people using Arduino, making hardware and software, writing blogs, answering questions. That's the biggest plus of Arduino.

I think the reason why people say Arduino is easier to use is because it has a nice IDE and code upload is rarely troublesome.
Especially, the IDE allows the user to code hardware related functions with simplified functions.
For example, when coding an Arduino Sketch, the user doesn't really have to bother about registers.
The Arduino family are based on Atmel chips, so when it comes to raw hardware performance, Arduino performances are identical to Atmel chips.
If you are familiar with what you have(STM32F4) and don't want to make things easier but rather challenging, you don't have to move to Arduino.
Of course, if you want a simple & fast prototyping, nothing beats Arduino. (<- my personal opinion)

"Nice IDE"? I find it terrible.

@EugeneSh.yeah well for beginners, it could be. Since they don't have to configure complicated options and all that.

I agree with @EugeneSh., and even as a beginner I despised it. I quickly moved on to PIC and MPLab, and since have done work in Atmel Studio on AVRs and so on. MUCH better! Gives you a lot more freedom and more tools to work with


text:
Noise from the I2C - digital potentiometer
I have a digital potentiometer controlled by Arduino, connected via the I2C bus (it controls volume). For some reason it beeps just by itself, even when no audio is playing. I have checked that it is def. I2C, as when I unplug the SDA&SCL then there is no noise (and I have many more connected to Arduino. Is there anything special about "de-noising" the I2C bus?

What do you mean by noise? what is the audio you mentioned? plrease add more details (at least the schematic), because I know that it is perfectly clear in your mind but if you can't communicate us the details we can't figure out what the problem can be

Best thing to do is take the I2C speed outside the audible range (if its not already there), try to put some distance between the I2C lines and speaker input lines and add an RC low-pass filter centred around 20kHz to the speaker's lines.

Hi, thanks for the idea, but what you exactly mean by "take the I2C speed outside the audible range"?

Or where to put it? just in front of the speaker?

My suggestion may have been premature. Best you include your schematic and code so we can get a better idea of your problem.

Are you saying a digital potentiometer is making an audible sound?  Have you got pull up resistors in your circuit for the I2C bus?

I have replaced the I2C with SPI in the end, as nothing helped with removing this noise :(