text:
Need help on the "expected primary expression before "}" token"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

#define M11  2
#define M12  3
#define M13  4
#define M14  5

#define M21  6
#define M22  7
#define M23  8
#define M24  9

uint32_t commands[32];
int confirmationBuzzer = 17;
int i = 0;
boolean DirectionR = true;
boolean DirectionL = true;
int StepsR;
int StepsL;
int steps_left = 4095;
int caseLeft;

RF24 radio(10, 14); // CE, CSN
SoftwareSerial mySoftwareSerial(15, 16); // RX, TX

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

const byte address[6] = "00001";

void stepperR(int xw) {
    for (int x = 0;x < xw;x++) {
        switch (StepsR) {
        case 0:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        case 1:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, HIGH);
            break;
        case 2:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 3:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 4:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 5:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 6:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 7:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        default:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        }
        SetDirectionR();
    }
}

void SetDirectionR() {
    if (DirectionR == 1) { StepsR++; }
    if (DirectionR == 0) { StepsR--; }
    if (StepsR>7) { StepsR = 0; }
    if (StepsR<0) { StepsR = 7; }
}

void stepperL(int xw) {
    for (int x = 0;x<xw;x++) {
        switch (StepsL) {
        case 0:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        case 1:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, HIGH);
            break;
        case 2:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 3:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 4:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 5:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 6:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 7:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        default:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        }
        SetDirectionL();
    }
}

void SetDirectionL() {
    if (DirectionL == 1) { StepsL++; }
    if (DirectionL == 0) { StepsL--; }
    if (StepsL>7) { StepsL = 0; }
    if (StepsL<0) { StepsL = 7; }
}

void setup() {

    myDFPlayer.volume(7);

    radio.begin();
    radio.openReadingPipe(0, address);
    radio.setPALevel(RF24_PA_MIN);
    radio.startListening();
}
void loop() {

    if (radio.available()) {
        radio.read(&commands, sizeof(commands));
    }
    for (i = 0; i<15; i++) {
        switch (i) {

        case(0):

            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }

            break;

        case(1): {
        case1:
            int caseLeft = 1;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }

            break;
        }
        case(2): {
        case2:
            int caseLeft = 2;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(3): {
        case3:
            int caseLeft = 3;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(4): {
        case4:
            int caseLeft = 4;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(5): {
        case5:
            int caseLeft = 5;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(6): {
        case6:
            int caseLeft = 6;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(7): {
        case7:
            int caseLeft = 7;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(8): {
        case8:
            int caseLeft = 8;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(9): {
        case9:
            int caseLeft = 9;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(10): {
        case10:
            int caseLeft = 10;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(11): {
        case11:
            int caseLeft = 11;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(12): {
        function1:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;

            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(13): {
        function2:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;

            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(14): {
        function3:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;

            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(15): {
        function4:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):
                goto function1;

                break;

            }
            break;
        }
        }
    }
outoftheloop:
}

Sorry for the long code,I would send only a part of the code that shows error however I am not sure if the other parts are related to the code. I get this error.

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"
C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino: In function 'void setup()':
C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino:183:35: warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'uint64_t {aka long long unsigned int}' [-fpermissive]
radio.openReadingPipe(0, address);
In file included from >C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino:3:0:
C:\Users\TEDNOKENT2\Documents\Arduino\libraries\RF24-master/RF24.h:350:8: note: >initializing argument 2 of 'void RF24::openReadingPipe(uint8_t, uint64_t)'
void openReadingPipe(uint8_t number, uint64_t address);
C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino: In function 'void loop()':
sketch_dec22a:1288: error: expected primary-expression before '}' token
}
exit status 1
  expected primary-expression before '}' token
This report would have more information with
  "Show verbose output during compilation"
  option enabled in File -> Preferences.

Normally, it should have been fixed adter I deleted some extra brackets but it just stayed there. I have no idea why. I know I shouldn't use gotos that often, however, I am kind of a novice programmer and goto s make it a lot easier to code. I would appreaciate any suggestions on the error. If anthing else that is not related to the error but you think is not necessary, you can mention them too.
Thanks in advance

That is terrible code! There is no call ever to use goto. Full stop. And certainly never in the way you have used it. That code is completely impossible to follow the flow of.

You should not only use goto's that often, you should not use them ever.

goto s make it a lot easier to code - but not as easy to read, eh? Or debug?

Actually, although I'm not going to check for every bracket, here are some tips to prevent the problem (and if you follow them you will find your missing/extra bracket):

Align brackets, I know it saves one line if you put the { directly after statements, but much neater to put them separately on the next line.
Make short functions. If you get a compiler error in a function, it's much easier to find the location. It also helps clearity, maintainability, testability etc. To do this:
Do not use  more than 2 or 3 compound statements (while, switch, if etc).
Put (almost) duplicate code in separate functions and call them (either without parameters or with parameters if needed).
Never use goto statements. You can always replace them by if, while, for or do statements.


text:
Arduino and Pyton Serial Communication, Pyton get stuck and keeps sending
I have recebntly started a project where I need to connect an arduino and another device through serial. And I have setup a simple serial connection betwee python and arduino. I can setup the connection through the python terminal and it works just fine. When I type : arduino.write("1"), It receives the data only once. However, When I run this code:
import serial

arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)

while True:
    arduino.write("1")

it writes properly, howver when I stop it using Ctrl-C, it doesn't stop sending and keeps sending to the arduino. Stops sending when I open a serial. connection using the python terminal again.
I have no idea what is causing this or how I can fiz it. Would reallly appreciate some help. Thanks in advance:

PC side programming is off-topic here.

Your python code is sending "1"'s to the serial port at a blinding speed.  Way faster than they can go out over the serial line.  So when you stop your python code the serial port is still sending trying to get all the "1"'s out of its buffer.  When you open the connection again you clear that buffer and it stops sending.

source: Stack Exchange

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

sort: votes


text:
I am very new to Arduino and the kit tutorial just told me to wire this, I do not know how to wire it. Can Someone plz help!

I can't figure out how to do this the only wires I have are Jumper Wire and M-F 20 Pin DuPont Wire.

Comments are not for extended discussion; this conversation has been moved to chat.

If you don't have a breadboard, get one. It's a board with lots of holes in it that you can use for experimenting.
Something like this:

https://www.adafruit.com/product/64?gclid=EAIaIQobChMIz9XDj9bF6wIVcuW1Ch2_kgD3EAQYAiABEgL4DvD_BwE
As Delta_G said in their comment, you could wire a circuit as simple as yours by just putting the wires from the components directly into the terminals on the Arduino, but that quickly becomes impractical. Get a breadboard and learn how to use it.

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


text:
What is the best way to define an I/O pin?
I read definitions like
const int ledPin = 9;

and also
#define ledPin 9

I know that a definition like
int ledPin = 9;

is a bad practice if you're not going to change it (which you usually won't), though I've see this several times in Arduino programs.
Which one of the other two is preferred?

Strictly speaking, the #define approach will use slightly less memory. The difference is usually tiny though. If you need to reduce memory usage, then other optimisations would probably be far more effective.
An argument in favour of using const int is type safety. Wherever you refer to that pin number by variable, you know exactly what data type you're getting. It might be promoted/converted implicitly or explicitly by the code which uses it, but it should behave in very clear ways.
By contrast, the value in a #define is open to interpretation. The vast majority of the time, it probably won't cause you any problems at all. You just need to be a little careful if you have code which makes assumptions about the type or size of the value.
Personally, I almost always prefer type safety unless I have a very serious need to save memory.

I was in the #define camp until I read Peter's answer. Guess I know who will be refactoring code this weekend. ;)

Probably the best way would be
const uint8_t LED_PIN = 9; // may require to #include <stdint.h>
    or
const byte LED_PIN = 9; // with no include necessary
const unsigned char LED_PIN = 9; // similarly
The name is in caps as per general practice in C++ (and others) to name constants.
This should not use any RAM in itself, and use about 1 byte of program memory per use.
However, there might be problems when the number is higher than 127 and is sign-extended while getting promoted to larger signed integers (not entirely sure on this), although that is unlikely to happen with pin numbers.

Not only will
const int ledPin = 9;

take up RAM, but in this case, will use more RAM than necessary as digitalWrite(uint8_t, uint8_t) only needs one-byte arguments, and an int is usually two bytes (compiler-dependent, but typical). Note that you can give the literal an explicit type in the #define:
#define ledPin ((int)9)

though in a context such as a function argument where a specific type is required (because the function was properly prototyped!) it would either be implicitly cast or get an error message if the types don't match.

@DrivebyDownvoter, would you comment on your reasons?

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


text:
Why does the ADC overflow when using an external Aref?
I'm using an LM35 temperature sensor, and it outputs 190 mV (which agrees with 19°C). I'm using an external reference voltage which I measure as 417mV. (This is set by the pin "aRefCntrl"). So I should have a reading around 467, but I get 1023, i.e. the ADC overflows. When I use the internal 1.1V reference I get a correct reading. Any ideas?
This is my code (Arduino waits for a request from the PC to send the data):
#define lm35 A0
#define aRefCtrl 10

void setup() {
  pinMode(aRefCtrl, OUTPUT);
  digitalWrite(aRefCtrl, 0);
  analogReference(EXTERNAL);
  analogRead(lm35); // dummy read
  Serial.begin(9600);
}

void loop() {
  if ( Serial.available() ) {
    Serial.read(); // PC's request is a single byte
    int temperature = analogRead(lm35);
    Serial.println(temperature);
  }
}

follow-up on Ignacio's comment: would a decoupling capacitor do? (I can't test for the moment, I have to order some)

How much current is available from the reference?

@Ignacio: not much, the reference voltage comes from a 10k/1k divider on 5V. Isn't the voltage I measure at the aRef pin not the real reference voltage?

The minimum reference voltage is 1.0 V according to the atmega328 datasheet. See table 29-15. ADC Characteristics.

Thanks a lot! I was looking for that information in the datasheet, scanning it for all occurrences of "Aref", but it seems to appear as "Vref" :-). (Page 310, FTW.)

Hmm, Aref as in Analog Reference and Vref as in Voltage reference I think? But indeed 1.1V bandgap is a reliable (small voltage) source.

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


text:
Any chance of repairing Mega 2560?
So I decided it would be a good idea to flash a firmware onto my mega 2560 for a project. I ended up flashing the wrong side of the ICSP header (one near the usb plug/ M16U2) and killed my arduino for the most part. I'm a newb for the most part when it comes to this. I plug it in and the 'L' light is on with the 'ON' light as well. Wont show up in device manager, can't load sketches and can't get into DFU mode.(REV3 has DFU right?)
I have both a usbasp and USBtinyisp programmer and have tried 5 - 10 firmwares on both sides of the headers in hopes I can get it to work again.Literally almost tried everything I could find to get it to flash back. One guide I did find that got me somewhere was this though:
http://forum.arduino.cc/index.php?topic=96706.0
It appears to flash correctly with whatever firmware I use, but cannot pull the firmware off it to check or do the rest of what it says.My guess is that 1 or 2 of the firmware's are gone. Have erased the firmware/relfashed and tried to flash in the IDE with no luck in it working.
I did use this command to read the firmware(or lack of) on both sides of the header to get a better idea of what might be going on:
M16U2/M2560
I don't know if I'm supposed to flash one side first then the other or flashing one will be able to do both at the same time. Here is an example of how I would flash from avrdude:
avrdude -C avrdude.conf -P usb -p m16u2 -b 1200 -c usbasp flash:w:Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xF4:m -U lock:w:0x0F:m

With each side I would set the correct the fuses from what that file pulled up. So if anyone knows what I'm doing wrong for why it is not working I'd appreciate the help. Just don't know if I can save this or it's gonna end up in the garbage.Thanks

this https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/firmwares/atmegaxxu2/Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex is the one you'd need for the 16u2. For the 2560 you'd need https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex . First do the 16u2. After that you should see something in the device manager.

I tried it but didn't have any luck getting to work once plugging it back in to the laptop. Maybe i'm doing the wrong lock fuse or something. To me though it looks like it is flashing.  http://pastebin.ca/3178364

I got a board with the symptoms you described. I ended up removing the 16u2 as I found out it was faulty, the board works now (without any USB of course) so if you can make sure the mega2560 chip works and the 16u2 doesn't and you are not bothered by the lack of USB connection then you can do the same.

I think i could possibly live without it but i just can't seem to get a firmware to  stay on either one of them. Starting to wonder if there are certain steps to flashing that i'm missing besides what i have been doing.(erasing/unlocking/flashing/locking of the firmware) When i look over everything, it seems like it should all be good. Gonna keep trying and see if i can get a firmware to stick.

This looks interesting even though it's to repair a Mega ADK:   http://altlab.org/d/m/jpralves/recover_atmega2560/  Maybe i do have to unlock/flash/lock the firmwares to keep them in. Will see how it goes.

It looks like it's back to working :) I flashed both side's with the firmware 'Gerben' mentioned using the guide in the link above.(* Setup Fuses unlocking bootloader * Burn the bootloader * Setup Fuses locking bootloader) From what it looks like it was because i didn't unlock/lock in the firmwares correctly and or the right firmware. For whatever reason i'm on COM4 instead of COM3 now but i can upload sketches and seems to have no issues. Hopefully someone else will be able to use this if they end up messing up there firmware to fix it. Appreciate all the help.

http://altlab.org/d/m/jpralves/recover_atmega2560/
Change fuses/firmware depending on which side you are flashing (M2560/M16U2)
Firmwares: Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex / stk500boot_v2_mega2560.hex
1st command:
avrdude -C avrdude.conf -P usb -p m16U2 -b 57600 -c usbtiny -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xF4:m -U lock:w:0x0F:m

2nd command:
avrdude -c usbtiny -p m16U2 -P usb -b 115200 -V -U flash:w:Arduino-COMBINED-dfu-usbserial-atmega16u2-Mega2560-Rev3.hex

3rd command:
avrdude -c usbtiny -p m16U2 -P usb -b 115200 -U lock:w:0x0F:m

http://pastebin.ca/3178420


text:
What is the most Inputs on An analog Pin with a specified Accuracy on the Resistors Used?
I am asking about the technique were switches and resistors in series are used on the analog pin, so it creates a voltage divider when one of the switches is pressed, it a well known technique it is described here with 5 buttons on one analog pin
http://www.instructables.com/id/How-to-access-5-buttons-through-1-Arduino-input/
Obviously we assume the circuit is set up correctly e.g. the voltage reference is stable-but such things aren't the main point of this question.
My question is about an obvious generalisation of that circuit for more inputs - and where the resistors are all replaced by, a single type of resistor, a fixed resistance value (e.g. 4k ohm) with a known error on the resistance (e.g. 5%). We use the 4k resistors to make up larger values 4k,8k,12k,16k,20k Ohms and make the 100k Ohm resistor out of twenty five 4k ohm resistors.
My question using only 4k ohm resistors (connected in series in the voltage divider) within a 5% error of their value -what is the most switches  we can theoretically connect for reliable working on one analog pin?-I'll have a guess at 20 switches.
The second question is given X ohm resistors (connected in series in the voltage divider) within a E% error of their value -what is the most switches, M,  we can theoretically connect for reliable working on one analog pin? So there will be a formula for this-so what is it?

As some point even with perfect resistors you will run into noise, impact of dirty contacts, etc...  It's not clear that what precision resistors will gain you over loose tolerance ones would leave enough margin for these concerns, though the situation might be better than pushing the limits of these concerns on top of only approximate resistances.

@LookAlterno Its not a school assignment

@LookAlterno I just wan't to know a formula,or a proof, for the most number of
inputs I can use reliably,as that means I can replicate the circuit reliably,on the arduino with certain resistors
as that would be the cheapest way for me to do the
input of my circuit.

My question using only 4k ohm resistors (connected in series in the
  voltage divider)

The circuit you are describing (a collection of identical resistors
connected in series) is quite different from the one discussed in the
link you provide. I assume you have in mind something analogous to a
digital pot, i.e. a chain of N resistors between GND and Vcc, with N
switches connecting the Arduino analog input to N taps along the chain:
N−1 taps in between consecutive resistors and one extra tap at GND¹.

simulate this circuit – Schematic created using CircuitLab
The push buttons could actually be the multiple contacts of a rotary
switch. If using real push buttons, it would be tricky to know whether a
button is being pushed or if you are just reading noise, though it can
be known by toggling the pin between output and input and using its
stray capacitance.
In this case, the worst case error happens when all the resistors on one
side of the tap are 5% too high, while all those at the other side are
5% too low. It can be easily shown that the minimum possible voltage at
tap k is
V−(k) = k(1−e)/(k(1−e) + (N−k)(1+e))×Vcc
where e = 0.05 is the 5% tolerance of the resistors. The
maximum possible voltage, V+(k), is given by the same formula
after replacing e by −e.
The graph below shows the expected voltage at each tap for a chain of
20 resistors, with the error bars showing the possible voltage
ranges. The blue curves are the analytical expressions of
V−(k) and V+(k).

As can be seen on the graph, the largest error bars are the ones closer
to the central tap. Thus, the buttons can be discriminated accurately if
the two taps closest to the center have non overlapping error bars. This
criterion could be translated into an exact formula, but the formula
would be quite complex and depend on the parity of N. I propose to do
instead some approximations, starting with a first order Taylor
expansion on the error. This expansion gives the length of the error
bars as
V+(k) − V−(k) ≈ 4/N2 k (N−k) e × Vcc
This error is maximal when k = N/2 (we will forget the parity issue):
V+(N/2) − V−(N/2k) ≈ e × Vcc
Then we say that the taps can be discriminated if this error is smaller
than the expected voltage difference between consecutive taps, namely
Vcc/N. Then the criterion is now simply
N ≤ 1/e
which is exactly what your intuition told you.

¹ The extra tap could be at Vcc rather than GND. It would be unwise,
however, to tap both GND and Vcc, as a user pressing both buttons
simultaneously would then short the power supply.

Edit to answer the questions in comments.
Yes, you could use the expressions of V+ and V− to
set the thresholds for discriminating the buttons. For example, you
could set the threshold between k and k+1 halfway between
V+(k) and V−(k+1), i.e.
threshold(k, k+1) = (V+(k) + V−(k+1)) / 2
However, if you try to compute that, you will see that it is extremely
close the average between the expected voltages. Then, you could use
that average as your threshold, which is given by a very simple formula:
threshold(k, k+1) = (k+½)/N×Vcc
Then, the pin number is just the integer closest to Vin/Vcc×N.
Assuming N = 20, this can be written as:
const float VCC = 5.0
float voltage = analogRead(PIN) * VCC / 1024;
int button_number = round(voltage / VCC * 20);

This can be optimized to use only integer arithmetics:
int button_number = (analogRead(PIN) * 5 + 128) >> 8;

Note that 20 buttons is kind of borderline. Your system could fail
if the errors due to resistor inaccuracies and those intrinsic to the
ADC (offset, gain error...) add up the wrong way. If you are building
only one device, then the safest fix would be to take note of the value
analogRead() returns for each button, and set the thresholds halfway
between consecutive values.
You could also measure the resistances of all your resistors, and
arrange them in a way that minimizes the errors. For example, if you
number the resistors from 0 to 19 in order of increasing resistance, I
would build the chain by arranging them in the following order:
0 19 2 17 4 15 6 13 8 11 9 10 7 12 5 14 3 16 1 18
Then you could most likely use the simple formula above to get the
button number at a minimal computing cost.

Yes-you are correct -that is the circuit I had in mind.

Im asking this question because I have a lot of cheap
resistors with the same value and wanted to know what
the most inputs I can get from 1 pin.

So If I chose N=20 resistors, then I could directly use
the analytical expressions of V−(k) and V+(k) to set the
bounds voltage tap k will produce in my arduino sketch?-
-is that correct?

And we dont use tap 20 in practice because V−(20)=V−(20)
i.e. there are no lower and upper bounds

This is the sort of answer I had in mind when I wrote this Meta post.

"Your system could fail if the errors due to resistor inaccuracies and those intrinsic to the ADC (offset, gain error...) add up the wrong way. If you are building only one device, then the safest fix would be to take note of the value"

So if you wanted the safest provable way without measuring any voltages (e.g. for building lots of devices quickly) then you can use tap all the odd taps
as input buttons 1,3,5,...,19 and the theoretical thresholds for would be
V+(k-1)< V(k) < V-(k+1) , for the voltage V(k) from tap (odd numbered) k.

This would give you 9 buttons you can use without the need to know
the actual voltages-measured in some way -it only depends on the
known errors of the known components involved.

So you get theoretically provable lower and upper voltage thresholds, for
each of the  at the cost of using 11 less buttons-you could think of it
as- to make error bounds theoretically computable-without measurement
of some sort-costs 11 resistors.

Is that correct?

You're just making a voltage resistor divider, with Vin at the top of the string of resistors, Vout at the selected resistor junction, and Gnd at the bottom.
Using 5V as Vin, then
Vout = Vin x (sum of resistance below the junction)/(sum of the entire string).
For example, 10 1K resistors, and the junction is between resistors 5 and 6 (resistor 1, R1, connected to Vin, resistor 10, R10, to Gnd).
Vout = 5V * 5,000/(10,000) = 2.5V
Arduino will return a reading of ~ 2.5V/5V * 1024 = 512 (~4.88mV per step)
Connect between R7 and R8
Vout = 5V * 3,000/(10,000) = 1.5V
analogRead = ~1.5/5 * 1024 = 307


text:
Can't make Quadratic Programming Library (EigenQP) library work in NodeMCU
I have been trying to use find a Quadratic problem solver library which is compatible with Eigen library for my NodeMCU board (In my code I have to deal with a lot of matrix operations, that's why I chose Eigen)
I found the following one by jarredbarber : [https://github.com/jarredbarber/eigen-QP][1]
For some reason, I'm not being able to use the QP library in my nodemcu. Here is one very simple demonstration I've been trying to establish. But it throws away some errors, I can't figure why :
In my sketch for the NodeMCU:
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>

#include <iostream>
//#include<ctime>        //srand()
#include<cstdlib>

#define _USE_MATH_DEFINES
#include <cmath>        // for using M_PI, Trig. functions (sin/cos/tan)
using namespace std;

//#include <ArduinoSTL.h>
#include "Eigen313.h"
using namespace Eigen;

#include "eigen-qp.hpp"
using namespace EigenQP;

//#define PI 3.1416

void setup() {
  // put your setup code here, to run once:
  Serial.println("Lets start!!");
int N=4;
float gamma =10000;
float safety_radius=0.1200;
  Eigen::Matrix<float,Dynamic,Dynamic> Q(2*N,2*N); Q.setIdentity();
  cout<<Q<<endl;
  Eigen::Matrix<float,Dynamic,1> c(2*N,1);
      c<<0,
         0,
    0.0086,
    0.0847,
   -0.1499,
    0.0687,
    0.0083,
   -0.0867;

    Eigen::Matrix<float,Dynamic,Dynamic> A(6,2*N);
  A<<0.0655,0.6483,-0.0655,-0.6483,0,0,0,0,
    -0.5839,0.6251,0,0,0.5839,-0.6251,0,0,
    -0.5587,-0.0275,0,0,0,0,0.5587,0.0275,
     0,0,-0.6494,-0.0232,0.6494,0.0232,0,0,
     0,0,-0.6242,-0.6758,0,0,0.6242,0.6758,
     0,0,0,0,0.0252,-0.6526,-0.0252,0.6526;
std::cout<<"A:\n"<<A<<endl;
    Eigen::Matrix<float,Dynamic,1> b(6,1);
b <<7.7197,
   47.8600,
    2.6007,
    7.5770,
   76.6646,
    7.8461;

    A=-A;
    b=-b;
Eigen::Matrix<float,Dynamic,1> x(2*N,1);
yield();
delay(2000);
        //boost::timer::auto_cpu_timer t;
        //for (int ii=0; ii < 8; ii++)
        //QPIneqSolver::solve(&H,&f,&A,&b,&x);
EigenQP::quadprog<float,Dynamic,Dynamic>(Q,c,A,b,x);
std::cout<<x<<endl;

}

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

}

It compiles fine, but in the serial monitor, these show up :
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld

I tried to run the same code in codeblocks software in my computer, it works fine though.
PS: This is the first time I've ever posted any topic in this forum, please inform me if I've violated any guideline for the post..
Thanks in advance.
[1]: https://github.com/jarredbarber/eigen-QP

what prints in the serial monitor if you compile and upload a simple LED blink sketch?

Serial.begin is missing

FYI: Don't use all uppercase in the title, it's the same like SHOUTING to the audience. Not very polite ;-). In addition to what was already commented: How do you upload the sketch? Please add the output from the upload to the question. -- Why did you tag the Question with "reset"?  -- You have std::cout calls in the code. What's their meaning for an ESP? (I don't know that, it's an open question, not a rhetoric one). Try if the sketch works without the cout. Just use Serial.prinl("cout debug 1"), Serial.prinl("cout debug 2")  (and so on) instead out the std::couts.


text:
Servo keeps rotating
I use a 360° servo with a program I made on an Arduino Uno.
The program was meant to control two separate servos with two potentiometers on a breadboard.
When I used 180° and 90° servos, it worked fine, but when the 180° servo was swapped with a 360° servo, it kept spinning and spinning non-stop.
Could it be the knock-off board (not Arduino brand) I'm using?
Servo serX;    // create servo object to control a servo for x axis
Servo serY;    // create servo y axis

int pot1 = 0;  // potentiometer pin number on board
int pot2 = 1;  // potentiometer number 2
int val;       // variable to read the value from the analog pin
int val2;      // read value from other analog pin

void setup() {
  serX.attach(9);  // attaches the servo on pin 9 to the servo object
  serY.attach(10); // attach servo to number 10 pin
}

void loop() {
  val = analogRead(pot1);           // reads the value of the potentiometer
  val = map(val, 0, 1023, 0, 180);  // scale it to use it with the servo
  serX.write(val);                  // sets the servo position

  val2 = analogRead(pot2);          // reads the value of the potentiometer
  val2 = map(val2, 0, 1023, 0, 90); // scale it to use it with the servo
  serY.write(val2);                 // sets the servo position

  delay(15);                        // waits for the servo to get there
}

Is this a "continuous rotation" servo? If so then the "angle" defines the speed and direction of rotation, with 90° normally being "stop".

The "360°" you refer to is probably a continuous-rotation servo.
These servos behave more like motors than like the normal RC servos; the signal you send such a servo will determine its speed and direction, not its position, and a pulse width of 1.5 ms (the "90° setting") will make it stop.
There's an article on the Adafruit site where you can read more about this.


text:
How to determine when a sensor's output signal changes significantly?
Starting with an analog signal from any sensor, how do I automatically determine if there is a real signal change or a reset? Below is the sample code that better explains my goal:
value = sensorRead();

if (millis() > t + 1000) {
  value_old = value;
  t         = millis();
}
float d = value - value_old;
if (d < -3) {
  print("signal change")
  myfunction(); //ex. bink led
}

So if value(current) = 0.5 and value_old = 100 there has been a real change in signal, even if, because of noise, the value is not exactly 0.
It's very raw code, and I'd like to use a more professional approach that can detect both increments and decrements. Ideally, a suitable library, or alternatively a more efficient algorithm, would be ideal.

https://www.best-microcontroller-projects.com/arduino-absolute-value.html

you could average the reading over a period of time

if (millis() > t + 1000) { will run into problems about 49 days after arduino reset ... read this https://www.norwegiancreations.com/2018/10/arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros/

The problem is not the millis(). The values are already filtered by a moving average, I want to distinguish the increments.

What do you know about the process's insignificant noise?  Do you care about drift?  E.g. would +0.1 per cycle be noteworthy?

There is no simple and universal way of deciding what is to be
considered a “significant” change. My recommendation would be to create
an algorithm by playing with the data on your computer:

Upload a sketch to your Arduino that simply reads the sensor and
transmits the readings to your computer via Serial.

Capture a long run of such readings in a file.

Graph the readings and look carefully at the graphs: what would you
consider a significant change? What criteria could you use to decide
that a change is significant? Devise an algorithm to take this
decision.

Implement and test the algorithm on your computer, using the recorded
data.

Port the code to the Arduino.

Below are two examples that maybe could give you some ideas. First comes
the simplest I can think of: it says the change is significant if the
current value is far enough from a previously recorded value. That
recorded value is only updated after a significant change has been
detected.
bool changed_significantly(int value) {
    const int change_threshold = 4;  // tune to taste
    static int old_value;
    bool changed = abs(value - old_value) >= change_threshold;
    if (changed)
        old_value = value;
    return changed;
}

It would be used like this:
void loop() {
    int value = sensorRead();
    if (changed_significantly(value))
        Serial.println("Signal changed.");
}

Note that, with this algorithm, a very slow drift would periodically
trigger the detection of significant changes. If this is not what you
want, you may prefer to only detect changes that happen fast enough.
This can be detected by the use of a high-pass filter. The function
below implements a simple low-pass filter (exponentially weighted moving
average) and compares the current input to the previous output. The
difference is equivalent to a high-pass filter. If that difference is
large enough, a significant change is detected, then the filter is reset
in order to prevent it from being repeatedly triggered by a drift:
bool changed_significantly(int value) {
    const float change_threshold = 4;   // tune to taste
    const float filter_constant = 0.1;  // this one also
    static float filtered_value;
    float delta = value - filtered_value;
    bool changed = abs(delta) >= change_threshold;
    if (changed)
        filtered_value = value;
    else
        filtered_value += filter_constant * delta;  // low-pass filter
    return changed;
}

Edit: some details about the filter.
This high-pass filter alone could be simplified as this:
float filter(float x)
{
    static float y;
    float delta = x - y;
    y += filter_constant * delta;
    return delta;
}

Note that, if this function returned y, we would have a conventional
first-order low pass filter (exponentially weighted moving average).
Since it returns instead the difference between the input and the
previous low-pass-filtered value, it behaves as a high-pass filter. Its
transfer function is
H = (z − 1) / (z + k − 1)
where k is the filter constant. This constant is related to the cut-off
frequency as
fc ≈ k fs / (2π)
where fs is the sampling frequency. Note that, for
frequencies significantly smaller than the cut-off, the filter behaves
like a scaled derivative:
H x ≈ 1/(k fs) dx/dt

We don't deserve you Edgar!

Why would small drift over time trigger the first code? Is it because it accumulates over a long period of time? Basically, is it a math thing, or a programming issue?

@ZhelyazkoGrudov: It's the algorithm. The recorded value is only updated after a significant change has been detected. If there is a slow drift,  it will accumulate change until that detection is triggered.

Thank you so much, I tried your last function, and it works as desired, simple and effective!!!! I don't really understand the filter_constant function, how can I estimate the ideal value based on usage?

@boromyr: See expanded answer.


text:
Movement and storage of servo positions via bluetooth
I am trying to program a 4DOF robotic arm (using 4 servo motors). I would like to move the servos using an Android app, by means of bluetooth (HC-05).
I developed the app, in whose interface there are 7 buttons (Up, Down, Left, Rght, Forward, Backward, Save) and a field in which you can type using the keyboard (I attached an app interface screenshot).
So far, I tried to manage only Up button (which senses 'U' via bluetooth to Arduino) and Down (which sens 'D'), which would move the servo intended for vertical arm movements. That code works quite well, although the app crashes sometimes, but I still can't figure the reason out. Using the println() function I can see that Arduino reads in the bluetooth input the character '⸮ ' , what does it mean? I use a baud rate of 9600.
Then, what I would like to do is: once I have moved the servo and reached a particular position, I'd like to save the position of the motor into an array. In order to allow multiple positions saving, after Save button is pressed, I want Arduino to wait for another input from the bluetooth (so that, it would wait for another button to be clicked from the app or for something typed into the dedicated bar from keyboard) that defines the position within the array, in which I save the servo position (eg: I type '2' into the dedicated app bar and Arduino, which is waiting for an input, receiving it, saves the last servo position into the array servo01pos[2]). I wrote the "case ('S')" code that is quite long and inefficient but, at least, it works.
Thanks in advance to anyone who will help me.
Giammarco

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10, 11); //BlueTooth RX ,TX
#include <Servo.h> // servo library

unsigned long timestampSend = 0; // BT Write wait period
String readString;
Servo servo01; // servo name
byte pos01 = 90;
#define ANGLE_STEP 10 // 10 degree steps
int servo01pos\[50\];
char data;
char button;
int x = 1;

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  delay(1000);
  Serial.println("Ready ...");
  Serial.println ("\n");
  servo01.attach(12);
  servo01.write(pos01);
}

void loop() {
  do {
    if (bluetooth.available() > 0) {
      Serial.println(data);
      do {
        delay(500);
        data = bluetooth.read();
        Serial.println(data);
      } while (bluetooth.available() > 0);
      if (data == 'U' || data == 'D' || data == 'L' || data == 'R' || data == 'F' || data == 'B' || data == 'S' || data == 'X') {
        Serial.print("Received value: ");
        Serial.println(data);
        switch (data) {
          case ('S'): //SAVE
            Serial.println("Enter the button you desire to set...");
            do {
              data = 254;
              bluetooth.listen();
              button = bluetooth.read();
              if (button == '0' || button == '1' || button == '2') {
                data = 'S';
              }
            } while (data != 'S');
            Serial.println("SAVING POSITIONS ...");
            if (button == '0') {
              servo01pos\[0\] = pos01;
            } else if (button == '1') {
              servo01pos\[1\] = pos01;
            } else if (button == '2') {
              servo01pos\[2\] = pos01;
            }
            servo01.write(pos01);
            Serial.println(button);
            Serial.println(pos01);
            Serial.println(servo01pos\[1\]);
            Serial.println(servo01pos\[2\]);
            Serial.println(servo01pos\[0\]);
            //}
            break;

          case ('U'):
            Serial.println("UP");
            if (pos01 <= 180 - ANGLE_STEP) {
              pos01 = pos01 + ANGLE_STEP;
              servo01.write(pos01);
              Serial.println(pos01);
              delay(20);
              Serial.println(data);
            }
            break;

          case ('X'):
            Serial.println("EXIT");
            x = 0;
            break;
        }
      }
    }
  } while (x != 0) ;
}

When your app crashes, it might send garbage data via serial, which then gets interpreted as a reversed auestion mark. You should look into the logs of your app to check, why it crashes. What is your question about the rest of the project? You didn't formulate one there

Thank you for your answer. I tried with the catlog but no problems seems to occour (I'll try again just to be sure). My main question is just about the reversed question mark. The second part is just to know if there are others easier way to do what I would like to do in the case 'S'.
Many thanks.
Giammarco

I just tried using a random app downloaded from PlayStore (and using the same Arduino code) and I still have that problem. In that case I think I can say that it is not a my app problem, is it?

I think I solved the problem using a power supply. It seems that the reversed question mark occurred when there was a power outage. Now it seems to work properly. Thank you very much again for your help!
Giammarco

Please write that solution into a proper answer and accept it as correct. That helps us keeping track, which questions are already answered.

The character '⸮ ' was given everytime there was a power outage. So, it is enough to use an external power supply to power bluetooth and servos (Arduino cannot power all those modules simultaniously).

As chrisl mentioned, you should mark your answer as accepted.


text:
Arduino expected ')' before '{' token
 for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte{i}, HEX);
    content.concat(String(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte{i}, HEX));
  }

I use this code to get UID of an RFID card, but the error "expected ')' before '{' token" comes out. Does anybody know how to solve this problem?

This is a simple C(++) error and not Arduino-specific. To address array elements you should use square brackets ([, ]) instead of curly brackets. Replace all instances of {i} with [i] and you will be good to go.

I’m voting to close this question because it's a trivial syntax error question.

Welcome to this site, next time use ctrl-k to align your code.
The problem is in:
Serial.print(mfrc522.uid.uidByte{i} < 0x10 ? " 0" : " ");

To get the value of an array, use [i], not {i}, thus
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");

(similar to other lines).
To give some additional hints:

Try to prevent duplicated code by storing the byte into a variable. Now you can use uidValue in the subsequent lines.
uint8_t uidValue = mfrc522.uid.uidByte[i];

Store 0x10 inside a constant.

So you get:
const uint8_t MAX_HEX_SINGLE_DIGIT_VALUE 0x0F;

for (byte i = 0; i < mfrc522.uid.size; i++)
{
    uint8_t uid_byte = mfrc522.uid.uidByte[i];
    Serial.print(uid_byte < LOW_VALUE <= MAX_HEX_SINGLE_DIGIT_VALUE ? " 0" : " ");
    Serial.print(uid_byte, HEX);
    content.concat(String(uid_byte < MAX_HEX_SINGLE_DIGIT_VALUE ? " 0" : " "));
    content.concat(String((uid_byte, HEX));
  }

I assume the Serial lines are only for debugging, otherwise you can combine them too.

Thank you very much, Michel.

If the answer helps, consider upvoting and if the answer solves your problem, also accept it.

Michel, if you answer a syntax error question, then at least without syntax errors. I thought you agreed that syntax error questions shouldn't  be answered.

@Juraj I don't have a compiler at hand ... and I will ask to move the question to StackOverflow.

they will reject a syntax error question. it is off-topic on SO

So how can the op get an answer? For a beginner programmer I can imagine it's not immediately trivial to use [] instead of {}.

to ask a question for every syntax error is not a way to learn codding. a codding course is a much better way

@Juraj .. However, at least instead of answering a duplicate question could be found. Although I hope if the OP stays interested he will do a course, or at least find out how C(++) syntax works.


text:
Problem with displaying true RMS of the main voltage using Arduino Uno and MAX7219
I want to display the true RMS of the main voltage using an Arduino and a MAX7219. At first, I used of circuit #1 (the following circuit) and the True RMS library. The circuit could successfully return the true RMS and print it on the Serial Monitor of thr Arduino every 0.5 s. The accuracy was acceptable. This circuit was supplied from a PC.

At the next step, I decided to use a MAX7219 (LedControl library) to display the true RMS voltage on a 7-segment display. For this, I used of circuit #2. I don't know what happened. The displayed voltage had variations. In circuit #1, the maximum error was 1 volt (checked by a true-RMS multimeter - for example, the voltage was 210 V and the printed voltage was 209 or 211 V). But in circuit #2, the error was up to 10 V with a fast variation. For example, the voltage was about 210 V (using multimeter) for a minute, but the displayed voltage by the Arduino was 203, then 209, then 217, then 205, then 200, then 215, ....
I also used another true-RMS multimeter to check the first multimeter. The voltage was the same value as the first one showed. The problem was that the voltage remained approximately constant, but the Arduino showed a variable voltage. I checked the supplied voltage from the PC. It doesn't have effective changes.
I also used of circuit #3, but I still had the same problem. It seems that the used of MAX7219 is causing the problem. What is the solution?

My code (for bi-color common anode 7-segment - I have 3 digits):
#include <TrueRMS.h>
#include <digitalWriteFast.h> // It uses digitalWriteFast only for the purpose of debugging!
                              // https://code.google.com/archive/p/digitalwritefast/downloads

#define LPERIOD 1000    // 1000 loop period time in us. In this case 1.0ms
#define ADC_INPUT 0     // define the used ADC input channel
#define RMS_WINDOW 40   // rms window of 40 samples, means 2 periods @50Hz
// #define RMS_WINDOW 50   // rms window of 50 samples, means 3 periods @60Hz

#define PIN_DEBUG 4

unsigned long nextLoop;
int adcVal;
int cnt = 0;
float VoltRange = 5.00; // The full scale value is set to 5.00 Volts but can be changed when using an
                        // input scaling circuit in front of the ADC.
Rms readRms; // create an instance of Rms.

// Max7219 ===============================================================
#include "LedControl.h"
int ClockPin = 5;
int CsPin = 6;
int DinPin = 7;
LedControl lc = LedControl(DinPin, ClockPin, CsPin, 1);
// 1 as we are only using 1 MAX7219

void setup() {
  Serial.begin(115200);
  pinMode(PIN_DEBUG, OUTPUT);

  // configure for automatic base-line restoration and continuous scan mode:
  readRms.begin(VoltRange, RMS_WINDOW, ADC_10BIT, BLR_ON, CNT_SCAN);
  //readRms.begin(VoltRange, RMS_WINDOW, ADC_10BIT, BLR_OFF, CNT_SCAN);
  // configure for no baseline restauration and single scan mode:
  //readRms.begin(VoltRange, RMS_WINDOW, ADC_10BIT, BLR_OFF, SGL_SCAN);
    readRms.start(); //start measuring
    nextLoop = micros() + LPERIOD; // Set the loop timer variable for the next loop interval.

  // Max7219
  // the zero refers to the MAX7219 number, it is zero for 1 chip
    lc.shutdown(0, false); // turn off power saving, enables display
    lc.setIntensity(0, 15); // 15 sets brightness (0~15 possible values)
    lc.clearDisplay(0); // clear screen
  }

// Max7219
byte board[1][8] = {
  {
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000
  }
};

//  Max7219             Device
void ClearDisplay(int b, int digit) {
  for (int i=0; i<8; i++) { // It clears all segments of Digit "digit" of Device "n"
    bitClear(board[b][i], 7-digit);
  }
}

void Display() {
  for (int i=0; i<9; i++){
     lc.setRow(0, i, board[0][i]);
  }
}

//         Device  Digit      Num       Dot
void SetNum(int b, int digit, int num, int dot) {
switch(num) {
  case -2: // displays  "-"
    bitClear(board[b][0], 7-digit); // seg-A
    bitClear(board[b][1], 7-digit); // seg-B
    bitClear(board[b][2], 7-digit); // seg-C
    bitClear(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case -1: // it clears the display
    bitClear(board[b][0], 7-digit); // seg-A
    bitClear(board[b][1], 7-digit); // seg-B
    bitClear(board[b][2], 7-digit); // seg-C
    bitClear(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitClear(board[b][6], 7-digit); // seg-G
    break;
  case 0:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitSet(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitClear(board[b][6], 7-digit); // seg-G
    break;
  case 1:
    bitClear(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitClear(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitClear(board[b][6], 7-digit); // seg-G
    break;
  case 2:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitClear(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitSet(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 3:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 4:
    bitClear(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitClear(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 5:
    bitSet(board[b][0], 7-digit); // seg-A
    bitClear(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 6:
    bitSet(board[b][0], 7-digit); // seg-A
    bitClear(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitSet(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 7:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitClear(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitClear(board[b][5], 7-digit); // seg-F
    bitClear(board[b][6], 7-digit); // seg-G
    break;
  case 8:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitSet(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
  case 9:
    bitSet(board[b][0], 7-digit); // seg-A
    bitSet(board[b][1], 7-digit); // seg-B
    bitSet(board[b][2], 7-digit); // seg-C
    bitSet(board[b][3], 7-digit); // seg-D
    bitClear(board[b][4], 7-digit); // seg-E
    bitSet(board[b][5], 7-digit); // seg-F
    bitSet(board[b][6], 7-digit); // seg-G
    break;
    default:
    break;
  }
  if (dot == 1) {
    bitSet(board[b][7], 7-digit); // seg-dp
  }
}

//                          0-1-2-3
//                  Device  Digit      Num       Dot      Color
void SetNumWithColor(int b, int digit, int num,  int dot, char color) {
  switch(color) {
    case 'r': // digits 0,1,2,3 are Red
      SetNum(b, digit, num, dot);
      SetNum(b, 4, -1, dot); // clears all Green segments
      SetNum(b, 5, -1, dot); // clears all Green segments
      SetNum(b, 6, -1, dot); // clears all Green segments
      SetNum(b, 7, -1, dot); // clears all Green segments
      break;
    case 'g': // digits 4,5,6,7 are Green
      SetNum(b, digit+4, num, dot);
      SetNum(b, 0, -1, dot); // clears all Red segments
      SetNum(b, 1, -1, dot); // clears all Red segments
      SetNum(b, 2, -1, dot); // clears all Red segments
      SetNum(b, 3, -1, dot); // clears all Red segments
      break;
    case 'o':
      SetNum(b, digit, num, dot); // Turns Red on
      SetNum(b, digit+4, num, dot); // Turns Green on
      break;
    default:
      break;
  }
  Display();
}

void loop() {
  adcVal = analogRead(ADC_INPUT); // read the ADC.
  // readRms.update(adcVal); // update
  digitalWriteFast(PIN_DEBUG, HIGH);
  readRms.update(adcVal); // for BLR_ON or for DC(+AC) signals with BLR_OFF
  // readRms.update(adcVal-512); // without automatic baseline restoration (BLR_OFF); substract a fixed DC offset in ADC-units here.
  digitalWriteFast(PIN_DEBUG, LOW);

  cnt++;
  if (cnt >= 1500) { // 500 = publish every 0.5s
    readRms.publish();
    float RMS=readRms.rmsVal;
    float AccRMS=RMS*128;
    int RMSout=AccRMS;
    Serial.print("Vrms=");
    Serial.print(RMSout);
    int a1=RMSout/100;
    int b1=RMSout%100;
    int a2=b1/10;
    int a3=b1%10;
    if (RMSout<200) { // Red
      SetNumWithColor(0, 0, a1,  0, 'r'); // displays a1 at digit 0
      SetNumWithColor(0, 1, a2,  0, 'r'); // displays a2 at digit 1
      SetNumWithColor(0, 2, a3,  0, 'r'); // displays a3 at digit 2
    }else if(RMSout<215){ // Orange
      SetNumWithColor(0, 0, a1,  0, 'o'); // displays a1 at digit 0
      SetNumWithColor(0, 1, a2,  0, 'o'); // displays a2 at digit 1
      SetNumWithColor(0, 2, a3,  0, 'o'); // displays a3 at digit 2
    }else{ // Green
      SetNumWithColor(0, 0, a1,  0, 'g'); // displays 1 at digit 0
      SetNumWithColor(0, 1, a2,  0, 'g'); // displays 2 at digit 1
      SetNumWithColor(0, 2, a3,  0, 'g'); // displays 3 at digit 2
    }
    Serial.print(", Vin=");
    Serial.println(RMS,4);
    // float dc=readRms.dcBias;
    // Serial.println(dc,4);
    cnt=0;
    // readRms.start();  // Restart the acquisition after publishing if the mode is single scan.

  }

  while (nextLoop > micros());  // wait until the end of the loop time interval
  nextLoop += LPERIOD;  // set next loop time to current time + LOOP_PERIOD

}

// end of Measure_rms.ino

Without seeing your code it's hard to guess completely, but this is my supposition:

The RMS measurement has to measure rapidly over a short period to capture the peak values of the AC waveform
You are updating the display while doing the measurement
The display updates slow down the sampling of the AC waveform giving you incorrect values.

You need to make sure that the sampling of the AC waveform happens fast enough by not doing anything else (much) during the sampling period and only displaying anything on the display when all the sampling for the current data point is done.

Thank you. Before displaying, the RMS over two periods is calculated. So, updating occurs after measurement and calculation. The code is here (https://github.com/MartinStokroos/TrueRMS). Actually, I plotted only A0 using "Serial Plotter" of Arduino. I could see that the amplitude is obviously changing fast in circuit #2!!!! while the multimeter didn't confirm this. But for circuit #1, it didn't occur at all. Why? Does Max7219 affect the input A0 or sin wave of main voltage?

That's just the library and examples. It's not your code.

I added my code. Thanks

@soheil the answer applies for setup() too


text:
Mouser/DigiKey part number for female "stacking" headers?
Adafruit has this wonderful product: http://www.adafruit.com/products/85

What are these actually called? I've looked all over the place in Mouser and DigiKey and can't find them (now, granted, it's impossible to find anything in DigiKey since they discontinued their printed catalog, but I'm digressing...)
They are great for putting other things that are on breakout boards on a tightly-packed breadboard and still be able to attach probes.
Also, sometimes, the board you are trying to attach is this:
As you can see, there is a dip switch blocking the 8-pin header. This is fine for its final resting place (on a board with a socket), but putting it on a breadboard is a bit of a pain. I have quite a few of those, so even though the aforementioned adafruit part is pretty cheap and I could get a bunch, I'd rather know what the real part is called so I can get the sizes I need.
PS: In case you are wondering, and you like old-style nixie tubes, that's http://www.tayloredge.com/storefront/SmartNixie/DataSheets/Datasheet_SmartNixie.pdf.)

A Digikey.com search for samtec SSQ includes numerous longtail stackable headers (as well as some shorttail, or non-stackable).  However, looking at Digikey's prices suggests to me that adafruit.com's price is a bargain unless you are purchasing thousands of pieces.
Also see an arduino.cc thread on this topic.

This one has la posta, search for samtec SSQ and you'll get the stackable pins in all sizes.

They're actually called "Wire-Wrap Headers" and they pre-date the Arduino probably by at least 50 years or so.
They used to be used to create circuits between chips and components by wrapping wire around the pins and routing it to other connectors:

They have since been adopted as stackable headers on boards like the Arduino shields.
Being such old technology not many people make them any more, though thanks to their popularity on things like the Arduino they are starting to appear more and more from normal electronics supply companies.

Thanks. I'm old enough to know about WW headers, but the pins on these are not thick enough to be usable for WW, and the receptacle part is also too high. I've looked for WW  headers on mouser and digikey, and none match these. What I can find are the nice, machined (round, that is) receptacles that are meant for plugging in ICs. I have lots of those from days past, but they are too narrow to accept other headers.

If you are already ordering from Digi-Key, they do carry the same set of stackable headers from Adafruit.  It's just a bit hidden in an accessories family.  Otherwise it looks like Samtec is the only company Digikey carries that makes this style of connector.  Some dual-row long tail headers can be found using the term stackthrough, but this is mostly used in conjunction with PC104 connectors.

You can find stackable headers on eBay (ie. 141063314400 , 191576393028 , 271841707614). Futurlec also sell them pretty cheap - about half-way down the page.

That DIP switch is also connected to the 4 address pins so you could cut the switch off, plug it into a proto board and then set the address by selectively grounding those pins.


text:
What is the difference between Serial.write and Serial.print? And when are they used?
What is the difference between Serial.write and Serial.print? And when are they used?
Both have been used to print on serial monitor, what are their actual differences?

Serial.write is more down to earth , it is simple and fast, it is made to talk binary, one byte at a time.
example:
Serial.write(0x45);   // will write 0100 0101 to the cable

Serial.print in the other hand is more versatile , it will do the conversion for you from ASCII to binary it also can convert to BIN/ HEX/OCT/DEC but you need to specify a second argument like so
Serial.print(76, BIN) gives "0100 1100"
Serial.print(76, OCT) gives "114"
Serial.print("L", DEC) gives "76"
Serial.print(76, HEX) gives "4C"

more examples with visual serial output :
Code:
  Serial.write(0x48);   // H
  Serial.write(0x45);   // E
  Serial.write(0x4C);   // L
  Serial.write(0x4C);   // L
  Serial.write(0x4F);   // O

SERIAL OUTPUT :

Code:
  Serial.print("HELLO");

SERIAL OUTPUT :

Serial.println() in the other hand will add end of line 2 bytes 0x0D and 0x0A as you can see in the frame
Code:
  Serial.println("HELLO");

SERIAL OUTPUT :

From the Arduino site for Serial.write and Serial.print:
Serial.write()

Writes binary data to the serial port.
This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Serial.print()

Prints data to the serial port as human-readable ASCII text.
This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is

All of the examples above are correct, but maybe more clearly.... All data sent through the serial port is sent as 1's and 0's. (obvious.... I hope)... The difference in the two commands is how whatever is being sent is actually translated/interpreted to/from these 1's and 0's. The clearest example involves transmitting numbers.
Let's say you need to send the number 217. The binary (1's and 0's) representation of this number is 11011001. Using the command Serial.write(217) will literally just send 11011001 across the line. The Hex representation of the same number is 0xD9, and the command Serial.write(0xD9) will send the same thing ... 11011001.
Here is where it gets interesting... If you were to use the command Serial.write("217") you would instead get this: 00110010 00110001 00110111... WHAT?!?!?
When a string is passed as the argument it is broken down into individual characters, converted to ASCII, and then sent as a byte for each character. You would get the exact same output if you were to use the following:
Serial.write(50);
Serial.write(51);
Serial.write(55);

(00110010 00110001 00110111)
Now let's look at Serial.print(). The commands Serial.print(217) or Serial.print("217") will both print the same thing: 00110010 00110001 00110111. This is because the print command first converts any number to a string representation and then uses the command Serial.write() to send each character as individual ASCII bits.
Though this is not a COMPLETE description, I hope it helps to get you thinking in the right direction...

Serial.write sends bytes to the serial port while Serial.print sends ASCII characters so people can read easily.
Some devices work using bytes to set configurations, commonly use packets of data and you need to use write function to communicate with them. Eventually they will send bytes through serial interface and then you can interpret by going each byte.

Yet another way to answer this is to state the Serial.write accepts single characters where Serial.print accepts strings. There may be some differences but this is the main one.

What about Serial.write(stringData.c_str());? if stringData is defined as String.

Please show how stringData is defined as String.


text:
Has anyone used an Arduino to transfer software to an Apple IIe?
Yes, you read that right.  An Apple IIe.
I have many retro computers and I was thinking that since the Apple II line of computers (especially the IIe) have pretty speedy serial ports that it should be somewhat simple to use an Arduino to send data to/from the Apple.
My issue is that all of my every-day computers are USB based with no RS232 serial.  I guess I could boot up an old Linux laptop that has a serial port but that seems too cumbersome given my limited desk space.  :-)
Anyway, I've Googled but I'm not finding anything.
Since most Apple IIe disk images are pretty small (<144k or so) they should fit pretty easily into, say, an Arduino DUE that I have.  Or better yet, using the DUE to mount as a USB device that could send over to the IIe would be great.
So, my question is, does anyone know of any site that explains how this could be done?
Thanks!
EDIT
OK, turns out it WAS pretty easy getting the Arduino DUE to send to the Apple IIe.  I simply connected 3 wires from TX1/TR1/GND to the Apple IIe serial port (SSC).
Now, when I launch ADT Pro and start sending, the IIe is receiving garbage.  Bunch of "9d9d9d9d" all over the place.
Here's my code:
// ADT.ino

int incomingByte = 0;

void setup() {

    Serial.begin(9600);
    Serial1.begin(9600);

}

void loop() {

    if(Serial.available() > 0) {

        while(Serial.available() > 0) {

            incomingByte = Serial.read();

        }

        Serial1.write(incomingByte);
        delay(100);
    }

}

The ADT Pro software is set to send the data over the USB port and it's obviously sending data to the IIe.  I'm just wondering if the formatting is wrong or something.

The Arduino part is easy peasy. Getting the ][e stuff is going to be the hard part.

What baud rate does IIe expect? | Is polarity correct? | Are there pauses between characters? | presumably sending N81? - what does IIe expect (no polarity or odd or even? 1 start almost always, 1 stop or two (irrelevant with gaps between)). Try a small pause between sent characters. Are you sending RS232 levels or TTL? ....

To be honest, I really don't know at this point. I think it expects RS-232 but I'm not sure.  For baud rate, everything I have read says it should be able to handle up to 19,200 and I've tried that and 9600.  This is where I'm getting my information from:  http://adtpro.sourceforge.net/connectionsserial.html

May be useful- http://adtpro.sourceforge.net/bootstrap.html | Maybe -> http://pdw.weinstein.org/2007/06/apple-hacking-for-fun-and-profit.html | LOOKS very useful -> http://mirrors.apple2.org.za/ground.icaen.uiowa.edu/Docs/SerialPortFirmwareInterfaceCommands.txt | Maybe -> http://apple2online.com/index.php?p=1_13_Apple-II-IIe | Wow !!! -> http://www.applelogic.org/UserManuals.html |

Realistically, you'd be better off with an off the shelf USB-serial (not "USB-TTL" as used with Arduinos) already incorporating the necessary inverting level translation.  Drastically cheaper, too.

You need to adapt the levels. Arduino Due operates on 0 - 3.3 V. Apple IIe is either on 0 - 5 V or +/- 12 V (depends which port you are using). There are standard ICs to do the level adaptation.

That's important yes.  But equally important to success is that the signals must be inverted.

Yes, correct connection and correct level. Non-adaptation of the voltage level may lead to the destruction of the Arduino board. A typical chip would be in the MAX220-MAX249 series from Maxim, and many other providers have compatible chips.

I went ahead and marked this as the answer.  My mistake is not realizing the differences in TTL vs. RS232.  I'm lucky I didn't fry my DUE!  I have always just plugged serial cables into that DB9 connector and never really understood what it was doing (like most people).  Only now, I need to know HOW it works because I want to build an SD card reader for my Apple IIe.  Anyway, I have bought the correct cable/converter off eBay.  In the meantime, I'm going to mess around with some MAX232 chips I have and experiment.  This time, with a better understanding.  :-D


text:
arduino rfid with led, code is compiling but not working as expected!
code is compiling, what I want is that when rfid tag kept led should get on but instead its getting to the else loop.
int count = 0;
char input[12]; // rfid input
int led = 3; // led pin
char first[20] = {"270059204B15"}; //rfid tag no
char a[24];

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

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) // code for rfid input
  { count = 0;
    while (Serial.available() && count < 12)
    {
      input[count] = Serial.read();
      count++;
      delay(10);
    }
    Serial.print(input);
  }

  if (strcmp(input, first) == 0) /*comparing two string rfid tag no and input
   if matches led is on*/
  {
    digitalWrite(led, LOW); /* as the board which we are using is in sink
    mode low is taken as 1 or on*/
    delay(500);
    Serial.print("led is on\n");
  }

  else
  { digitalWrite(led, HIGH);
    delay(500);
    Serial.print("led is off\n");
  }
}

Could you clarify your question, provide step by step explanation on how you want it to work(or how you expected it as per title)? Something like a program flow.