text:
Different ways (and the fastest) to compute sines (and cosines) in Arduino
I am using an Arduino Uno board to compute the angles of my system (robotic arm). The angles are actually 10 bit values (0 to 1023) from the ADC, using the full range of the ADC. I am only going to be operating in the 1st quadrant (0 to 90 deg), where both sines and cosines are positive, so there is no problem with negative numbers. My doubts can be expressed in 3 questions:

What are the different ways to compute these trigonometric functions on Arduino?
What is the fastest way to do the same?
There are the sin() and cos() functions in the Arduino IDE, but how does the Arduino actually calculate them (as in do they use look-up tables, or approximations etc.)? They seem like an obvious solution, but I would like to know their actual implementation before I try them out.

PS: I am open to both standard coding on the Arduino IDE and assembly coding, as well as any other options not mentioned. Also I have no problems with errors and approximations, which are inevitable for a digital system; however if possible it would be good to mention the extent of possible errors

Would you be ok with approximate values?

Yes actually, but I would like to know the extent of the error of different methods. This is not a precision product but a side project of mine. Actually approximations are inevitable for almost any (if not any) digital system implementing a mathematical function

I assume you are wanting to work in degrees. Are you going to want to input integers or decimal numbers for the angle?

Degrees yes. I think it would be easier to write code and test if we use integers, so I would go with that. I ll put more clear information about edits

For just 90 (integer) degrees a 90-entry lookup table would be fastest and most efficient. In fact for the full 360 degrees you can use a 90-entry lookup table. Just read it backwards for 90-179 and invert it for 180-269. Do both for 270-359.

Could you quantify your accuracy requirement? The approximation cos(π/2x) ≈ 1−x² has a maximal error of 5.6e-2. And (1−x²)(1−0.224x²), which costs 3 multiplications, is good to within 9.20e-4.

@EdgarBonet Sorry for the late reply. I dont have any current quantified fixed accuracy. I just want to know all possible options for now

The two basic methods are mathematical calculation (with polynomials) and lookup tables.
The Arduino's math library (libm, part of avr-libc) uses the former. It is optimised for the AVR in that it is written with 100% assembly language, and as such is almost impossible to follow what it's doing (there's zero comments as well). Rest assured though it will be the most optimised pure-float implementation brains far superior to ours could come up with.
However the key there is float. Anything on the Arduino involving floating point is going to be heavyweight by comparison to pure integer, and since you are only requesting integers between 0 and 90 degrees a simple lookup table is by far the simplest and most efficient method.
A table of 91 values will give you everything from 0 to 90 inclusive. However if you make that a table of floating point values between 0.0 and 1.0 you still then have the inefficiency of working with floats (granted not as inefficient as calculating sin with floats), so storing a fixed point value instead would be far more efficient.
That may be as simple as storing the value multiplied by 1000, so you have between 0 and 1000 instead of between 0.0 and 1.0 (for instance sin(30) would be stored as 500 instead of 0.5). More efficient would be to store the values as, for instance, a Q16 value where each value (bit) represents 1/65536th of 1.0. These Q16 values (and the related Q15, Q1.15, etc) are more efficient to work with since you have powers-of-two which computers love to work with instead of powers-of-ten which they hate working with.
Don't forget as well that the sin() function expects radians, so you first have to convert your integer degrees into a floating point radians value, making the use of sin() even more inefficient compared to a lookup table that can work directly with the integer degrees value.
A combination of the two scenarios, though, is possible. Linear interpolation will allow you to get an approximation of a floating point angle between two integers. It's as simple as working out how far between two points in the lookup table you are and creating a weighted average based on that distance of the two values. For instance if you are at 23.6 degrees you take (sintable[23] * (1-0.6)) + (sintable[24] * 0.6). Basically your sine wave becomes a series of discrete points joined together by straight lines.  You trade accuracy for speed.

I wrote a library a while back that used a Taylor polynomial for sin/cos that was faster than the library. Given, I was using floating point radians as the input for both.

There are some good answers here but I wanted to add a method which hasn't been mentioned yet, one very well suited to computing trigonometric functions on embedded systems, and that's the CORDIC technique
Wiki Entry Here It can compute trig functions using only shifts and adds and a small look-up table.
Here's a crude example in C. In effect, it implements the C libraries' atan2() function using CORDIC (i.e. find an angle given two orthogonal components.) It uses floating point, but can be adapted for use with fixed-point arithmetic.
/*
 * Simple example of using the CORDIC algorithm.
 */

#include <stdio.h>
#include <math.h>

#define CORDIC_TABLE_SIZE  16

double cordic_table[CORDIC_TABLE_SIZE];

void init_table(void);
double angle(double I, double Q);

/*
 * Given a sine and cosine component of an
 * angle, compute the angle using the CORIDC
 * algoritm.
 */
double angle(double I, double Q)
{
    int L;
    double K = 1;
    double angle_acc = 0;
    double tmp_I;

    if (I < 0) {
        /* rotate by an initial +/- 90 degrees */
        tmp_I = I;
        if (Q > 0.0) {
            I = Q;           /* subtract 90 degrees */
            Q = -tmp_I;
            angle_acc = -90;
        } else {
            I = -Q;          /* add 90 degrees */
            Q = tmp_I;
            angle_acc = 90;
        }
    } else {
        angle_acc = 0;
    }

    /* rotate using "1 + jK" factors */
    for (L = 0, K = 1; L <= CORDIC_TABLE_SIZE; L++) {
        tmp_I = I;
        if (Q >= 0.0) {
            /* angle is positive: do negative roation */
            I += Q * K;
            Q -= tmp_I * K;
            angle_acc -= cordic_table[L];
        } else {
            /* angle is negative: do positive rotation */
            I -= Q * K;
            Q += tmp_I * K;
            angle_acc += cordic_table[L];
        }
        K /= 2.0;
    }
    return -angle_acc;
}

void init_table(void)
{
    int i;
    double K = 1;

    for (i = 0; i < CORDIC_TABLE_SIZE; i++) {
        cordic_table[i] = 180 * atan(K) / M_PI;
        K /= 2.0;
    }
}
int main(int argc, char **argv)
{
    double I, Q, A, Ar, R, Ac;

    init_table();

    printf("# Angle,    CORDIC Angle,  Error\n");
    for (A = 0; A < 90.0; A += 0.5) {

        Ar = A * M_PI / 180; /* convert to radians for C's sin & cos fn's */

        R = 5;  // Arbitrary radius

        I = R * cos(Ar);
        Q = R * sin(Ar);

        Ac = angle(I, Q);
        printf("%9f, %9f,   %12.4e\n", A, Ac, Ac-A);
    }
    return 0;
}

But try the native Arduino trig functions first - they might be fast enough anyway.

I have taken a similar approach in the past, on stm8. it takes two steps: 1) calculate sin(x) and cos(x) from sin(2x), and then 2) calculate sin(x +/- x/2) from sin(x), sin(x/2), cos(x), and cos(x/2) -> through iteration you can approach your target. in  my case, i started with 45 degrees (0.707), and worked my way out to the target. it is considerably slower than the standard IAR sin() function.

I have been playing a bit with computing sines and cosines on the
Arduino using fixed-point polynomial approximations. Here are my
measurements of average execution time and worst case error, compared
with the standard cos() and sin() from avr-libc:
function    max error   cycles   time
-----------------------------------------
cos_fix()   9.53e-5     108.25    6.77 µs
sin_fix()   9.53e-5     110.25    6.89 µs
cos()       2.98e-8     1720.8   107.5 µs
sin()       2.98e-8     1725.1   107.8 µs

It's based on a 6th degree polynomial computed with only
4 multiplications. The multiplications themselves are done in
assembly, as I found that gcc implemented them inefficiently. The angles
are expressed as uint16_t in units of 1/65536 of a revolution, which
makes the arithmetic of angles naturally work modulo one revolution.
If you think this can suit your bill, here is the code: Fixed-point
trigonometry.
Sorry, I still did not translate this page, which is in French, but you
can understand the equations, and the code (variable names, comments...)
is in English.

Edit: Since the server seems to have vanished, here is some info on
the approximations I found.
I wanted to write angles in binary fixed-point, in units of quadrants
(or, equivalently, in turns). And I also wanted to use an even
polynomial, as these are more efficient to compute than arbitrary
polynomials. In other words, I wanted a polynomial P() such that
cos(π/2 x) ≈ P(x2)  for x ∈ [0,1]
I also required the approximation to be exact at both ends of the
interval, to ensure that cos(0) = 1 and
cos(π/2) = 0. These constraints led to the form
P(u) = (1 − u)(1 + uQ(u))
where Q() is an arbitrary polynomial.
Next, I searched for the best solution as a function of the degree of
Q() and found this:
        Q(u)             │ degree of P(x²) │ max error
─────────────────────────┼─────────────────┼──────────
          0              │         2       │  5.60e-2
       −0.224            │         4       │  9.20e-4
−0.2335216 + 0.0190963 u │         6       │  9.20e-6

The choice among the solutions above is a speed/accuracy trade-off. The
third solution gives more accuracy than achievable with 16-bits, and
it's the one I chose for the 16-bit implementation.

That is amazing, @Edgar.

What did you do to find the polynomial?

@TLW: I required it to have some “nice” properties (e.g. cos(0)=1), which constrained to the form (1−x²)(1+x²Q(x²)), where Q(u) is an arbitrary polynomial (it's explained in the page). I took a first-degree Q (only 2 coefficients), found the approximate coefficients by fit, then hand-tuned the optimization by trial and error.

@EdgarBonet - interesting. Note that that page does not load for me, though cached works. Could you please add the polynomial used to this answer?

@TLW: added that to the answer.

You could create a couple of functions that uses linear approximation to determine the sin() and cos() of a particular angle.
I am thinking something like this:

For each I have broken the graphical representation of sin() and cos() into 3 sections and have done a linear approximation of that section.
Your function would ideally first check that the range of the angel is between 0 and 90.
Then it would use an ifelse statement to determine what of the 3 sections it belongs and then does the corresponding linear calculation (ie output = mX + c)

Will not this involve floating point multiplication?

Not necessarily. You could have it so the output is scaled between 0-100 instead of 0-1. This way you are dealing with integers, not floating point. Note: 100 was arbitrary. There is no reason that you couldn't scale the output between 0-128 or 0-512 or 0-1000 or 0-1024. By using a multiple of 2, you only need to do right shifts to scale the result back down.

Pretty clever, @sa_leinad.  Upvote.  I remember doing this when working with biasing of transistors.

I looked for other people that had approximated cos() and sin() and I came across this answer:
dtb's answer to "Fast Sin/Cos using a pre computed translation array"
Basically he computed that the math.sin() function from the math library was faster than using a look-up table of values. But from what I can tell, this was computed on a PC.
Arduino has a math library included that can calculate sin() and cos().

PCs have FPUs built into them that make it fast. Arduino's don't, and that makes it slow.

The answer is also for C# which does stuff like array bounds checking.

A lookup table will be the fastest way to find sines. And if you're comfortable computing with fixed-point numbers (integers whose binary-point is somewhere other than to the right of bit-0), your further calculations with the sines will be much faster as well. That table can then be a table of words, possibly in Flash to save RAM space. Note that in your math you may need to use longs for large intermediate results.

Just for the fun of it, and to prove it can be done, I finished an AVR assembly routine to calculate sin(x) results in 24 bits (3 bytes) with one bit of error.  The input angle is in degrees with one decimal digit, from 000 to 900 (0~90.0) for the first quadrant only.  It uses less than 210 AVR instructions and runs on average of 212 microseconds, varying from 211us (angle=001) to 213us (angle=899).
It took several days to do it all, more than 10 days (free hours) just thinking about the best algorithm for the calculation, considering the AVR microcontroller, no floating point, eliminating all possible divisions. What took more time was to make the right step-up values for integers, to have good precision it needs to step-up values of 1e-8 to binary integers 2^28 or more.  Once all the errors culprits of precision and rounding up were found, increased their calculation resolution by extra 2^8 or 2^16, the best results were met.  I first simulated all the calculations on Excel taking care of having all values as Int(x) or Round(x,0) to represent exactly the AVR core processing.
For example, in the algorithm the angle must be in Radians, the input is in Degrees to facilitate for the user.  To convert Degrees to Radians the trivial formula is rad=degrees*PI/180, it seems nice and easy, but it is not, PI is an infinite number - if using few digits it will create errors at the output, division by 180 requires AVR bit manipulation since it has no division instruction, and more than that, the result would require floating point since involves numbers far below integer 1. For example, Radian of 1° (degree) is 0.017453293. Since PI and 180 are constants, why not reverse this thing for simple multiplication?  PI/180 = 0.017453293, multiply it by 2^32 and it results as a constant 74961320 (0x0477D1A8), multiply this number by your angle in degrees, lets say 900 for 90° and shift it 4 bits right (÷16) to obtain 4216574250 (0xFB53D12A), that is the radians of the 90° with 2^28 expansion, fit in 4 bytes, without a single division (except the 4 bit shift right). In a way, the error included in such trick is smaller than 2^-27.
So, all further calculations need to remember it is 2^28 higher and taken care of it. You need to divide the on-the-go results by 16, 256 or even 65536 just to avoid it use unnecessary growing hunger bytes that would not help resolution.  That was a painstaking job, just finding the minimum quantity of bits in each calculation results, keeping the results precision around 24 bits. Each one of the several calculations where done in try/error with higher or lower bits count in the Excel flow, watching the overall quantity of error bits at the result in a graph showing 0-90° with a macro running the code 900 times, once per tenth of a degree. That "visual" Excel approach was a tool I created, helped a lot to find the best solution for every single part of the code.
For example, rounding up this particular calculation result 13248737.51 to 13248738 or just lose the "0.51" decimals, how much it will affect the final result precision for all the 900 input angles (00.1 ~ 90.0) tests?
I was able to keep the animal contained within 32 bits (4 bytes) on every calculation, and ended up with the magic to obtain precision within 23 bits of the result. When checking the whole 3 bytes of the result, the error is ±1 LSB, outstanding.
The user may grab one, two or three bytes from the result for its own requirements of precision.  Of course, if just one byte is enough I would recommend to use a single 256 bytes sin table and use AVR 'LPM' instruction to grab it.
Once I had the Excel sequence running smooth and neat, the final translation from Excel to AVR assembly took less than 2 hours, as usual you should think more first, work less later.
At that time I was able to squeeze yet more and reduce registers usage. The actual (not final) code uses around 205 instructions (~410 bytes), runs a sin(x) calculation in average of 212us, clock at 16MHz. At that speed it can calculate 4700+ sin(x) per second. Not being important, but it can run a precise sinewave up to 4700Hz with 23 bits of precision and resolution, without any lookup tables.
The base algorithm is based on Taylor series for sin(x), but modified a lot to fit my intentions with the AVR microcontroller and precision in mind.
Even that using a 2700 bytes table (900 entries * 3 bytes) would be speed attractive, what is the fun or learning experience on that?  Of course, CORDIC approach was also considered, maybe later, the point here is to squeeze Taylor into the AVR core and take water from a dry rock.
I wonder if Arduino "sin(78.9°)" can run Processing (C++) with 23 bits of precision in less than 212us and the necessary code smaller than 205 instructions. May be if C++ uses CORDIC.  Arduino sketches can import assembly code.
Makes no sense to post the code here, later I will edit this post to include a weblink to it, possibly on my blog at this url. The blog is mostly in portuguese.
This hobby-no-money venture was interesting, pushing the limits of the AVR engine of almost 16MIPS at 16MHz, without division instruction, multiplication only in 8x8 bits.  It allows to calculate sin(x), cos(x) [=sin(900-x)] and tan(x) [=sin(x)/sin(900-x)].
Above it all, this helped to keep my 63 years old brain polished and oiled. When teenagers say the 'old people' know sh*nothing about technology, I answer "think again, who do you think created the bases for everything you enjoy today?".
Cheers

Nice! A few remarks: 1. The standard sin() function has about the same accuracy as yours and is twice as fast. It is also based on a polynomial. 2. If an arbitrary angle has to be rounded to the nearest multiple of 0.1°, this can lead to a rounding error as high as 8.7e-4, which sort of negates the benefit of the 23 bit accuracy. 3. Would you mind sharing your polynomial?

I had a simillar question to OP. I wanted to make a LUT table to calculate the first quadrant of the sine function as unsigned 16 bit integers starting from 0x8000 to 0xffff. And i ended up writing this for fun and profit. Note: This would work more efficiently if i used 'if' statements. Also it's not very accurate, but would be accurate enough for a sine wave in a sound synthesiser
void sin_lut_ctor(){

//Make a Look Up Table for 511 terms of the sine function.
//Plugin in some polynomials to do some magic
//and you get an aproximation for sines up to π/2.
//

//All sines yonder π/2 can be derived with math

const uint16_t uLut_d = 0x0200; //maximum LUT depth for π/2 terms.
uint16_t uLut_0[uLut_d];        //The LUT itself.
//Put the 2 above before your void setup() as global variables.
//This coefficients will only work for uLut_d = 511.

uint16_t arna_poly_0 = 0x000a; // 11
uint16_t arna_poly_1 = 0x0001; // 1
uint16_t arna_poly_2 = 0x0007; // 7
uint16_t arna_poly_3 = 0x0001; // 1   Precalculated Polynomials
uint16_t arna_poly_4 = 0x0001; // 1
uint16_t arna_poly_5 = 0x0007; // 7
uint16_t arna_poly_6 = 0x0002; // 2
uint16_t arna_poly_7 = 0x0001; // 1

uint16_t Imm_UI_0 = 0x0001;              //  Itterator
uint16_t Imm_UI_1 = 0x007c;              //  An incrementor that decreases in time

uint16_t Imm_UI_2 = 0x0000;              //
uint16_t Imm_UI_3 = 0x0000;              //
uint16_t Imm_UI_4 = 0x0000;              //
uint16_t Imm_UI_5 = 0x0000;              //
uint16_t Imm_UI_6 = 0x0000;              //  Temporary variables
uint16_t Imm_UI_7 = 0x0000;              //
uint16_t Imm_UI_8 = 0x0000;              //
uint16_t Imm_UI_9 = 0x0000;              //
uint16_t Imm_UI_A = 0x0000;
uint16_t Imm_UI_B = 0x0000;

uint16_t Imm_UI_A = uLut_d - 0x0001;     //  510

uLut_0[0x0000] = 0x8000;        //Assume that the middle point is 32768 (0x8000 hex)
while (Imm_UI_0 < Imm_UI_A) //Construct a quarter of the sine table
  {
Imm_UI_2++;                                   //Increase temporary variable by 1

Imm_UI_B = Imm_UI_2 / arna_coeff_0;           //Divide it with the first coefficient (note: integer division)
Imm_UI_3 += Imm_UI_B;                         //Increase the next temporary value if the first one has increased up to the 1st coefficient
Imm_UI_1 -= Imm_UI_B;                         //Decrease the incrementor if this is the case
Imm_UI_2 *= 0x001 - Imm_UI_B;                 //Set the first temporary variable back to 0

Imm_UI_B = Imm_UI_3 / arna_poly_1;           //Do the same thing as before with the next set of temporary variables
Imm_UI_4 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_3 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_4 / arna_poly_2;           //And again... and again... you get the idea.
Imm_UI_5 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_4 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_5 / arna_poly_3;
Imm_UI_6 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_5 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_6 / arna_poly_4;
Imm_UI_7 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_6 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_7 / arna_poly_5;
Imm_UI_8 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_7 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_8 / arna_poly_6;
Imm_UI_9 += Imm_UI_B;
Imm_UI_1 -= Imm_UI_B;
Imm_UI_8 *= 0x0001 - Imm_UI_B;

Imm_UI_B = Imm_UI_9 / arna_poly_7          //the last set won't need to increment a next variable so skip the step where you would increase it.
Imm_UI_1 -= Imm_UI_B;
Imm_UI_9 *= 1 - Imm_UI_B;

uLut_0[Imm_UI_0] = (uLut_0[Imm_UI_0 - 0x0001] + Imm_UI_1); //Set the current value as the previous one increased by our incrementor
Imm_UI_0++;              //Increase the itterator
  }
  uLut_0[Imm_UI_A] = 0xffff; //Lastly, set the last value to 0xffff

  //And there you have it. A sine table with only one if statement (a while loop)
}

Now to get back the values, use this function.It accepts a value from 0x0000 to 0x0800 and returns the appropriate value from the LUT
uint16_t lu_sin(uint16_t lu_val0)
{
  //Get a value from 0x0000 to 0x0800. Return an appropriate sin(value)
  Imm_UI_0 = lu_val0/0x0200; //determine quadrant
  Imm_UI_1 = lu_val0%0x0200; //Get which value
  if (Imm_UI_0 == 0x0000)
  {
    return uLut_0[Imm_UI_1];
  }
  if (Imm_UI_0 == 0x0001)
  {
    return uLut_0[0x01ff - Imm_UI_1];
  }
  if (Imm_UI_0 == 0x0002)
  {
    return 0xffff - uLut_0[Imm_UI_1];
  }
  if (Imm_UI_0 == 0x0003)
  {
    return 0xffff - uLut_0[0x01ff - Imm_UI_1];
  }
}// I'm using if statements here but similarly to the above code block,
 //you can do without. just with integer divisions and modulos

Remember, this is not the most efficient approach to this task, i just couldn't figure out how to make taylor series to give out results in the appropriate range.

Your code doesn't compile: Imm_UI_A is declared twice, a ; and some variable declarations are missing, and uLut_0 should be global. With the necessary fixes, lu_sin() is fast (between 27 and 42 CPU cycles) but very inaccurate (maximum error ≈ 5.04e-2). I can't get the point of these “Arnadathian polynomials”: it seems quite a heavy computation, yet the result is almost as bad as a simple quadratic approximation. The method also has a huge memory cost. It would be way better to compute the table on your PC and put it in the source code as a PROGMEM array.

As others have mentioned lookup tables are the way to go if you want speed. I've recently been investigating computation of trig functions on an ATtiny85 for use of fast vector averages (wind in my case). There's always trade off's...for me I only needed 1 deg angular resolution so a lookup table of 360 int's (scaling -32767 to 32767, only working with int's) was the best way to go. Retrieving the sine is just a matter of supplying an index 0-359...so very fast! Some numbers from my tests:
FLASH lookup time (us): 0.99 (table stored using PROGMEM)
RAM lookup time (us): 0.69 (table in RAM)
Lib time (us): 122.31 (Using Arduino Lib)
Note these are averages across a 360 point sample for each. Testing was done on a nano.

generally, look-up table > approximation -> calculation. ram > flash. integer > fixed point > floating point. pre-calclation > real time calculation. mirroring (sine to cosine or cosine to sine) vs. actual calculation / look-up....
each has its pluses and minuses.
you can make all sorts of combinations to see which works best for your application.
edit: I did a quick checking. using 8-bit integer output, calculating 1024 sin values with look-up table takes 0.6ms, and 133ms with floaters, or 200x slower.


text:
Is setting AVR TWCR for mega328p TWEN in each function necessary?
I found this useful website explaining for to configure and implement the TWI for mega328p, but what I found not so obvious is that in the website they set TWEN in every function, while in the datasheet C code example, they only set it in START and STOP functions.
I also find another style in Arduino Wire library, that they implement more easier functions for starting and ending the module.
This is the website, could anyone take a look and tell me if setting TWEN in each function is necessary?
Also, is it necessary to check for TWINT after sending STOP command?
http://www.embedds.com/programming-avr-i2c-interface/
Regards,

The big difference is interrupt driven or busy wait implementation of the TWI hardware module protocol. Enabling interrupts is necessary in an interrupt driven implementation while busy wait should not enable interrupts.
A busy wait implementation is much simpler but it cannot be asynchronous. An interrupt driven implementation can be asynchronous and even power down the rest of the MCU during the TWI communication. It should be noted that the Arduino Wire class is interrupt driven but synchronous.
Cheers!

OK so the interrupt driven is when I enable TWIE with the GIE. And busy wait is with disabling TWIE. So my question now is when TWINT is 0? I see it 1 all the time in the 328p datasheet. And it's mentioned that you have to wait until the bit is 0. And if you want to clear it then write 1 to this bit. So what's the value of TWINT when it's set or cleared? Regards,

source: Stack Exchange

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

sort: votes


text:
How does ESP8266 work as web server with Arduino?
I am trying to learn internet of things with smart home. I will connect an ESP8266 and an Arduino with the IBM Bluemix server to control a circuit from a public IP.
I want to start with the local network for now. I have gone through a couple of links which seem like a great start http://allaboutee.com/2014/12/30/esp8266-and-arduino-webserver/
They explain how it works, and have source code but no circuit diagram. How should I connect the ESP8266 with Arduino for this example ?

#include <SoftwareSerial.h>

const byte rxPin = 2; // Wire this to Tx Pin of ESP8266
const byte txPin = 3; // Wire this to Rx Pin of ESP8266

// We'll use a software serial interface to connect to ESP8266
SoftwareSerial esp8266(rxPin,txPin);

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);
  delay(1000);
}

void loop() {
  Serial.println("Sending an AT commands");
  esp8266.println("AT");
  delay(30);
  while(esp8266.available()) {
    String data = esp8266.readStringUntil('\n');
    Serial.println("Got response from esp8266: " + data);
  }
}

The exact same way as any other ESP8266 example. The fact that you are using it as a web server doesn't change how you wire it.
For that specific code snippet it even tells you in the comments:
// make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3

In case of ESP8266 act as web server, can we connect TX/RS of ESP8200 to any digital pins of arduino ?

Like I said: the fact you're using it as a web server makes absolutely no difference to how you wire it. It's using SoftwareSerial. SoftwareSerial will work on any pins. Take your pick.

I have this code https://pastebin.com/1miy33ne but esp is not sending a message as this condition while(esp8266.available()) { always fails

I have added my pictorial representation of my project in question

How about power? How about ground? How about the other pins that are required to make the ESP8266 work? Every single Arduino tutorial for the ESP8266 shows you those.

I see. Thanks for giving hint. Lemme try :)

Another hint: Do a google image search for "ESP8266 Arduino". You will get thousands of pictures showing you how to wire it.

Yup, trying to do same..

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


text:
How does Arduino WiFi Shield connect to Blynk and How can I use ESP Module to connect to blynk?
I was actually quite impressed how easily Blynk gets connected to Arduino. So I had a doubt how does Arduino WiFi Shield allow Arduino Uno to connect to blynk ? And my second doubt is how can I use the ESP boards to connect to blynk using Windows10 ( for steps ) ?
Thank You.

how does Arduino WiFi Shield allow Arduino Uno to connect to blynk

Blynk is just a web service. The WiFi shield connects your board to the internet and sends data to that web service. The Blynk app is just an interface to that web service.

how can I use the ESP boards to connect to blynk using Windows10?

That makes no sense. You can connect an MCU to Blynk through something like an ESP8266 board (or program the ESP8266 board directly to communicate with Blynk). It is merely a network interface.
Windows 10 is not an MCU, and you already have an internet connection on it. If you want to communicate with Blynk from Windows 10 I suggest you look at the documents to see if there is an API you can use from your own programs, or a web interface you can view in your browser of choice.
Windows 10 and ESP8266 are two completely alien concepts.


text:
Arduino Mega + XBee Shield + RAMPS 1.4 Is it possible?
Is it possible to sandwich an XBee shield between Arduino Mega and Ramps 1.4 without damaging any of the boards or lose of Ramps functionality ?
I am not trying to make a 3D printer it is rather a completely different project and I am afraid of burning my XBee or losing some functionality of ramps.
Since XBee shield has the form factor of Arduino UNO I had to 3D print the rest of the shield and use some connectors to completely cover the Mega.

IF The XBee module uses different pins from the RAMPS board then yes it will "work". However with it sandwiched like that wireless performance may be rather less than desirable. You may want to, if possible, add an external antenna.

Thanks, do you think there is no risk burning the xbee shield under normal circumstances, I'm a bit affraid of the 12v used in ramps, I burned megas before but it was because of the way I wired stepper motors and lose cable connections however I do not want to burn my shield if it is preventable

The only way you would burn it is if you let power go where it shouldn't.

Yes, it would seem like folks on the RepRap forums have gotten this to work with the following setup that should work in general:

Attach the XBee shield to the RAMPS board by attaching 4 pins on AUX-1 (namely, 5V, Gnd, D1 and D0) to the 5V, Gnd, Rx and Tx pins on the XBee Shield
Switch the UART / DLINE switch on the XBee Shield to UART

Source: RepRap Forum: Printing completely wirelessly - can it be done?
This forum thread may also be useful.

Hi, thanks for the answer, one of the links you provided links to a question I asked some time ago but what is different now is that I want my xbee shield to be between Arduino Mega and Ramps rather than on top of rams as I managed to wire it and get it to work, I am afraid not to burn it if Ramps plugs into this shield

Yes, and the answer is really simple.  You need to extend the connectors between the xbee shield and the ramps shield.  Either by adding another layer of the through pin connectors you have or by resoldering some longer ones on the RAMPS board.
You might have an issue with the RF signal, sticking that much metal near the xbee board might cause problems, but I think you can get external aerials for them, if that becomes an issue.

Hi, thanks, not sure if you noticed in the picture but I already did the pin extension as you suggested, however I was looking for someone who can tell me whether there is any risk of burning the circuits of any board by doing this, also as a bonus I was hoping to find if there are any pin clashes


text:
Can I cut the pin holes off of my Arduino board?
I'm making a project, but my arduino pro micro is just slightly too big to fit in it's case.
What would happen if I cut off the pin holes that I don't need? Would it still work? Or would it be ruined?

Hello and welcome! Do you mean cutting the pins (as suggested in the questions) or cutting the printed circuit board itself (as hinted in the title)?

Cutting the printed circuit board itself

If you don't accidentally cut any other traces you'll be fine. I think most board don't snake any traces around the pins, but you'd have to check yourself, or post a detailed picture. Where did you buy your pro micro, as there are a few variations of the same board?

I bought a fake Chinese one from eBay

I guess that one could cut (or hone down) parts of the PCB. Obviosly this process devoids the warranty and there is the risk of damaging either the board or some components on it (i.e. by holding or clamping it during the process).
I would not expect any other wiring besides those connecting the pins to run in this part of the PCB. The reference layout (see board layout and using Eagle (free version)) - if the clones follow that layout - confirms that there are no other traces there. So it is mostly important to not cut any other traces and remove any chippings after the cutting to avoid short circuits.
If in doubt buy two or three boards ;) to try and repeat the process.

Rather than trying to modify the board (which I think is a bad idea) why not use a Pro Mini.
These are smaller (and cheaper), and almost identical in function, apart from the USB interface. You do need a serial interface (or ICSP) to program.

This is a bad ideas:

the PCB is made of fiber glass which, when broken, cut or filed, produces a very fine powder; any powder you might breath in during the process wil stay in lungs for a very long time, in the order of decades
the risk of cracking traces running close to the are of cut, even if they are not directly being cut, is pretty high; the copper traces rely on the fiberglass rigidity, but a crack in the fiberglass propagating under the traces is welcoming a broken trace

Go for a different case, if that's not a viable option go for a barebone arduino. If that is also a non viable option consider the idea of producing a few custom PCBs: they are not that expensive!

You could use a Pro Mini as Milliways suggested, but you don't necessarily need a USB interface to programme it.  You can use your Arduino UNO to directly programme the Arduino Pro Mini.


text:
Testing an oscilloscope with my Arduino
I bought a very old oscilloscope and although it works, I have my doubts about refreshing the screen (the electron beam fades away very slowly or is not showing up consistently across the screen).
I tested a bit with digitalWrite and anlogWrite, but this only sends either 0 or 5V out. Is there a way to make a preferably sine or triangle wave of 0 to 5V or -5 to +5V... I guess the Arduino should be capable of it, but so far I only get 0V or 5V out, nothing in between.
According to lesson 8 I should get a variable voltage of 0-5V on a pin with analogWrite, but all I get is 0 or 5V.
For what I see:

PWM gives with analogWrite a pulse signal of 0V or 5V with a duty cycle.
non PWM pins give either 0V or 5V (analogWrite of 0-127 appr: 0V, >127: 5V).

You can convert the PWM output of analogWrite() into an (approximate) analog voltage by adding a low-pass filter. That is, a resistor in series followed by a capacitor to ground.
The values to choose depend on the frequency response you want.

RC Filter Calculator (second section).

For anything more complex or accurate you will need a DAC. I often use the MCP4822 SPI DAC chip.

Thank you very much .... it worked, still the scope is very slow (meaning the old signals fade away very slowly), but it works.... guess my scope is 'kind of' usable now.
I only needed to test my oscilloscope so a 'good' DAC is not needed (yet), But I put the component in my wish list for future use :-) ... I already ordered a wave generator IC (8038) for later possible use for my project.

a few ways:
1) you can generate a square wave of a known frequency to test the time base of the scope;
2) you can generate an analog signal to test for the vertical. that can be done via pwm or dds.

Thanks ... a square wave is not really useful since it is not 'moving' vertically enough, so it's a bit hard to see if there is a problem with the scope ... I assume DDS is with the low pass filter as the accepted answer from Majenko.


text:
Can I make existing libraries use external SRAM for the Arduino
I am running out of memory on my Arduino due to the libraries I am using. After I have included all the libraries I need then there is very little in the way of free memory for my own code.
If hook up some external RAM to the Arduino, is there a way I can make existing libraries use the external RAM rather than the SRAM on the arduino or will I need to alter the code in the library to do it.
If I have to modify the code is that a relatively easy thing to do?

Hello and welcome. I would think that this is very specific to the libraries involved (could you specify them?) - albeit somewhat unlikely in a generic sense.

Thanks for the welcome :-) The ones I am thinking of at the moment are the Adafruit OLED library here and the SD card library here

What did programmers do back in the 80-ies when they run out of memory?

There is an old technique called overlays. The same area of memory is used for different phases/steps of a program. Results are naturally saved but any intermediate data is discharged before the next phase/step which reuses the "common" area of memory. Necessary state may be saved to an external storage between execution of the phase/step to increase available local memory.
To compile and link this you have to do some deep diving into the manual and read up on memory sections.
It is possible to abstract this using C++ smart pointers but getting existing libraries to use it will be a very tough challenge. There are a few attempts to support virtual memory such as virtmem.
Cheers!

Thanks. I have seen the virtmem library before in passing but took a close look and it looks like it might get me part of the way there.

Before considering using overlay, virtual memory and external memory (e.g. SPI RAM) use all the available tricks to reduce SRAM usage such as the F() macro, limit the usage of String class, reducing allocation size, packing struct's, etc.

Making a library use external SRAM is not an easy task. It can be cone, but it takes quite a bit of work.
The best start would be to abstract the SRAM into a library that provides simple access to it. Then you would need to use that new library to read and write data in the SRAM instead of accessing variables in the other libraries.
There is no way of simply mapping a variable into external SRAM - for that you need a microcontroller with an MMU  and/or an EIB.


text:
Getting an error while uploading code to Arduino when ESP8266 is attached
I am trying to use an ESP8266 and an Arduino to access the Arduino from the network. I tried to do something but I'm facing some challenges. I hope I am using the correct board "Generic ESP8266 Module".

#include <SoftwareSerial.h>

const byte rxPin = 2; // Wire this to Tx Pin of ESP8266
const byte txPin = 3; // Wire this to Rx Pin of ESP8266

// We'll use a software serial interface to connect to ESP8266
SoftwareSerial esp8266(rxPin,txPin);

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);
  delay(1000);
}

void loop() {
  Serial.println("Sending an AT commands");
  esp8266.println("AT");
  delay(30);
  while(esp8266.available()) {
    String data = esp8266.readStringUntil('\n');
    Serial.println("Got response from esp8266: " + data);
  }
}

Error
Archiving built core (caching) in: C:\Users\XYZ~1\AppData\Local\Temp\arduino_cache_322251\core\core_esp8266_esp8266_generic_CpuFrequency_80,FlashFreq_40,FlashMode_dio,UploadSpeed_115200,FlashSize_512K64,ResetMethod_ck,Debug_Disabled,DebugLevel_None_____feb0d75f3cf4e05271af4f91549cd009.a
Sketch uses 226657 bytes (52%) of program storage space. Maximum is 434160 bytes.
Global variables use 32464 bytes (39%) of dynamic memory, leaving 49456 bytes for local variables. Maximum is 81920 bytes.
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

Select the Arduino/Genuino Uno in the Arduino IDE Board menu and try again. You are simply using the wrong configuration.

First off you have the wrong board selected. It's the Arduino Uno that you are programming, not the ESP8266. Just because you have connected an ESP8266 to the Arduino doesnt't magically change what the Uno is.
Secondly you cannot have anything connected to pins 0 and 1 of the Arduino while programming. Those pins are used for communication with the PC. As a general rule it is best to never use ghem for anything.
To communicate between the Arduino and the ESP8266 you should either pick two other pins and use SoftwareSerial or invest in a board that has at least one independent UART (such as the Mega2560 or the Leonardo).

I changed Board to Arduino Uno and using pins 2 and 3 of the Arduino https://i.stack.imgur.com/PK7K9.jpg . I am unable to upload https://pastebin.com/AyUWvDxf

@Williams You have the right port selected in the IDE?

It is COM3(Arduino/Genuino Uno and I updated question

Try pressing the reset button on the Arduino just as the upload is starting.

I tried, but didn't work :(

When you press RESET does the LED on the Arduino blink 3 times very rapidly?

Let us continue this discussion in chat.

Uploading programs to the Arduino requires the RX and TX lines. The ESP8266 module is probably interfering. You will need to disconnect the ESP8266 module, or at least, don't power the module while uploading.
Connecting the ESP8266 to RX/TX pins will probably also prevent you from using the serial terminal to output debugging information.
An alternative would be to use the (not so great, but it works) SoftwareSerial library.

I am using pins 2 and 3 of the Arduino Uno https://i.stack.imgur.com/PK7K9.jpg and if you see snippet of code in question, there you will find I am using SoftwareSerial

I believe you are trying to directly program the ESP8266 and you're simply using the Arduino Uno as a USB to serial interface for your computer. There are many tutorials on the web on how to do this. First thing I see wrong with your setup is you've left the Arduino Uno MCU attached to the board. The first thing you'll want to do is remove this chip carefully (the pins can bend easily so apply force evenly). Right now when the Arduino IDE tries to talk to the ESP8266 the communication is going to both the ESP8266 and the Arduino. Physically removing the Arduino MCU chip will ensure any communication is directly with the ESP8266.
Also make sure you are connecting the correct voltage. ESP8266 can only handle 3.3V max, while the Arduino runs at 5V mostly. I believe the Uno board provides a 3.3V output but I'm not 100% sure.


text:
How to pass a string pointer to a function
I'm working on a project, where I wish to use pointers. I had a very quick overview of using pointers in C in collage, but I don't know how to use them.
I want that function uses input String (answer) as pointer.
Calling function in setup block
void setup() {
    sendCommand("AT+RST\r\n", 5000, DEBUG, "ready");
    sendCommand("AT+CWSAP_CUR=\"mega\",\"password123\",10,3\r\n", 4000, DEBUG, "OK");
}

String sendCommand(String command, const int timeout, boolean debug, String *answer)
{
    String response = "";

    Serial1.print(command); // send the read character to the esp8266

    long int time = millis();

    while ((time + timeout) > millis())
    {
        while (Serial1.available())
        {
            // The esp has data so display its output to the serial window
            char c = Serial1.read(); // read the next character.
            response += c;
        }

        //Waiting for the right answer and breaking a while loop
        if (response.indexOf(*answer) > 0)
        {
            break;
        }

    }

    if (debug)
    {
        Serial.print(response);
    }

    return response;
}

Format your code nicely. Many people simply won't bother to spend the extra time to read code if you've made it hard to read, and why should they? 2. If you don't understand well how pointers work and you want to use them as a solution to a problem, you have an XY problem that you need to fix.

Give this webpage a read, it fully explain how strings in c++ work, even down to the memory handling. https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

What you have there are not String objects, but string literals. Their type is const char *
You should avoid using String objects if at all possible. Instead use character arrays.
To get a character array out of a function it is best to create one outside your function and pass it to the function as one of the parameters. The return value of the function can then be a status value indicating if the call worked or not.

see https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/ for more info.

If you want to use the String type, have a variable String rdy="ready"; and pass &rdy (the address of the variable). Same with "OK".
I assume you want to use a pointer to avoid copying?
As you are already using C++ techniques (String is C++, not C), you might want to consider to pass rdy (not a pointer), and to write const String &answer in the sendCommand parameter list (meaning: reference to a string, which will not be modified), and omit the * when using the parameter. Passing references is quite similar to pointers: no copying involved. The additional benefit is that you may even write sendCommand("AT+RST\r\n", 5000, DEBUG, "ready"); then... the "ready" is automagically converted into a String, and passed via reference.

This is what most people are wanting as an answer. I came here looking for examples of what could be used for various Serial LCD wrappers, the ones where control characters need to be inserted and then a passed string decomposed into bytes for individual write().

You can pass parameters to functions by reference by value or 'by pointers'.
Passing a parameter by value means that the function has a separate copy of the parameter so this is more expensive in terms of memory.  It also means that any changes you make to the parameter inside the function are not reflected outside the function, they are two separate variables after all.
Passing by reference in C/C++ is done by adding a & after the parameter's type, (int& byRef).  This means that you are using the same variable inside and outside of the function.  If you want to prevent the function changing the value then you set the parameter to be a constant reference. const int& constByRef
There is a third method which is passing a pointer.  This is very useful for parameters that are a few bytes in size, i.e. strings and structures char* ptr.  When you pass a pointer it is passed by value, you can modify the pointer or the data it points to.  Since a pointer is usually only the word size of the processor it is only as expensive as passing an int parameter.
When you pass a const char* cPtr you can not modify the value of the pointer, but you can modify the value of the data pointed to.  This type of parameter will let you pass in string literals.
To make you code cleared always write functions to accept constant parameters.  If the parameter is larger than a word pass it by reference unless its an array (including char*) in which case pass it by pointer.  If in doubt it should be a constant reference to start with.  Sometimes embedded constraints affect these rules.


text:
Distance Readings Getting Manipulated When Calling Unrelated Function
I'm using a Lidar Lite v3 to measure the distance from one object to another. I pass the results to the serial monitor to see what that distance is. It appears that when I call an unrelated function, the distance that's being printed seems to be roughly subtracted by 512.
To describe exactly what I mean, this is my function:
int pulseWidth = pulseIn(monitorPin, HIGH); //begin measurement of time
int distanceConverted = (pulseWidth / 10); // 10usec = 1 cm of distance
Serial.println(distanceConverted); // Print the distance

and it gives me an output of this (which is correct):
780 cm
787 cm
790 cm
794 cm
782 cm
787 cm
790 cm
786 cm
790 cm
780 cm
788 cm
784 cm

I'm using these distances to control LED lights and a speaker, and call this function to do so:
void lights(int distanceConverted)
{
    if (distanceConverted < 1000 && distanceConverted > 500)
    {
      digitalWrite(yellowLEDPin, HIGH);
      delay(20);
      tone(8, 35000, 250);
    }

    else if (distanceConverted <= 500)
    {
      digitalWrite(redLEDPin, HIGH);
      delay(20);
      tone(8, 40000, 250);
    }
}

However, when this is called (and I call the function after I print the distance values), these are the results I get:
268 cm
267 cm
266 cm
267 cm
269 cm
267 cm
265 cm
267 cm
266 cm
266 cm
267 cm
264 cm
269 cm

When I try to account for this and multiply by three, my values become even more off and seem to be multiplied by ten:
int distanceConverted = (pulseWidth / 10)*3; // 10usec = 1 cm of distance

New Values:
2640 cm
2673 cm
2634 cm
2649 cm
2592 cm
2619 cm
2631 cm
2628 cm
2631 cm
2673 cm
2634 cm

At first I thought it might have been the delays within the lights() function manipulating the readings, but even after commenting them out I still get the same results. I don't know how the function could be throwing my values off.
Edit: I noticed at when the program is first looping, the 512 gets subtracted after the first time I run through the lights() function.
761 cm <- first value from sensor, correct
312 cm
308 cm
308 cm
309 cm
308 cm
307 cm
307 cm
309 cm
308 cm
308 cm

When I comment out the tone() function, that seems to fix my problem. Could that function be overwriting a bit in the memory register causing it to subtract 512 from my distanceConverted variable?

Messing up a single bit in a register seems quite far fetched to me. Could you also post this tone function - as you seem to indicate it being the culprit?

Tone is a standard arduino function.

The tone function uses timer interrupts to toggle a GPIO pin to make noise. That's quite intense from a CPU point of view and is most likely skewing the timing of the pulseIn function.
I would suggest not using tone if you can help it. Maybe change the frequency of a PWM output to something suitable and use analowWrite to create a 50% duty square wave.
Also learning how to use the Input Capture peripheral of the MCU (read the datasheet) will enable you to perform asynchronous sampling of your pulses and obtain much more accurate results.


text:
Can't send AT comands to ESP8266 on any baud rate
I have problem with baud rate. I have changed baud rate of esp from 115200 to 9600 with AT command AT+CIOBAUD. And everything worked fine until I wanted to change it back and executed command AT+CIOBAUD115200, after which everything I send trough arduino IDE serial monitor to ESP I get gibberish. I have tried all baud rated available in serial monitor but got nothing. Wiring stayed the same.
TX -> arduino RX
RX -> arduino TX
GND -> GND
CH_PD -> 3,3V
VCC -> 3,3V

I thought that if I can flash ESP firmware it will get back all default parameters, hence I tried flow these steps (link) but I stopped when executed esptool.py --port /dev/ttyACM0 erase_flash which got me
esptool.py v1.3
Connecting...................

A fatal error occurred: Failed to connect to ESP8266: Invalid head of packet ('\x00')

In some forums is said that to get rid of error I need to change baud rate, but nothings works for me.
Please advise how can I fix this problem.

GPIO0 has to be connected to ground to enable programming mode.

Yes, during the flashing GPO0 was connected to ground.


text:
Nested for loop errors
Ok, I am trying to make 2 LEDs run at two different speeds.
Here is the code I am using:
void setup() {

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

for ( ; ; ) {
  for ( ; ; ) {
   digitalWrite(12, HIGH);
   delay(250);
   digitalWrite(12, LOW);
   delay(250);
  }
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

This code unfortunately returns this error:
error: expected unqualified-id before 'for'
 for ( ; ; ) {

Thanks for the help!

Where is your loop function...?

where should it be?

Around your (pointless) for loops?

Like what would that code look like?

It would look like all the examples in the IDE - especially the blink example.

As Majenko's comments are suggesting you're missing the loop function here - which is not just eye candy but essential. It is the basic structure of the sketches to have two methods on the highest (user accessible) level: void setup() and void loop(). setup is run exactly once at the beginning of your program. Following that loop is looped (hence the name) forever.
This is what it should look like:
void setup() {
  // doing the setup
}

void loop() {
    for ( ; ; ) {
        for ( ; ; ) {
            // something
        }
        // something else
    }
}

However (and again Majenko's comment hints it) something else is never going to be executed as the inner loop is never exited. This tutorial (per Majenko's comment) should get you started. See also: www.arduino.cc with many more tutorials worth studying.

so how do I make it run both at the same time?

Start with the BlinkWithoutDelay example.

First of all your question is so basic it means you didn't spend any time to look any example out there, not even after Majenko has clearly pointed out you can find the answer to your question in the many examples out there.
Once that is clear here follow the code you are looking for, but using it as it is without knowing what you are doing is not going to help you. Not to mention not many will be willing to help somebody with the attitude you are demonstrating.
#define LED_1 5
#define LED_2 6

void setup() {
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
}

long one, two;
bool led_1, led_2;

void loop() {
  if (millis() - one >= 250) {
    digitalWrite(LED_1, led_1++);
    one = millis();
  }

  if (millis() - two >= 500) {
    digitalWrite(LED_2, led_2++);
    two = millis();
  }
}

One correction, never compare millis() like that. Always make >=, because it will be rarely equal to 250 or 500 and it will never run code.

You are 100% right, thanks for spotting the mistake: fix applied. For those reading the comment afterwards, I erroneously had an equality comparison == instead of a greater than comparison >= on the two if statements.

You have a compiler error because you have executable code outside a function. In particular your first "for" loop.
Second, an infinite loop inside an infinite loop is not particularly useful.

so how do I make it run both at the same time?

Ah yes, well unless you have multiple CPUs you don't run multiple things "at the same time". You do one thing, and then another thing.
I have an article about this general idea here:
http://www.gammon.com.au/blink
To save having to necessarily go to that link, here is an example from that page which blinks two LEDs at different rates:
// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup ()
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis ();
  }  // end of toggleRedLED

void loop ()
  {

  // Handling the blink of one LED.
  if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval)
    toggleRedLED ();

/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}  // end of loop

In that code, in the main loop, the code uses millis() to find the current time, and if sufficient time has elapsed it does something useful. As an analogy, when you are cooking dinner you look at the clock from time to time, and if sufficient time has elapsed since you started cooking, you take the roast from the oven and eat it.


text:
Arduino MotorShield code running but not motors
I have a motor shield V2 on my Arduino, i compile the codes just fine and i'm trying to just run the example dc motor test. The code compiles the serial is printing out the "tick tock ...." but the motors are not running. They ran once before on my mac though.
Using a 9v battery to power a basic Dc motor.
I'm using the right ports on pc. Same exact port for motor as the code tells. I'm not sure where i am wrong. TYIA

We are not sitting beside you and we can't look at your code over your shoulder... You know that right?
You must post your code using the appropriate formatting and describe or draw a diagram of your wiring: pictures are not helping.
How are you powering the shield?
What type of motor are you trying to drive? You spoke of motors (plural) but I believe you have only one motor connected.

Neither can we see where those wires go. This isn't Bladerunner.

My code has no errors and compiles fine. It also did work before. I'm just wondering if there is something wrong with my set up that i'm not noticing or if there are PC specific differences, since this did run fine on my mac. I Will add photos of what the wires go to.

Do i need to provide more descriptions? sorry about earlier =(

Your mac has been blessed by fairy dust and is using its supernatural powers to force your midichlorians spin the motor...
Option 1: battery voltage is not enough to spin the motor
Option 2: battery is wired the other way around
Option 3: one of those taped connections has simply got lose and circuit is open
Option 4: motor is wired the other way around
Option 5: motor is dead
Option 6: shield is dead
Option 7: arduino is dead
Option 8: the universe is playing against you
Option 9: I can keep going, the number of things you could have done wrong is probably endless

lmaooooo so helpful. Nah the motor runs more than fine when connected straight to the battery. So battery and dc motor are not dead. The battery is wired right other wise the LED would just not even go on. The taped connections are also not lose, because again the LED light would not turn on. Motor wired around or not would only change the direction of rotation and i did switch them previously just in case. Arduino is not dead as it runs other stuff just fine. Thanks anyways.

Turns out the pins connecting the shield to the Arduino are quite loose and didn't "touch" the Arduino.

That was option 42 on my list of possible failures

I had that on one Arduino once - the actual header sockets were inferior quality and some contacts didn't have the springiness they should have. I resorted to replacing all the sockets on the board. I think Arduino had a bad batch at one point in their history, since I haven't had the issue since with any other Arduino.

Using a 9v battery to power a basic Dc motor.

Don't do that.  These were designed for early transistorized radios, which needed a moderately high voltage but very low current.
There are very few motors with suitable low-current coil characteristics to be driven by such a battery.  Others might work at best briefly before killing the battery.
If you need around 9 volts for a motor project, you should probably use 6 or so AA cells.


text:
Fingerprint sensor code won't work correctly with LCD 16x2
I am doing a fingerprint sensor that works with a 16x2 LCD. The code works correctly for the fingerprint sensor, but when I enter LCD code, the LCD works but the fingerprint stops working.
I am using an Arduino Uno and an Adafruit fingerprint sensor. Both the fingerprint and LCD are wired correctly and work separately. I have enrolled and tested fingerprints and it worked perfectly until I added LCD code.
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int getFingerprintIDez();
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup() {
 // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  Serial.begin(9600);
  Serial.println("fingertest");
  // set the data rate for the sensor serial port
  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
  Serial.println("Waiting for valid finger...");
}

void loop() {
  getFingerprintIDez();
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!
  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
   Serial.println("Unknown error");
    return p;
  }

  // found a match!
  Serial.print("Found ID #");
  Serial.print(finger.fingerID);
  Serial.print(" with confidence of ");
  Serial.println(finger.confidence);
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;
  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;
  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  // found a match!
  Serial.print("Found ID #");
  Serial.println(finger.fingerID);
  Serial.print("This Finger print related to : ");
  switch(finger.fingerID){
   case 0:
     Serial.println("Subhy Mesmar");
     break;
   case 1 :
     Serial.println("Amjad Mesmar");
     break;
  }
  return finger.fingerID;
}

In this image the fingerprint sensor works without LCD code:

And in this one the LCD works after adding code but the fingerprint sensor stopped working:

You have pins 2 and 3 used by both the LCD and the SoftwareSerial used for the fingerprint sensor.
Use different pins for one or the other.
Worth looking at the Software Serial reference to understand more about how it works.

is it safe to use different pins for fingerprint sensor ? , Wont changing pins 2,3 damage the sensor ?Please advise .

Yes it should be safe.


text:
Send .wav file from SD card to NodeMCU via serial port
I was able to send a text file in the SD card to NodeMCU using the serial port of an Arduino Mega using this code:

if (readSerial1 == "Okay I'm ready") {
  myFile = sdf.open("uid.txt", FILE_READ);
  if (myFile) {
    while (myFile.available()) {
      char toUID = myFile.read(); //read each char and store to toUID
      uidContent += toUID; //store char to form a String
    }
    Serial1.print(uidContent);
    myFile.close();
  }
}

And it will be read by the NodeMCU then sent to another NodeMCU and stored in the SD of another Mega wirelessly using this code.
while (1) {
  if (Serial.available()) {
    uid = Serial.readString();
    break;
  }
}
data = uid.c_str();
port.beginPacket("192.168.4.255", 2390);
port.write(data);
port.endPacket();

Now, is it possible to send a wav/mp3 file using this process or is there any other way?

What is the type of uidContent, String? If so, then String is not suitable for transmitting bytes. Also your current code will not work with big files, as it requires to load all file content to SRAM (8KB on MEGA) before sending it.

uidContent is String

do have any examples?

For the Arduino part, a quick google search for "arduino read sd file by chunk" gave me this https://forum.arduino.cc/index.php?topic=357548.0 which has an example of reading and writing a file one byte at a time. Not really performant, but it has no memory problem.

For the other side (NodeMCU), I can't comment as I don't use NodeMCU. It is likely one problem you may have is to detect when the file is finished receiving.

I am now able to send the content of the wav files from Mega to NodeMcu through its serial ports, but when i would recieve it on my other nodeMcu part, some parts of the WAV header is not being sent.
Here is a sample of the content of the original wav file:
RIFF¤L  WAVEfmt      À]  À]    data€L  €€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€
While this is what Im recieving on the other node mcu:
RIFFäN€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€
It seems to skip some of the 44byte header of the wav so I wont be able to play it on the other Mega.

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


text:
Auto Reset during programming Arduino
I know this that when you try to program a pro mini without a DTR pin it will give you a error as the atmega needs to be reset during programming. So I had a doubt why does the microcontroller needs to be reset and clearly at which time you know at which moment does it reset ?

A Reset is necessary to activate the Bootloader and allow programming. Without the DTR signal the Reset button has to be pressed just before starting the upload from the Arduino IDE.

Is there any serial traffic before upload or is there anything like that the compiler sends any notification to the device.

The compiler does not know about the upload. It is avrdude that does the upload. The reset is generated by pulling the DTR signal. Please see http://playground.arduino.cc/Learning/AutoResetRetrofit for more information about the auto-reset feature.

If the software is somehow unable to send the reset signal then how could I create reset signal without manually pressing the reset button.

What FTDI (USB-Serial bridge) are you using? Does it lack a DTR pin?

Actually I don't really have a DTR pin I have only TX RX and GND pin.

Is there any timeout for the bootloader to take a decision whether to go for programming or to load sketch ??? If yes then how much ?????

Yes, there is a timeout in the bootloader. After reset the bootloader will wait for a short period (second, see bootloader code) for a command. If no command is received and timeout the current sketch is called. Here is a bootloader hack tutorial https://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader.


text:
Can I power up my Arduino and MotorShield with four AA batteries?
can I power up my Arduino UNO with four AA batteries? It's about 6V, will my arduino burn? And, if I power my Arduino, I have to power my Motor Shield too or not?
I want to make line follower. I have to know what I have to power up. I dont know, how many battery holders I need. I have: Arduino UNO, motor shield, two 6V DC motors

connect 4 AA batteries through the power jack. nothing will get burned. Please share which motor shield you are using

this one: https://goo.gl/XbYSx0 ; and I have two DC motors (6V), btw, I have battery without ON/OFF switch, if I plug it in power jack, and after minute suddenly I disconnect this jack, will it damage my Arduino? is there other option? btw how many volts can I connect to this power jack?

L293d can support upto 36V. (http://www.ti.com/lit/ds/symlink/l293.pdf)
arduino Uno: Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V

@Sanu-OpenMaker I have to give power to Arduino Uno, Motor shield and two motors? so I need 3 battery holders?

Generally No need. You can give 6V to arduino through power jack. Arduino has a voltage regulator to convert to 5v.
Motors driver will get powerder through Vin pin in arduino(in some other cases you can connect 6V to the shield and it will power the arduino as well)
Motors are gerring power from motor driver in most cases

You won't damage the Arduino if you remove the power to the motors as even though you're using the same power supply to power the motots and Arduino (4 x 1.5 - 6V batteries) you're not powering it from the Arduino pins itself.
What exactly are you trying to accomplish here?  Post some more info.

I updated question

can I power up my Arduino UNO with four AA batteries?

6v is about the max if it is directly applied to the chip;
6v is just shy of the minimum if it is applied to a regulator and then to the chip.

This is multiply mistaken.  6v is too low for the usual regulator to deliver rated output.  But 6v is also not the sum of four new alkaline AA's, which will be higher.

It won't cause damage to connect 4xAA to the input of the Arduino's regulator, however it also won't result in specified operation, as even with fairly new cells the voltage will below that which the typically used 5v regulators require to deliver 5v at the output.
Additionally, the common L293D and L298 based bipolar motor shields are horribly lossy.  Feed them about 6v input, and you'll get just over 4v to your motor, which is likely to be disappointing.
To build an Arduino-based battery-powered motor project, you typically need to do one of two things

Use 6, 7, 8 or more cells, and higher-voltage, lower current motors
Use a 3.3v Arduino and a more efficient FET-based H-bridge motor driver/shield, for example the TB6612FNG.   This supports 4 cell operation quite well, and may to a more limited degree work on 3 alkaline cells.


text:
How to create jumping wire using fritzing tool
I am tying to design my circuit diagram with the help of http://fritzing.org/home/ I designed like below circuit where wires are simply straight.

I want some kinda jumping wires with this tool. Does anyone know how to create these jumping wires ?

Thanks for asking this, I was wondering the same thing after accidentally doing it (I didn't know I could).

Draw the wires
press and hold control button (mouse pointer will appear with a curve symbol)
drag the wire using mouse


text:
Building Custom Bootloaders
Recently I was checking out the Atmel's AVR series and I found the Atmega328PB. I am really familiar with Arduino's environment. So I thought if somehow we could bootloader it with Arduino bootloader. My doubt is how can I make a Arduino bootloader for it.

From what I remember the 328PB is not that different from the 328P. I wouldn't be surprised is the default uno bootloader would work without modification.

Here are a couple sources of ATmega328PB bootloaders based on optiboot:

https://github.com/watterott/ATmega328PB-Testing
https://github.com/ElektorLabs/Arduino

I haven't used either.
If you are set on making your own at least they can provide you with a reference.

Is it fully compatible with Arduino

Both projects are specifically written for use with the Arduino IDE, though the bootloaders could be used for any purpose you like.


text:
Using ATiny102 as HID device(emulating mouse)
I am trying to make my own mouse/keyboard from an attiny (it doesn't have to be attiny102 as the title suggests). I have read many articles on this subject: some are using v-usb library.But I understand this is for arduino uno/leonardo.I want to use an attiny with the least amount of electronic parts attached to it(no external oscillator-only the internal one..etc.). All the articles I read said that arduino can't be used as a host usb device but this applies only for it's built-in micro-usb. Can't I just emulate the HID device using digital pins on my attiny? If this is possible where can you provide some well-documented source for the protocol used when communicating with the pc?

Everything is documented on the V-USB website

Atmel Appnote AVR 309 is also a good read on the topic, though v-usb should get you there. And yes, emulating a HID device using GPIO is exactly the thing. And no, this is not about the AVR being a USB host. As posted now the question seems to be a little broad, i.e. there is no specific question here besides "please guide me to some documentation". IMO such questions are not well suited for good StackExchange answers. Maybe it would be better if you research the topic a little more and post more distinct questions along the way.

Don't know anything about the '102 but this is likely achievable in ideal cases with software USB on an ATtiny85.  However, the implementation is unlikely to fully comply with USB specifications so may not work everywhere.  Devices with a dedicated USB host interface and phy should fix that.

I have build a HID midi keyboard with the hiduino lib which is base on LUFA.
http://code.google.com/p/hiduino/w/list
I know LUFA is also capable to build a HID mouse device. But you need to switch to a bigger controller (i think). I used a 8u2 from atmel.
I used AVR Studio 6 to compile hiduino.
Currently i have no link to the LUFA because the git page is gone. Maybe you find a mirror or a other webpage.

the only USB option for AVR microcontrollers without embedded USB, is VUSB which is somehow rare in Arduino; despite some aged libraries around :
https://blog.petrockblock.com/2012/05/19/usb-keyboard-with-arduino-and-v-usb-library-an-example/
https://github.com/gloob/vusb-for-arduino
but if you can rely on atmel studio, the very rich examples of VUSB itself are enough. I made a HID presenter (just two keys to go back and forth on powerpoint slides) using only an attiny24 which is super cheap. the codeandlife blog is a good start for VUSB. it also has a HID mouse example explained :
http://codeandlife.com/2012/02/11/v-usb-tutorial-continued-hid-mouse/

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


text:
Sending value from Arduino to MySQL using ESP8266
I am trying to send a value from Arduino to a remote MySQL. I am following this tutorial.
I have tried but no data is received on the server. What is the issue? I am new to Arduino. My code is:
int led1=8;
int count=1;
void setup() {
  pinMode(led1, OUTPUT);
  delay(3000);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  Serial.println("AT+CWMODE=1");
  delay(1000);
  Serial.println("AT+CWJAP=\"mhboys\",\"mhboys123\"");
  delay(1000);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led1,HIGH);
  // turn the LED on (HIGH is the voltage level)
  delay(1000);  // wait for a second
  if(digitalRead(led1)==LOW) {}
  if(digitalRead(led1)==HIGH) {
    count++;
    //code for sending updated count to mysql
  }
  digitalWrite(led1, LOW);
  // turn the LED off by making the voltage LOW
  delay(1000); // wait for a second
  int adjustedValue = count;
  // print out the value you read:
  Serial.println("AT+CIPSTART=\"TCP\",\"www.onemediagroup.in\",80");
  delay(1000);
  Serial.println("AT+CIPSEND=66");
  delay(1000);
  Serial.print("GET /energy/getUsage.php?currentValue=");
  Serial.print(adjustedValue);
  delay(1000);
  Serial.println("AT+CIPCLOSE");
  digitalWrite(13, HIGH);
  delay(100000); // delay in between reads for stability
}

You must post your code. We cannot tell what you are doing wrong unless you show your work.

I am not getting any data in my server.

The wifi module does not blink the blue light. Is it any issue?

You're sending commands blindly without checking if they actually succeeded. If you're not ready to correctly handle the responses, you're better off using a library like this. The 'HTTP GET' example should help.


text:
Using RFID and fingerprint sensor together
I am using both an RFID and a fingerprint sensor in my project. When running the program, the RFID is detected but the fingerprint sensor is not detected and hence my code is not going forward.
Here is my code
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <MFRC522.h>
#include <SPI.h>

#define SAD 10
#define RST 5

MFRC522 nfc(SAD, RST);

uint8_t id;
uint8_t getFingerprintEnroll();
int getFingerprintIDez();

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup() {
  SPI.begin();
  Serial.begin(115200);
  Serial.println("Looking for MFRC522.");
  nfc.begin();
  byte version = nfc.getFirmwareVersion();
  if (! version) {
    Serial.print("Didn't find MFRC522 board.");
    while (1); //halt
  }

  Serial.print("Found chip MFRC522 ");
  Serial.print("Firmware ver. 0x");
  Serial.print(version, HEX);
  Serial.println(".");
  Serial.begin(9600);

  Serial.println("Fingerprint and RFID Test");

  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
}

char abc;

void loop() {
  abc = Serial.read();
  switch (abc) {
    case '1': {
      // enroll method
    }
    break ;
    case '2': {
      // verify method
    }
    break;
    case '3': {
      // rfid method
    }
  }
}

What does happen?

serial monitor is not showing anything. when I run the rfid and fingerprint code individual they both are working correctly but when join them not working

Literally nothing?  Not even Fingerprint and RFID Test in your setup()?

fingerprint and rfid test is working if I write rfid.init() after this code. if I write before fingerprint and rfid test then it shows nothing

Could you replace your code with a minimal, complete, verifiable example, please? You could also edit your question to remove irrelevant information - such as the stuff about the switch statement. Help us to help you.

Get rid of the Serial.begin(9600). You're accidentally changing the Serial baud rate without doing the same in your serial monitor so you'll probably see gibberish, if anything.

ya I deleted the Serial.begin(9600) and it worked

@TisteAndii would you mind turning your comment into an answer?

Get rid of the Serial.begin(9600). You're accidentally changing the Serial baud rate without doing the same in the serial monitor so you'll probably see gibberish, if anything.


text:
ESP8266 TCP connection WiFiClient issue
I am using ESP8266 01 board to send some data via TCP to server on local network. My code is:
#include <ESP8266WiFi.h>

const char* ssid     = "ssid";
const char* password = "pas";

const char* host = "192.168.1.103";

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

  // Wifi connection

  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");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

   delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int port = 8888;
  if (!client.connect(host, port))
  {
    Serial.println("connection failed");
    return;
  }

}

void loop() {

  while(Serial.available() == 0)
  {
    //wait for arduino serial data
  }

   while(Serial.available())
  {
    // send data to server
    client.print(Serial.read());
  }
}

After chacking code in Arduino IDE I am getting error message:
In function 'void loop()':
espwifitcp1:57: error: 'client' was not declared in this scope
     client.print(Serial.read());
     ^
exit status 1
'client' was not declared in this scope

It appears that if I declare WiFiClient client; inside setup() function it can't be accessed outside it. Do I correctly understand the problem? I have tried to declare object outside setup() and loop() functions but it got even more errors. Any suggestions?

You have declared WiFiClient client; in the scope of setup() only. Move it's declaration to the global scope by placing it above the declaration of void setup()

Or write WiFiClient instead of WifiClient X_X

in the header include WifiClient.h

ESP8266WiFi.h includes WiFiClient.h


text:
State Machine with If/Else?
I am trying to make a state machine with if/else. But some of the code does not seem to be executing. I have information being displayed on an LCD screen so I know where it is, and it stays on Todo=1 and does not go to the next state. I have tried using switch/case and it works. But I think it might be better for my project if I use if/else. Can I do this using if/else?
Void loop{
  if (state == PLAY) {
    Todo = 1;
    Mastermachine(Todo);
  } else if (state == TRYAGIN) {

  }
}

void Mastermachine(int Todo) {
  if (Todo == 1) {
    lcd.setCursor(16, 2);
    lcd.print(String(Todo));
    SongSeq[largestindex];
    for (int index = 0; index < largestindex; index++) {
      SongSeq[index]();
    }
    Todo = 2;
  } else if (Todo == 2) {
    for (learnindex = 0; learnindex < largestindex;) {
      MIDI.read();
      if (learnindex >= largestindex) {
        Todo = 3;
      }
      lcd.setCursor(16, 2);
      lcd.print(String(Todo));
    }
  } else  if (Todo == 3) {
    if (array_cmp(learn, note, largestindex, largestindex) == true) {
      largestindex++;
      delay(2000);
      Todo = 1;
    } else {
      delay(2000);
      Todo = 1;
    }
    lcd.setCursor(16, 2);
    lcd.print(String(Todo));
  }
}

Consider a switch statement instead of the if/else statements.

Make the state a global variable. Todo is lost after each call of MasterMachine() (if you do no introduce a loop).

Inside Mastermachine(), Todo is a copy whatever is passed as its paramter. Even if you make a global named Todo, it will be 'hidden' by the parameter of the same name. That parameter goes out of scope - disappears - when Mastermachine() exits, including any changes made to it within the function.
There are a few choices to accomplish what you want to do, but the easiest is probably to make a global Todo and get rid of the parameter Todo in Mastermachine(); i.e., make it void Mastermachine(void). Then the name Todo inside the function will refer to the global by that name.

Your loop executes continually, always setting the state to 1.....
The called state machine supposedly changes the state, but it get set back to 1 every time the loop executes.
The loop program needs to do the state changes, not the masternachine function.

would moving Todo=1 somewhere else fix that issue ?

If you want to have Todo=1 set only once, place it in setup.


text:
Measuring the capacitance of 2 capacitors at a time
I need help with my project. In my project, I have two homemade capacitors made from 2 aluminum foil strips separated by a water bottle. I need to detect which bottle is going to become empty as people drink from it. So I plan to use the change in capacitance to detect the empty bottle. I know how to measure capacitance of 1 bottle and detect the change, but I don't know how to do it to 2 bottles at a time. Any ideas how to do this?
Here is the following code, I have been working on.
// Initialize Pins
int analogPin = 0;
int analogPin1=1;
int chargePin = 13;
int chargePin1=12;
int dischargePin = 11;
int dischargePin1 = 10;

// Initialize Resistor
int resistorValue = 10000;

// Initialize Timer
unsigned long startTime;
unsigned long elapsedTime;

// Initialize Capacitance Variables
float microFarads;
float microFaradr;
float nanoFarads;
float nanoFaradr;

void setup()
{
  pinMode(chargePin, OUTPUT);
  pinMode(chargePin1,OUTPUT);
  digitalWrite(chargePin1,LOW);
  digitalWrite(chargePin, LOW);
  Serial.begin(9600); // Necessary to print data to serial monitor over USB
}

void loop()
{
  digitalWrite(chargePin, HIGH); // Begins charging the capacitor
  digitalWrite(chargePin1,HIGH);
  startTime = millis(); // Begins the timer

  If(analogRead(analogPin)>648){
    elapsedTime= millis() - startTime;
    microFarads = ((float)elapsedTime / resistorValue) * 1000;

    if (microFarads == X) // X is an arbitrary capacitance when the bottle is empty and X is not a variable
    {
      Serial.println(" Bottle A is empty");
    }

    digitalWrite(chargePin, LOW); // Stops charging capacitor
    pinMode(dischargePin, OUTPUT);
    digitalWrite(dischargePin, LOW); // Allows capacitor to discharge
    while(analogRead(analogPin) > 0)
    {
      // Do nothing until capacitor is discharged
    }
    pinMode(dischargePin, INPUT); // Prevents capacitor from discharging
  }

  If(analogRead(analogPin1)>648){
    elapsedTime= millis() - startTime;
    microFaradr = ((float)elapsedTime / resistorValue) * 1000;

    if (microFaradr == Y) // Y is an arbitrary capacitance when the bottle is empty and Y is not a variable
    {
      Serial.println(" Bottle B is empty");
    }

    digitalWrite(chargePin1, LOW); // Stops charging capacitor
    pinMode(dischargePin1, OUTPUT);
    digitalWrite(dischargePin1, LOW); // Allows capacitor to discharge
    while(analogRead(analogPin1) > 0)
    {
      // Do nothing until capacitor is discharged
    }
    pinMode(dischargePin1, INPUT); // Prevents capacitor from discharging
  }
}

Please edit your question and add a photo, a circuit diagram,  and  a Minimal, Complete, and Verifiable example of code.  This will indicate how you measure capacitance of a bottle.  That may dictate the approach to measuring two at once.

Do you need to do two at a time? How long does it take to do one? Can you do one after the other? Would that be easier for you to implement?

@Mark Smith I would like if the capacitor's capacitance can be measured at the same time

Your requirement of measuring “at the same time” makes no sense: you will never be able to synchronize the measurements to the picosecond. Please, tell us instead how much delay you can tolerate between the measurements. Do they have to be done in the millisecond? In the same microsecond?

If you know how to do one bottle you already know how to do two bottles. The second bottle is identical to the first (just change your pins / variables). Making a class is a good way of abstracting repeated complex code like this.

I have pasted the code. However unable to reformat it.

I just reformatted the code, now the edit is gone...

@Edgar Bonet, I realise that it is impossible to coordinate  the measurements.  Sorry for the request. It would be fine if there is a time difference of 30 seconds or so between each measurement.  I have tried to achieve it through my code. Thanks.

Taking a closer look at this... your approach is completely flawed. You expect a capacitance change in the tens of picofarads, maybe 100 pF at most. With a 10 kΩ resistor, the charging time constant will be no more than one microsecond. Forget analogRead(). You need a bigger resistor (1 MΩ should do) and a faster measurement method. Search this site (or the larger Web) for capacitive sensing methods suitable for small capacitances. Once you manage to measure one bottle (and not before), you may worry about having two bottles.

Ah, I assumed with my answer that the one-capacitor version was known to work...

Before we get started, there are a few problems with your code.
First, I wouldn't check for ==648 or ==0: what if you miss it by a moment and it jumps to 649, or it doesn't quite discharge to 0v?  I'd use >=648 and <=2 (or some other similarly low number - experimentation required).
How is your circuit connected?  I can't quite get my head around the charge and discharge pins, but I'll stick with what you have, assuming it works.
You say you want measurements every 30s, but don't say how long they take. (I have no idea of the capacitance of your bottle.)  My code assumes it takes much less than 30s to measure one bottle.  If it takes near to 30s to do one, this won't work.
I'll also assume your code works.  This is a bad assumption, since it won't even compile (capital I in if) but regardless, that's what I'll assume.
All that being said, here's the code:
// Pins connections
int analogPinA = 0;
int analogPinB = 1;
int chargePinA = 13;
int chargePinB = 12;
int dischargePinA = 11;
int dischargePinB = 10;

// Resistor being used
int resistorValue = 10000;

void setup()
{
  pinMode(chargePinA, OUTPUT);
  pinMode(chargePinB, OUTPUT);
  digitalWrite(chargePinA, LOW);
  digitalWrite(chargePinB, LOW);
  pinMode(dischargePinA, OUTPUT);
  pinMode(dischargePinB, OUTPUT);
  digitalWrite(dischargePinA, LOW);
  digitalWrite(dischargePinB, LOW);
  Serial.begin(9600);
}

// Measure the capacitance of specified capacitor
float measureCapacitance(int analogPin, int chargePin, int dischargePin)
{
    // Ensure the capacitor is discharged
    digitalWrite(dischargePin, LOW);
    while (analogRead(analogPin)>2) { /* Do nothing */ }
    pinMode(dischargePin, INPUT);

    // Start charging
    unsigned long startTime = millis();
    digitalWrite(chargePin, HIGH);

    // Wait for the capacitor to charge
    while (analogRead(analogPin)<=648) { /* Do nothing */ }
    unsigned long endTime = millis();

    // Start it discharging again, ready for next time
    pinMode(dischargePin, OUTPUT);
    digitalWrite(dischargePin, LOW);

    // Calculate measured capacitance
    unsigned long elapsedTime = endTime - startTime;
    float microFarads = ((float)elapsedTime / resistorValue) * 1000;
    return microFarads;
}

void loop()
{
    // Measure capacitance of first bottle
    float microFarads = measureCapacitance(analogPinA, chargePinA, dischargePinA);
    if (microFarads <= X) // X is an arbitrary capacitance when the bottle is empty
    {
      Serial.println("Bottle A is empty");
    }

    // Measure capacitance of second bottle
    float microFarads = measureCapacitance(analogPinB, chargePinB, dischargePinB);
    if (microFarads <= X) // X is an arbitrary capacitance when the bottle is empty
    {
      Serial.println("Bottle B is empty");
    }
}