text:
Can't upload to arduino from clion
I'm trying to upload a script to my arduino uno (mega and nano doesn't work either). It works unless I #include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <SimpleDHT.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET  4

Adafruit_SSD1306 display(OLED_RESET);

// initializing foo here. const static unsigned char foo_bits[] PROGMEM = {0x00, 0x00 ///

void draw(void) {
    display.drawXBitmap( 0, 0,foo_bits, foo_width, foo_height, 1);
    display.display();
}

void setup(void) {
    display.begin();
    display.clearDisplay();
}

void loop(void) {
    draw();
}

The error I'm getting is
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `Adafruit_SSD1306::ssd1306_command(unsigned char)':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `TwoWire::beginTransmission(int)'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `TwoWire::write(unsigned char)'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `TwoWire::write(unsigned char)'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `TwoWire::endTransmission()'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `Adafruit_SSD1306::begin(unsigned char, unsigned char, bool)':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::begin()'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `SPIClass::beginTransaction(SPISettings)':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::interruptMode'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::interruptMode'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::interruptSave'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::interruptMask'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `SPIClass::interruptSave'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `Adafruit_SSD1306::begin(unsigned char, unsigned char, bool)':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:416: undefined reference to `TwoWire::begin()'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `Adafruit_SSD1306::display()':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:472: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:472: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:472: undefined reference to `TwoWire::beginTransmission(int)'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `TwoWire::write(int)':
/opt/arduino/hardware/arduino/avr/libraries/Wire/src/Wire.h:78: undefined reference to `Wire'
/opt/arduino/hardware/arduino/avr/libraries/Wire/src/Wire.h:78: undefined reference to `Wire'
/opt/arduino/hardware/arduino/avr/libraries/Wire/src/Wire.h:78: undefined reference to `TwoWire::write(unsigned char)'
libuno_Adafruit_SSD1306.a(Adafruit_SSD1306.cpp.obj): In function `Adafruit_SSD1306::display()':
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:475: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:475: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:475: undefined reference to `TwoWire::write(unsigned char)'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:479: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:479: undefined reference to `Wire'
/opt/arduino/arduino-1.8.5/libraries/Adafruit_SSD1306/Adafruit_SSD1306.cpp:479: undefined reference to `TwoWire::endTransmission()'
collect2: error: ld returned 1 exit status

My cMake file is
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(PROJECT_NAME touch)
project(${PROJECT_NAME})
include_directories(/opt/arduino/arduino-1.8.5/libraries/)
set(${CMAKE_PROJECT_NAME}_SKETCH touch.ino)

set(${CMAKE_PROJECT_NAME}_BOARD uno)
set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyACM0)
generate_arduino_firmware(${CMAKE_PROJECT_NAME})

If I remove all of the Adafruit_SSD1306 uploading works. The exact same program works from Arduino IDE. What could be the cause and how can I fix this?

It seems that your posted sketch doesn't match with yours error messages. You never declared those foo_* variables. Did you update your libraries?

The foo variables are all declared (see the commented line in the sketch, just wanted to keep the code short and foo_bytes is quite long). The sketch works from Arduino IDE and the libraries are updated. @LookAlterno

@LookAlterno But yeah - I don't see any kind of connection between sketch and error too, what makes this very hard to fix for me...

The error is in the library, not in your sketch. That's happens from time to time. Your code compile fine in my machine (after adding foo_* definitions) using Arduino 1.8.5 and UECIDE 0.9.1. Patience.

Patience? For what? @LookAlterno

There are some brilliant people here. Wait for them


text:
Reducing Memory Usage lots of Floats
Im using an Arduino Uno - All code at github.com/robbrad/AirQuality
Current sketch usage:
Sketch uses 23186 bytes (71%) of program storage space. Maximum is 32256 bytes.
Global variables use 1428 bytes (69%) of dynamic memory, leaving 620 bytes for local variables. Maximum is 2048 bytes.

I wouldn't normally just spam code for a optimisation - but im looking to get about 10% memory back from this sketch
It basically checks a bunch of sensors sets the values to floats, build a string and posts that to a php page where some more processing happens.
I have tried everything to my knowledge but I just cant think of a smarter way to use less memory.
I tried an array, a function to build the data string, using one float and concatenating it to the string when it set
I even thought of reducing the Math.h include to just the math functions I was using
Maybe im just reaching the little boards limits (which are pretty good anyway :) ! )
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#include <math.h>
#include <Wire.h>
#include "MutichannelGasSensor.h"
#include "HP20x_dev.h"
#include "KalmanFilter.h"

unsigned char ret = 0;

/* Instance */
KalmanFilter t_filter;    //temperature filter
KalmanFilter p_filter;    //pressure filter
KalmanFilter a_filter;    //altitude filter

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192, 168, 0, 30); // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 40);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

#define Vc 4.95
//the number of R0 you detected just now
#define R0 35.54
#define DHTPIN A1     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 2000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
String data = "";

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(150);
  HP20x.begin();
  delay(100);

  /* Determine HP20x_dev is available or not */
  ret = HP20x.isAvailable();

  pinMode(8, INPUT);
  starttime = millis();//get the current time;
  //Serial.println("power on!");

  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  //if (Ethernet.begin(mac) == 0) {
  //  Serial.println("Failed to configure Ethernet using DHCP");
  // try to congifure using IP address instead of DHCP:
  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(10000);
  // Serial.println("connecting...");
  //}
  // give the Ethernet shield a second to initialize:
  //delay(1000);
  //Serial.println("connecting...");

  // Serial.println(Ethernet.localIP());

  gas.begin(0x04);//the default I2C address of the slave is 0x04
  gas.powerOn();
  //Serial.print("Firmware Version = ");
  //Serial.println(gas.getVersion());
  //Serial.println("Particles\tRS\tHCHO (PPM)\tNH3 (PPM)\tCO (PPM)\tNO2     (PPM)\tC3H8 (PPM)\tC4H10 (PPM)\tCH4 (PPM)\tH2 (PPM)\tC2H5OH (PPM)");
  dht.begin();
  delay(10000);

}

void loop() {

  if (client.available()) {
    char c = client.read();
    //  Serial.print(c);
  }

  float HATemp2;
  float HAPres2;
  float HAAlt2;
  float d;

  if (OK_HP20X_DEV == ret)
  {
    unsigned long HATemp = HP20x.ReadTemperature();
    d = HATemp / 100.0;
    HATemp2 = t_filter.Filter(d);

    unsigned long HAPres = HP20x.ReadPressure();
    d = HAPres / 100.0;
    HAPres2 = p_filter.Filter(d);

    unsigned long HAAlt = HP20x.ReadAltitude();
    d = HAAlt / 100.0;
    HAAlt2 = a_filter.Filter(d);

  }

  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy + duration;

  if ((millis() - starttime) >= sampletime_ms) //if the sampel time = = 30s
  {
    ratio = lowpulseoccupancy / (sampletime_ms * 10.0); // Integer percentage 0=>100

    d = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
    //Serial.print("concentration = ");

   lowpulseoccupancy = 0;
    starttime = millis();
  }

 //Serial.print("\t");
  //HCHO

  int sensorValue = analogRead(A0);
  double Rs = (1023.0 / sensorValue) - 1;

  double ppm = pow(10.0, ((log10(Rs / R0) - 0.0827) / (-0.4807)));
  //Serial.print("HCHO ppm = ");

  //MultiChannel Gas
  float NH3 = gas.measure_NH3();
  float CO = gas.measure_CO();
  float NO2 = gas.measure_NO2();
  float C3H8 = gas.measure_C3H8();
  float C4H10 = gas.measure_C4H10();
  float CH4 = gas.measure_CH4();
  float H2 = gas.measure_H2();
  float C2H5OH = gas.measure_C2H5OH();

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  data = String("dust=") + d + "&rs=" + Rs + "&hcho=" + ppm + "&nh3=" + NH3 + "&co=" + CO + "&no2=" + NO2 + "&c3h8=" + C3H8 + "&c4h10=" + C4H10 + "&ch4=" + CH4 + "&h2=" + H2 + "&c2h5oh=" + C2H5OH + "&temp=" + t + "&hum=" + h + "&HATemp=" + HATemp2 + "&HAPres=" + HAPres2 + "&HAAlt=" + HAAlt2;

  //Serial.println(data);

  if (client.connect(server, 80)) {
    Serial.println(F("connected"));
    client.println(F("POST /air_add.php HTTP/1.1"));
    client.println(F("Host:  192.168.0.30"));
    client.println(F("User-Agent: Arduino/1.0"));
    client.println(F("Connection: close"));
    client.println(F("Content-Type: application/x-www-form-urlencoded;"));
    client.print(F("Content-Length: "));
    client.println(data.length());
    client.println();
    client.println(data);
  }
  else
  {
    Serial.println(F("could not connect"));
  }

  Serial.println(data);

  if (client.connected()) {
    client.stop();
  }

  delay(300000);
}

For which Arduino board? Are you trying to reduce ram or flash memory? What does the compiler say about ram and flash memory usage? Those few float variables on the stack are not the problem. The libraries might use a lot of memory. If no library uses the 'String' object, then you should try to avoid that. You can also send bare sensor data, and do the calculation and processing in the server.

It’s an Uno. The compiler didn’t complain but it’s right on the 70% and 69% limi for both respectively. I had a quick look at the libraries but not seeing anything jumping out at me. Maybe I’ll make a sketch with just the libraries to see just their usage

Please update your question with the extra information. You could add links to the used (non-Arduino) libraries, so we can have a look at them. For an Arduino Uno, you better get rid of the 'String' object. Even though that would result into 20 lines of code or more. The Ethernet shield is mostly used with an Arduino Mega 2560 or a Arduino Due, because the Uno has not enough memory.

Done - https://github.com/robbrad/AirQuality

Thanks, but those are only the *.h files, not the used libraries. I suggest to remove the 'String' class, and if that is not enough then replace the Arduino Uno with a board that has more memory (you probably are going to do that some day anyway).

Just added the cpp files

Why do you want to get 10% back? Even if the program memory is 99% full it won't cause any problems, unless you want to add more things. Even using 70% of RAM isn't outrageous. The linker tends to optimize heavily, so it is (probably) only including the routines you actually reference.

@NickGammon 10% was an arbitrary number, as I noticed intermittent issues when I got to this level of usage, of course that might not be related. The issue I saw was some times it would output all the values other times it would output nothing.

Is this your KalmanFilter? I can't quite make sense out of it. To me, it looks silly and very memory intensive. Couldn't you use a basic first-order low-pass instead?

You haven't replied to the first comment. Do you want to save RAM or PROGMEM? They have quite different purposes. You may have heap fragmentation due to your use of the String class which I suggest you stop using (as others have advised).

@NickGammon I have stopped using String and it seems to have improved the stability - I was trying to save PROGMEM

@EdgarBonet - yes thats the one, I dont have the experience on this - it was recommended as part of the sensor implimentation

Even if you used 99.9% of PROGMEM, that would not affect stability. However with string fragmentation (particularly caused by the String class) even 50% of RAM used might be too much. It looks like you have found the problem.

@NickGammon +1 your spot on. This looks like the root cause of the issue. I’m exploring alternative methods of sending the data now

@Rob. As you talk about PHP I think you're a software dev (like me). I suggest to have a look at the diff. between Harvard and Von Neumann architecture. Arduino is NOT a computer. Optimizing PHP code will give you more space (Von Neumann architecture) but optimizing the sketch will NOT give you more space for variable (Harvard architecture).

@Peter your too kind sir, I’m more a jack of all trades master of none. But your right I’m fast realising it’s quite a different arena to  work in and not all initially learned principles apply

Your use of the F macro (eg. Serial.println(F("connected"));) is very sensible. Without it, the compiler copies the string into RAM, further reducing available RAM. Done with the F macro the compiler generates code to pull the constant data directly from PROGMEM.

I counted the floats in the sketch:
Global: 2
setup: 0
loop: 14
Total: 16 floats -> 64 bytes.

If you want to reduce that, don't wait to have all your values before preparing your answer. You will use less memory if you do
  data = String("dust=") + dht.readHumidity();
  data+= "&rs=" + (1023.0 / sensorValue) - 1;
  data+= "&hch0=" + pow(10.0, ((log10(Rs / R0) - 0.0827) / (-0.4807)));
  ... and the rest ...

Your data String and concat's take way more space than the floats. Use char[] instead of string.
As last resort, you don't need to construct the complete POST before sending it to the server. Send it line by line, value by value. Use a fixed, big enough Content-Length:,  padding with spaces after you send the values and know the actual lenght.
Or you can switch your technology: MQTT need way less memory than HTTP.

MQTT might be the ticket - I might send the value as soon as I have it

hmm interesting - I tried char data[15]; and the String("dust=") didn't like it - My understanding is data[] would now be an array - is that right?

@Rob. You use char[] instead of String(), not with or intermixed. You have to go low-level, char by char, byte by byte.

There have been some great responses on this question. It was hard to accept an answer, but as this one mentioned the String issue first I choose this one

Two basic approaches:
1) use fixed point math. or
1) use int16_t or int32_t in lieu of the floating types. for example, you can use x10 or x100 to designate variables with some decimal points. use x16 or x128 or even x256 for speed gains as well. you will need to make sure that it doesn't overflow.
edit: here is an example that hopefully will help you get it going.
void setup() {
    Serial.begin(9600);
    var1=100;               //var1=1.00
    var2=123;               //var2=1.23
}

void loop() {
    var1+=var2;             //increment var1
    Serial.print("var1 = "); Serial.print(var1 / 100); Serial.print("."); Serial.print(var1 - (var1 / 100) * 100); Serial.println(".");
    delay(100);
}

var1 (1.00 initially) is incremented by var2 (1.23), both are int32_t types.
here is the printout.

Ok so what you're saying is I can use int16_t  instead of a float? do I have to cast the result in some other way? doing more reading now

That’s pretty cool, never done this before. So var1 is of type int32_t. So when it’s taken to the string it’s only converted to the correct precision at the point - is this the essence of fixed point math?

So, are you using int32_t instead of float? Both are 4 bytes long.

@dannyf What software are you using in the last picture?  Is that an emulator?

" is this the essence of fixed point math?" yeah. fixed point math is in base 2, and what I have shown is in base 10. using base 2 speeds up calculation and using base 10 is more intuitive.

I second the advice that has already been given to you about avoiding
String. Other than that, using fixed-point math could be a big win,
but given the math you do it won't be any easy.
Assuming you stick with floating point, I just found a couple of easy
optimization opportunities:

Do not use pow to evaluate a polynomial. Use the Horner's
method which is way
cheaper: d = ((1.1*ratio - 3.8)*ratio + 520)*ratio + 0.62.
The expression of ppm can be simplified:
ppm = 2500.33 * pow(Rs, -2.0803).

Edit: You could also try to implement chunked transfer
encoding. This
way you could send the data to the server on the fly, as you collet it,
instead of storing everything in memory and sending it in one go.
Example:
void send_chunk(Print &connection, const String &chunk)
{
    connection.println(chunk.length, 16);
    connection.println(chunk);
}

void loop() {
    // ...

    // last header sent to the client:
    client.println(F("Transfer-Encoding: chunked"));
    client.println();

    // Now the data:
    String chunk;
    chunk = "dust=";
    chunk += ((1.1*ratio - 3.8)*ratio + 520)*ratio + 0.62;
    send_chunk(client, chunk);
    chunk = "&rs=";
    chunk += Rs;
    send_chunk(client, chunk);
    chunk = "&hcho=";
    chunk += 2500.33 * pow(Rs, -2.0803);
    send_chunk(client, chunk);
    chunk = "&nh3=";
    chunk += gas.measure_NH3();
    send_chunk(client, chunk);
    // and so on...
    chunk = "";
    send_chunk(client, chunk);  // Empty chunk means end of message.

    // ...
}

Notice that there is no "Content-Length" header. Instead, each chunk is
preceded by its size in hex. And, most importantly, notice that this way
you can get rid of lots of variables.

Edit 2: I came out with a better (more generic) way to implement
chunked encoding, which involves sending the data through a “chunker”
filter that does the encoding and outputs to the client. Here is my
Chunker class:
/*
 * Transfer a message using the HTTP/1.1 "Chunked Transfer Coding".
 * C.f. https://tools.ietf.org/html/rfc7230#section-4.1
 */
class Chunker : public Print
{
public:
    Chunker(Print &downstream) : downstream(downstream), pos(0) {}

    virtual size_t write(uint8_t c)
    {
        buffer[pos++] = c;
        if (pos == BUFFER_SIZE)
            send();
        return 1;
    }

    void end()
    {
        if (pos) send();
        send();  // empty chunk means end of message
    }

private:
    static const uint8_t BUFFER_SIZE = 32;
    Print &downstream;
    uint8_t buffer[BUFFER_SIZE];
    uint8_t pos;

    // Send the buffer as a chunk.
    void send()
    {
        downstream.println(pos, HEX);
        if (pos) {
            downstream.write(buffer, pos);
            downstream.println();
            pos = 0;
        }
    }
};

And here is how you would use it:
Chunker message(client);  // instantiate the filter
message.print(F("dust="));
message.print(((1.1*ratio - 3.8)*ratio + 520)*ratio + 0.62);
message.print("&rs=");
message.print(Rs);
message.print(F("&hcho="));
message.print(2500.33 * pow(Rs, -2.0803));
message.print(F("&nh3="));
message.print(gas.measure_NH3());
// and so on...
message.end();  // terminate the chunked message

The main advantage of this method is that you completely avoid using the
String class, which means no dynamic memory allocation (a very
desirable thing). Also you can choose your buffer size. I've set it to
32 bytes, but you can make it smaller if needed.

@Rob: I added another suggestion about the usage of the HTTP protocol.

Thats really cool - thank you for spending some time on this - I think im edging towards this approach

@Rob: Here is a more generic way of using chunked encoding, which avoids String, or even the need to guess how mush space you need to sprintf() a number.

Thats pretty amazing ... I just need to work out the particulars like the connection part. My Endpoint is a php page/server which does a load more things with the POST request - would this be a good place to start https://dancingmammoth.com/2009/08/29/php-stream-filters-unchunking-http-streams/

@Rob: I don't know much about PHP. Not sure whether you have to take for the encoding yourself. The link you provide is for adding chunked support to PHP streams, which are a useful tool for handling very large payloads. You payload is tiny, so you don't need them. Also, I assume your web server will take care of the decoding step, at least if using mod_php. Note, however, that there is a PHP bug that prevents this to work in fast CGI mode.

Thank you so much for your help it’s really helped my understanding of what’s going on. I have found a link to send as a char array https://github.com/tttapa/Projects/blob/master/ESP8266/HTTP/HTTP-POST-char-array/HTTP-POST-char-array.ino - which should cut down my redevelopment of the receiver. This question has helped me hone in on the issue which I now believe was directly related to the dynamic allocation of the string. This was either blowing the dynamic memory or fragmenting the string variable, I noticed some times only half the string was posted. Thank you

source: Stack Exchange

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

sort: votes


text:
Why it's not possible to manipulate the TWSR directly?
I think I forgot the reason for this SFR manipulation in microcontrollers; like, checking the TWSR in twi.
The technique for checking the values of the TWSR is by masking it with another variable or a value.
So, why not checking the TWSR directly in; for example, switch function for checking the values of TWSR?
Like, switch (TWSR)
instead of
uint8_t Status;
  Status = TWSR & 0xF8;
  switch(Status)
Or, substitute it in any function; like, Serial.println(TWSR); or digitalRead(TWSR);. DO I have to mask it? If so then why?

switch is not a function; it doesn't provide a result. switch is nothing else than an if-else in disguise. You can't change what switch do, nor re-implement it in any other way. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

bit 0 and bit 1 of the TWSR register specify the I2C clock speed, so they may differ depending on the speed you selected (Wire.setClock())

What I mean is that why I have to mask TWSR instead of substitute it in any function parameter?

You mask it to ignore bits you don't care about.  In a switch, you'd have to list all of those as explicit cases which would be inefficient to express in  source code, even if a very optimizing compiler might figure out how to condense the actual tests to ignore the irrelevant bits.

OK, then why don't just check for TWSR values even with bit 0 and bit 1 included in the test, because they have ..... or wait. I'm sorry, it must be masked because these two bits may change between different I2C clock speeds.

Why it's not possible to manipulate the TWSR directly?
  i2c c switch

Because your premise is wrong: it is routinely done to manipulate twsr directly.
As to the use of masks, well, because sometimes you only care about certain bits but not others. So you mask away those that you don't want.
Just that simple.

OK, so all SFRs are accessible for direct manipulation. But this one is masked for unwanted bits. Thank you :)

The mask is a must, because bit 0 and bit 1 are I2C clock speed bits and they are changeable between different I2C clock speed configurations.

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


text:
Strange Output from DS1307 RTC
I'm using a DS1307 RTC (https://www.adafruit.com/product/3296) and an Arduino mega microcontroller trying to simply set and read the time of the RTC. However, whenever I try to output anything to the Serial Monitor, I get stuff like this:

The code and the wiring is fairly straightforward which is why I'm confused. I'm just directly attaching the Vcc, GRND, SCL and SDA pins of the clock to the Arduino and using a slightly modified version of the example code as a part of the RTC library Adafruit supplied.
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  while (!Serial); // for Leonardo/Micro/Zero

  Serial.begin(57600);
  rtc.begin();

  rtc.adjust(DateTime(2017, 11, 17, 20, 33, 0));

}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.println();
    delay(1000);
}

So I know I'm doing something wrong but I'm not sure what. Any help would be hugely appreciated.

Can you post a schematic of how you have the DS1307 wired?

Looks like a baud rate issue. Check your clock speed and settings.

Set the serial monitor to 57600 and check.

@ammar.cma In the code above you can see that I did set the serial monitor to 57600

@Trevor What should I be changing it too exactly?

@ThreePhaseEel like I said in the above posted I literally just have it plugged directly into the SDA, SCL, 5V and Ground pins of the arduino.

No idea, it depends on the frequency of your micro and what you are connected to.

I mean not in your code, but in the monitor that you display on : So check on the bottom right on that window, there should the a baud rate that can be set.  COM4 window

Change the baud rate to 57600, it specifies at:
    Serial.begin(57600);

Thank you for the advice @senpaiscuba. I am new to stackoverflow and did not realize you could edit after posting.


text:
Arduino Timer 0 in fast PWM mode and micros()
I have the same problem as described on this topic
I needed to set my prescale factor for Timer0 to 1 so I can get a 62.5Khz PWM out of pin 4
My issue is that now the delays() function on my sketches doesn't work correctly.
As mentioned also in the above topic, I've changed the MICROSECONDS_PER_TIMER0_OVERFLOW macro on the wiring.c library, but this doesn't fix the delay() function.
I understand I need to fix the micros() function as well, however I'm confused on how to address the problem.
Isn't the Timer0 Interrupt overflowing 64 times faster now?
So dividing the micros by 64 shouldn't address the problem

This question would get better answers if you asked on the Arduino SE. Questions for the Arduino on this website is more for people who want to know the hardware side of the Arduino. I'm to going to vote to migrate this question to the Arduino SE.

if the timer is counting 64 times faster, then you need to delay for 64 times longer.

Modifying standard libraries is a bad idea. The answers in your linked question tell you everything you need to know - "The best workaround is to only use millis() for your timing critical functions." If that isn't good enough then write your own delay() and micros() equivalents.


text:
Arduino Serial data communication with NodeJS
I am using an Arduino Uno and a DTH11 temperature sensor to read temperature and then send that value to a computer via the serial port.
A NodeJS application is running on the computer and captures that value and writes to a log file.
When I try to get values from my NodeJS application, sometimes the value is broken into two values. Eg: 25 -> 2 and 5
This is my Arduino code:
dht DHT;

#define DHT11_PIN 7
float tem [4];

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT);
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  int val = (int)(DHT.temperature);
  Serial.println(val);
  delay(600000);
}

This is my NodeJS program.
var fs = require('fs')
var logger = fs.createWriteStream('/data/Temperature/temperature.csv', {
  flags: 'a' // 'a' means appending (old data will be preserved)
})

var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600,
});

serialPort.on("open", function () {
  serialPort.on('data', function(data) {
    var moment = require('moment');
    var dayTime = moment().format('YYYY-MM-DD,hh:mm');
    var val1 = data.toString();
    var val = val1.trim();
    if (val == '') {
      console.log('no data');
    else {
      console.log(val);
      logger.write(dayTime + ',' + val + '\n');
    }
  });
});

Is it a problem with my code?
Thanks in advance.

You assume that all the serial data will arrive in one packet. That's is not the case most of the time. You need to code to read data char by char and reconstruct the value in the receiving side.

@LookAlterno yes, sometimes its getting data properly. So how I can get char by char?

You concatenate parcial readings until reaching a delimiter that signal you the end of the data. I think something like val1 = val1 + data.toString(). Maybe. I don't program in Javascript nor PHP.

As Look Alterno wrote in a comment, you need to parse the incoming data on the receiving side. In this instance, you have to look for the line terminators. The Node.js serialport module provides a parser for this called Readline. Read that doc and use it.


text:
Programming standalone 328P in circuit
I am designing a PCB using a 328P. As I understand it, I can either flash the chip via the ICSP header (without bootloader) or via Serial (if it has a bootloader already). But how does programming actually work?

According to my program running on the chip, it acts as master for SPI peripherals. If using ICSP, how does the chip know that it is supposed to act as SPI slave and receive a program?
Is there anything I must take into account with regards to what is connected to the SPI pins? i.e. does it harm in any way if I have other SPI slaves on the board connected to these same pins?
Same for serial programming: how does the chip know that it's supposed to receive a program on the serial interface, when my application uses this port for a different purpose?

According to my program running on the chip, it acts as master for SPI peripherals. If using ICSP, how does the chip know that it is supposed to act as SPI slave and receive a program?

A special signal is sent using the RESET pin. From the datasheet (p361):

Power-up sequence: Apply power between VCC and GND while RESET and SCK are set to “0”. In some systems, the programmer can not guarantee that SCK is held low during power-up. In this case, RESET must be given a positive pulse of at least two CPU clock cycles duration after SCK has been set to “0”.
Wait for at least 20ms and enable serial programming by sending the Programming Enable serial instruction to pin MOSI.
The serial programming instructions will not work if the communication is out of synchronization. When in sync. the second byte (0x53), will echo back when issuing the third byte of the Programming Enable instruction. Whether the echo is correct or not, all four bytes of the instruction must be transmitted. If the 0x53 did not echo back, give RESET a positive pulse and issue a new Programming Enable command.

Is there anything I must take into account with regards to what is connected to the SPI pins? i.e. does it harm in any way if I have other SPI slaves on the board connected to these same pins?

Yes. It is possible that a chip may respond to the ICSP signals. You should have pullup resistors on all your CS (or SS if you prefer to call it that) pins on all your slave chips so that while the ATMega328P is in programming mode (and all the GPIO pins are in INPUT mode by default) the CS pins of your slaves aren't floating.

Same for serial programming: how does the chip know that it's supposed to receive a program on the serial interface, when my application uses this port for a different purpose?

Simple: it listens for 2 seconds after power-on or reset for a special sequence of bytes arriving on the serial port. If they arrive within that 2-second window (8 seconds for ATMega32U4 based boards, to allow time for USB enumeration) the bootloader keeps running and looking for programming instructions. If it doesn't see that start sequence within the timeout period it exits the bootloader and runs your sketch.
The STK500 protocol (which the Arduino bootloader is based around) is detailed here. The trigger sequence is the "CMD_SIGN_ON" command detailed on page 4.

You pull the reset pin LOW.

Both the Flash and EEPROM memory arrays can be programmed using the serial SPI bus while RESET is pulled to GND. The serial interface consists of pins SCK, MOSI (input) and MISO (output). After RESET is set low, the Programming Enable instruction needs to be executed first before program/erase operations can be executed.

Most SPI devices have a Chip Select pin. Unless this pin reads LOW, the device isn't listening. That way you can have multiple devices sharing a single SPI bus.
You pull the reset pin LOW for a moment. That makes the chip reset itself. When the chip resets, it first runs the bootloader program. The Arduino bootloader waits 3 seconds for any programming instructions coming in via the serial connection. If these 3 seconds have past, it will exit the bootloader and run your main sketch.

Simply follow the hardware design guide from Atmel AVR042


text:
Integer array definitions: Length
I'm defining a variable as integer and want to use thati
int specLength = 17;
int spec[specLength-1];

When I do this, I get the following error message:
 TestProgram:34: error: array bound is not an integer constant before  ']' token
 int spec[specLength-1];

But when I set it up as
int specLength = 17;
int spec[16];

it all works fine.
What is the magic here?
Many thanks

You have to use a constant expression, one that produce always the same value.
const int specLength = 17;
int spec[specLength - 1];

Otherwise, the compiler won't know how much space reserve for the array.
The problem with C/C++ is that the compiler doesn't really know if a variable is modified somewhere or not (thru pointers, for example). You have to tell the compiler that something is static, not changing.
You can also use a #define for the same porpuse:
#define SPEC_LENGTH 17
int spec[SPEC_LENGTH]

Why both? const int val=17 means that val has a type (int). Using #define is just a text search-and-replace before compiling. So, SPEC_LENGTH has no type at all. If needed, you have to use a cast, like (int) SPEC_LENGTH.


text:
Arduino IDE: Board nodemcuv2 (platform esp8266, package esp8266) is unknown
I am receiving this error after reinstalling Arduino IDE (everything was working fine [same components, same code]):

Board nodemcuv2 (platform esp8266, package esp8266) is unknown

I have recently reinstalled ESP8266 libraries. Could that have caused this issue?

Did some digging and found a solution that worked for me:
Uninstall (by logically deleting folders) and reinstall ESP Libraries.
Open Arduino IDE
Go to FILE -> PREFERENCES

Open the preferences folder (this works in Linux, Mac and Windows)
Go to PACKAGES > ESP8266 > HARDWARE > ESP8266

Delete all libraries (if your issue is similar to mine and to others', you should have 2 or more folders, with different versions).
Restart Arduino IDE.
Go to SKETCH > INCLUDE LIBRARY > MANAGE LIBRARIES
Install ESP8266 Community
Go to TOOLS > BOARD and scroll down to your model of NODEMCU
Run compile and your problem should be fixed.

Good work finding a solution and answering your question! I feel like this wording: "Delete all libraries" could be confusing. I'm not sure if maybe it's common to call folders "libraries" but in the context of Arduino, where the term has a specific meaning which is not applicable here, I think it would be better to instead say "Delete all folders".

FYI,  it should not be necessary to delete both version folders. The problem is caused by the Arduino IDE not completely removing the previous version when you update to a different one so you could just delete the previous version's folder only, which saves you from having to do the reinstall step. However, your instructions work fine to solve the problem.

i tried deleting just the oldest, but it did not work.  You are correct: delete all libraries in the ESP8266 folder.

Many thanks! In fact, my another computer and Mac does not show this and my working PC seems before was installed something causes this error. even though I un-install the arduino the problem still there. without your sharing I cannot fix it. thanks again!


text:
How to use a coma as the decimal separator?
I have a LCD that displays a float variable. I would like to show a comma instead of a dot as the decimal separator.
float temp = 23.50;
lcd.setCursor(0, 0);
lcd.print(temp);

I want to show 23,50 instead of 23.50.
Is there any function to convert or format it?

Print the integer part, print a comma, then print the value minus the integer part.
float value = 123.45;
int ival = (int)value;
int frac = (value - ival) * 100;

Serial.print(ival);
Serial.print(",");
if (frac < 10) Serial.print("0");
Serial.println(frac);

Due to some performance reasons %f is not included in the Arduino's implementation of sprintf(). A better option would be to use dtostrf() - you convert the floating point value to a C-style string, Method signature looks like:
char *dtostrf(double val, signed char width, unsigned char prec, char *s)

Demo
void setup() {
  Serial.begin(9600);
  while(!Serial);

  float a = 12.34;
  char buffer[] = "9999.99";
  dtostrf(a, 4, 2, buffer);
  char* p  = strchr(buffer, '.');
  *p = ',';
  Serial.println(buffer);
}

void loop() {

}

The strchr search for the first ocurrence of second arg ('.' in this case) and return a pointer to that char. You then use that pointer to sustitute '.' with ','.
You have the final string in buffer, where you can use it for whatever purpose you fancy.
Caution: You have to reserve space (buffer) to hold your biggest value.

May be overkill for this simple usage but, if you are printing numbers
in more than one place in your program, it may be convenient to
implement a filter that replaces all dots with commas.
Here is one such filter:
// Replace dots with commas.
class CommaForDot : public Print
{
public:
    CommaForDot(Print &downstream) : downstream(downstream) {}
    virtual size_t write(uint8_t c) {
        return downstream.write(c=='.' ? ',' : c);  // replacement
    }
private:
    Print &downstream;
};

And here is a simple test:
void setup()
{
    Serial.begin(9600);
    float a = 12.34;

    Serial.print("Through Serial:      ");
    Serial.println(a);

    CommaForDot filteredSerial(Serial);
    Serial.print("Through CommaForDot: ");
    filteredSerial.println(a);
}

void loop(){}

The output is:
Through Serial:      12.34
Through CommaForDot: 12,34


text:
ESP8266 wifi mesh
I'm using 3 ESP8266 for my project: one ESP8266 is a server and controls any action.
I want to be able to connect to any ESP8266 with a phone and if that ESP8266 is a client, send data to the server.
I'm using TCP/IP for communication between ESP8266 models and ESP8266 with PC or phone.
My problem is with the number of clients. In ESP8266, a maximum of 5 TCP clients can connect to a server, and my master ESP8266 is using 2 (to connect to the other ESP8266), so that just leaves 3 clients (PC or phone) that can connect to the master ESP8266.
Is there any way to increase the number of clients?
Is there any alternative for communicating between ESP8266 models other than TCP/IP and leave 5 clients in ESP8266 for the user to connect?

Can I just clarify.  You have set one ESP8266 as an access point and you want to be able to connect more than 5 clients to it?

master model is AP

others AP and station

just buy a cheap router and connect them all to that.

@dandavis. Adding a router doesn't solve the OP problem. There are still 5+ clients trying to connect to his ESP8266 services.

@LookAlterno: connection persistence was unclear, but clearly, that's going to be a HW limit if applicable, i was referring to the AP mode incoming station limit. with polling instead of always-on, a router would lets dozens and dozens of clients talk to one ESP.

Have a look at e.g. this and use your favorite find-o-magic to locate more ESP mesh solutions.

If I understand correctly the problem is only 5 clients can connect to an ESP8266 at any one time.  This is an early morning solution to the problem and to be honest I haven't though it through that much, but hopefully its a starting point.
Rather than having all the clients in the mesh connect to the Master node, run an aggregation service on the slaves.  One slave will accept connections fro up to 4 other slaves and it will forward the data to the master.  If you ever wanted more than 5 slave nodes you would need to have the aggregator nodes feeding into another aggregator (like a tree structure). Its quite easy to do statically, but could be tricky if you wanted to do it dynamically.  These aggregators might need to be dedicated nodes, it depends on traffic.
That leaves the master 4 connections for clients.  If that isn't enough then you could send the data from the master to additional ESP8266s acting as dedicated webservers and get the clients to connect to them instead.  There may even be a method of sharing the load between these two web servers in the same way as proper servers do.

i get that. evry esp8266 get 5 client so i can have 15 client  but problem is connection between esp8266 models if i us tcp for connection, my master esp lost 2 space of clients. master model is in center of my place and most clients using that

I think you got half of it.  The master ONLY talks to one ESP, (lets call it slave1) Slave1 is a sensor and server, it forwards data from Slave2, Slave3, etc to the Master and from the master back to the Slaves.  I'm not sure if Slave2 will be able to be a sensor and a server, so it might need to be an extra ESP.  Another plan might be to look at ESP32, it will cost about 5x what a 8266 costs (~£5), but it might support more clients (Its dual core and has more RAM)

tnx fo your try to help. i'am thinking about beter slution

i read some where that max client can up to 255 i think about this

You don't need a separate socket for every client. A client connects to server, sends data or gets data and disconnects. Then the next client connects. One server and one client at the time plus one client connection to PC.

i need clients be conected

whats the difference? why is the keep alive of the connection important for you?

It depends on the frequency of the packets you are sending, to often and the connection overhead will be ridiculous.   Also there is no guarantee that you won't have a situation where all the available connections are used, because all devices and clients are connected.

i want send online status to clients so i need cients be alive


text:
How to import my own library or file?
I have some small functions to format numbers. I move them to a new file and I would like to import it to my proyect.
I can't find how to do it without defining a class (maybe it's the only way). I would like to import like JavaScript, PHP or other languages: just using the import word or something similar.
I know how to import libraries. Example:
#include <OneWire.h>
#include <DallasTemperature.h>

If the file is in your sketch folder then you use this syntax:
#include "foo.h"

All source files in the root of the sketch folder are shown as tabs in the Arduino IDE. In some cases that might not be desirable. If so, you can put the file in the src subfolder of your sketch folder and use this syntax:
#include "src/foo.h"

Or you might want to group files together into separate subfolders:
#include "src/foo/foo.h"

If you want to access the file from multiple sketches then you should turn it into a library and use this syntax:
#include <foo.h>


text:
How to show the º character in a LCD?
I have a 16x2 LCD and I want to show a temperature: 23 ºC.
But the º symbol is not properly shown. It shows a strange character instead of the º symbol .

It depends on the exact model of LCD you are using. Or more exactly, on the Dot Matrix Liquid Crystal Display Controller/Driver, like the Hitachi HD44780U that is used in many 16x2 LCD.
For that driver, you have a table of native chars, like this:

In this table, the char "°" is at col 0b1101, row 1111, (0xDF, or 223). But, you have to cast that value to a char before displaying, otherwise you will get the value (223), not the symbol.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print("temp 27");
  lcd.print((char) 223);
  lcd.print(" ");
  lcd.print(223);

}

void loop()
{
}

lcd.print() does indeed require a cast, but you could also lcd.write(0xdf). Unlike print(), which is mostly intended to format data as ASCII, the write() method is meant to send raw bytes.

How can I concatenate  lcd.print("temp 27")  and  lcd.print((char) 223)?
Something like this: lcd.print("temp 27" + (char) 223)

@Roby. Don't concatenate, just display/print every thing separately. That's the Arduino way.

You can also just do lcd.print("temp 27" "\xDF" "C") to show temp 27°C in one call.

Some displays have different characters above 128. It is best to make your own character.
That is what @dannyf ment with "CGRAM". The custom characters are stored in CGRAM. There is no need to read the datasheet though, because there is a function in the Arduino LiquidCrystal library for it.
Start here: Arduino CreateChar reference
There are even websites that help you to create the character: omerk and maxpromer.

Easy.

Pull up the datasheet and pick the character that most resembles your symbol. Char 0b11011111 looks the best to me but 0b10110000 isn't bad.
While you are in the datasheet, check out CGRAM.

do lcd.write(223);
Transform the Decimal into an ASCII symbol.

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


text:
Controlling JX PDI-6221MG Digital Servo with Arduino
So, I naively bought two of these JX PDI-6221MG Digital Servos thought they would work with the Arduino straight out of the box, just like MG996R did, for instance, but it seems they won't. However, I hear some clicking noise when the motor is connected, tho.
These are the Servo specs:
Dead band: 2μs
Working frequence: 1520μs / 330hz
Operating Speed (4.8V): 0.18 sec/60°
Operating Speed (6V): 0.16 sec/60°
Stall Torque (4.8V): 17.25 kg/cm
Stall Torque (6V):  20.32 kg/cm
Dimensions: 40.5X20.2X38mm
Weight:  62 g
Connector Wire Length: JR  265 mm
Bearing: 2BB

Manufacturer site: http://www.jx-servo.com/English/Product/49513727.html
Additionally, I used 5V 2.4A power input alongside with the Arduino Nano running just some sample code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Wiring:

I tested the same configuration with MG996R and SG90 and it worked. So what am I missing here?

Care to share your wiring?

Added wiring :)

Maybe these serves move slower and you are changing position too quickly. Try a simple sketch where you give one position and hold it for 1 second.

Thanks, good guess, but it didn't work. I also tried to increase voltage up to 7.4V but no progress. Only a clicky noise is to be heard.

i have same problem. i found that they not work with a tester. But gear is very good, sow i did take them a part and use them as gear motor, sow it is not totaly lost, but as normal servo is NO GO!!!!

can u tell to me about the supply ? im using the battery 6,8 volt with 6800 mAh but still cant use the servo,im stuck right now please help me

Check answer. @tommy

I managed to get servos running with 6 V and 3 A powered from lab power supply (GOPHERT CPS-1620 0-16V 0-20A). I guess, at the end, it was power supply which was crucial.

If you connect the servo to the same power supply as the MPU make sure you have a robust amperage rating.  As soon as the servo tries to move, the current draw shoots way up and your MPU will hit a brownout state and reboot.  At the very least you will need a capacitor at the power input to the MPU.

this servo is an absolute power hog.  You can run it off 5V (6V is not needed!), but it's drawing 0.44 amps of power


text:
Arduino Uno board works but Arduino Uno + Wifi is not being detected
When I connect my Arduino Uno board it instantly detects the board and shows it in the list. But when I connect my new Uno + Wifi board it doesn't detect it. I also manually selected Arduino Uno Wifi but it still gives me the message
Error uploading check if the selected board is currently available
How can I fix this?
This is the one that's not working:
Arduino Uno + Wifi
While this one I got does work:
Arduino Uno

Is it a genuine Uno?

I editted my question to contain the exact products. The power source inputs are different I don't know if that could have anything to do with it?

Install the USB drivers (CH340G). It's a cheap Chinese clone.

and the DIP switch must be in USB to Atmega setup

Possible duplicate of My Arduino isn't showing up on my computer. What should I do?

What you have there is a cheap Chinese clone. It uses the cheap CH340G USB chip instead of the proper ATMega16u2 that real Arduinos use.
Install the right drivers and it should start working.

Thank you this worked! But I just wonder, what's the downside of using the cheap clone?

They have a habit of dying easily. But they're cheap to replace.


text:
Calculate power requirement for 64 rgb led ws2812b
Hi I am working on small project where I want to run ws2812b module via arduino and lipo battery. I have purchased 64 matrix for ws2812b from eBay and lipo battery with 500mah.
I am looking to find how I can calculate how much power does WS2818b modules will consume with all leds running and will the battery be sufficient for it. How long will it run on full charge.
Can someone please point me to the formula how I can calculate this. Also to note I am using a power up converter from 3.7 to 5v.

If all LEDs are in full bright white, assume 60mA per LED. 64 would mean a maximum current draw of 64*60mA=3840mA. This figured would not include the Arduino itself.
a 500mAh battery could power this for no more than 7.8 minutes (500mAh / 3840mA = 0.17 hours = 7.8 minutes)
If your usage will not be full bright 100% if the time, then you could run longer.
You didn’t say how long you want it to run. Perhaps start with the design goal before specifying parts.
For a more realistic view of how much power it takes, measure with a multimeter the current draw from the battery or bench power supply to your boost converter while displaying a typical usage on the matrix. Use calculations as above to determine the expected battery lifespan.

The battery capacity is rated at 3.7V and the LEDs run at 5V. You need to account for that. And can that small battery even provide almost 4A?

@gre_gor my arduino has 5v output pin can i not use it? As per answer above i need 3840mA to run all LED. Is it not possible will the battery get damaged?

Whether your battery will run it for any amount of time depends on the battery specifications. What is the "discharge rate" of the battery? This is usually measured in "C", or multiples of the battery capacity. If you have a 5C discharge rate for a 500mAh battery, then you can discharge at 5*500mA, or 2500mA. What is the rated discharge rating of the battery?


text:
Is it possible to power up Arduino upon serial transmission without dropping data?
I have radiator valves that send readings about the temperature and valve opening to the serial output: Honeywell HR-25.
I want to collect those readings in a central server at home without flashing the thermostat with a custom firmware.
To do that, I need an Arduino (very bare one) that can receive the serial data, but at the same time I need it to be powered down most of the time, or it would kill the battery life of the thermostat.
Can I wake up an Arduino from power down using the serial transmission, and immediately capture the serial data without dropping bits? how?

There is lot of information about low power Arduinos/ATMega. Not good?

@LookAlterno Wake up from power-down upon incoming serial? I couldn't find it.

Is the thermostat a smart one that requires a C wire? If so you can put a buck converter on it to power the arduino.

The power is provided by the thermostat, there is a connector with Vcc. Still, I want the batteries to last at least a full season. If I keep the atmega in standby, they probably won't.

I can also power down the device and wake it up (LowPower library) some seconds before the expected serial transmission...

@LookAlterno maybe this actually https://arduino.stackexchange.com/questions/13167/put-atmega328-in-very-deep-sleep-and-listen-to-serial?rq=1

OT question: where do you find how to "hack" the thermostat? I mean, I couldn't find any reference to a serial port for the HR-25

How do you power up a device before it sends the information? Does it have a clairvoyance mode?

The linked (duplicate) page now has example code that preserves the first byte received from serial in the second example in my answer.

I never tried myself, but afaik you can define an wake up/interrupt from USART, meaning that if you get a receiving byte from the UART it will wakeup the Arduino.
When powered up, you can read continuous data. I would be surprised if they make an UART wakeup signal, that bytes would be lost (i.e. that powering up would take too long to miss more UART bytes).

You can build a circuit using an electronic counter to only start supplying power to the Arduino at a specific given time. You would still need to supply the circuit, but the energy need would be a small fraction of the Arduino's needs.
On the other hand, can't you use a second power source for the Arduino? Li-On batteries are pretty cheap nowadays, and you can always savage something from an old Notebook's battery.

I don't think you can use the HW peripheral, so IF you really want/need low power you will need something like this (note: this may be very overkill for your application)

simulate this circuit – Schematic created using CircuitLab
NOTE: I assume the "serial" is a UART and its "rest" voltage is 5V. Choose the PMOS so that it is turned on with about 3V.
In this case, the circuit is normally powered off. When you receive a START, the PMOS start conducting. The ATMEL will then pull pin B to 5V, thus keeping the circuit powered. Through pin A it can sense the UART.
In this case, since you miss the start bit, you won't be able to use the usual libraries or peripherals. You can, however, implement a software uart which can avoid the first start bit to overcome this.
Just a suggestion, however. If your thermostat is transmitting frequently, maybe a more useful approach can be to listen to one transmission, then put everything to sleep for let's say 30 seconds, then turn on again and wait for the first serial transmission. This can be simpler than this solution and more effective

This is completely unworkable - in actually cutting power to the chip, you'll suffer startup delays even worse than in allowing the chip to wake itself.  You really don't need to cut power to modern MCUs, they're designed to handle that task with their internal circuitry.

@ChrisStratton I have to thank you, because I never looked into the deep hole of startup times; I always thought that the clock was stable pretty soon. In any case, Table 13-12 on the datasheet states that the internal clock can be used in a stable way, when the BOD is enabled, after 20 clock cycles. I assume that the power rises in negligible time. With a 9600bps serial (I usually find this in this kind of applications) you will have to wait 156us before sampling the first byte from the falling edge (which is 1248 clock cycles). I think you have enough time to set up everything...

Of course you will have to be quick at initialising, but I think it is doable. I'll try to implement it sooner or later... In any case the power consumption in power down is in the range of 1uA, but I think that it'll stop the clock, so you will need exactly the same time to wake as a power on (excluding the VCC rise time). The only power-save mode with the oscillator enabled is the standby one, but it consumes slightly less than 1mA (and this is too much, since a rough estimation is that the thermostat absorbs around 0.2mA)


text:
Arduino Nano PWM frequency
I have been using an Arduino Nano as a PWM controller for a heating element.
I am currently using D11 (PB3). I could change, although all pins with PWM are in use, and would require a re-write and re-wire.
This produces a signal at ~490Hz, but I would like to experiment with changing this to a higher frequesncy.
I gather it its possible to vary the frequency by changing the divisor from its default 64.
My sketch uses Uses http://playground.arduino.cc/Main/SevenSegmentLibrary and calls millis() - will changing PWM divisor impact on either of these?
NOTE I have read the setPwmFrequency documentation, but find "disrupts the normal operation" unhelpful - I am actually after some explanation of the concrete effect. I only use millis() to time button presses and flash a LED, which are not time critical
Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).
I have been doing some further study. http://playground.arduino.cc/Code/PwmFrequency states  Changes on pins 3, 5, 6, or 11 may cause the delay() and millis() functions to stop working.
Other references say 3, 11 use timer 2 so do they affect millis() or not as other references claim timer 0 is used for these.

Yes and no, depending on the pwm timer used.

I don't know where this misinformation comes from... The answer is
no, reconfiguring Timer 2 does not affect millis() nor
delay(), as these functions rely on Timer 0. AFAIK the Arduino core
does not use Timer 2 at all. But note that there may be other Arduino or
third party libraries that rely on Timer 2.
You can see here the source code of millis() and
delay().
As MITU RAJ correctly states in his answer, the PWM signal on pin 11 is
provided by Timer 2, so you can use it safely.

The misinformation comes from http://playground.arduino.cc/Code/PwmFrequency. I am sure I am not the only user to have been mislead. The other answer just published an extract of the misleading documentation.

Depends on the pin you used for the PWM.
Arduino UNO/NANO specs say following things:
The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
The base frequency for pins 5 and 6 is 62500 Hz.
The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64, 256, and 1024.
The divisors available on pins 3 and 11 are: 1, 8, 32, 64, 128, 256, and 1024.
PWM frequencies are tied together in pairs of pins. If one in a pair is changed, the other is also changed to match.
Pins 5 and 6 are paired on timer0.
Pins 9 and 10 are paired on timer1.
Pins 3 and 11 are paired on timer2.
Changes on pins 3, 5, 6, or 11 may cause the delay() and millis() functions to stop working. Other timing-related  functions may also be affected.

Since you're quoting the specs, it'd be nice if you could also link to them? :)

The answer is NO, changing the PWM divider for pin 11 will not affect the millis() function (nor delay()).

Explanation:
The Arduino Nano uses the ATMega328P microcontroller, same as the Arduino Uno.
Pin 11's PWM is controlled by Timer 2. By default Timer 2 has a prescale of 64 (ie. setting 0x04). The base frequency of Timer 2 is 31372.55 Hz resulting in a frequency of 490.2 Hz (ie. 31372.55/64 = 490.1961).
On the other hand, the microsecond timer (ie. the one used by millis(), micros(), delay() and delayMicroseconds()) is controlled by Timer 0.
So changing the prescaler of Timer 2 will not affect millis().
It will however change the PWM frequency of Pin 3.

Back to your question:
The possible PWM frequencies of Pin 11 (higher than 488 Hz) are:
31373 / 32 = 980.4 Hz
31373 / 8 = 3921.6 Hz
31373 / 1 = 31373 Hz
Where 32, 8, 1 are the prescaler.
Which equates to a setting value of 0x03, 0x02 & 0x01 respectively.
Add this line of code:
TCCR2B = TCCR2B & 0b11111000 | setting;

Where setting is the value of the setting for the respective prescaler.
============================================
|| Frequency [Hz] || Prescaler || Setting ||
============================================
|| 31373.55       || 1         || 0x01    ||
|| 3921.57        || 8         || 0x02    ||
|| 980.39         || 32        || 0x03    ||
|| 490.20         || 64        || 0x04    ||
|| 245.10         || 128       || 0x05    ||
|| 122.55         || 256       || 0x06    ||
|| 30.64          || 1024      || 0x07    ||
============================================

Source code: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring.c

Just nitpicking but... the timer's base frequency in phase correct PWM mode is F_CPU/510 (31373 Hz), not F_CPU/512: it takes 255 cycles to count from 0 to 255, then 255 more to count back to zero. C.f. the equation for the PWM frequency in the datasheet.

@EdgarBonet Good point. The answer has now been corrected.

would it be possible to go down a step slower to 15.31 HZ?


text:
Control LED strip IR controller using Arduino
Is it possible to connect to one of these, which run at 12v, to turn on the LED strip using an ESP8266? It doesn't matter if it's an Arduino or the ESP8266, both should be able to do this. The board looks like this:

If I turn on the red light, the "R" gets grounded and therefore applies 12v to the rail. The 3 MOSFETs on the images are P0903BDG MOSFETs.
Alternatively, I can hook into the IR signal and send an IR signal using the ESP8266. That would be really awesome, as it only requires 3 wires. How that is possible, I'm not really sure, though.
I know it's possible to make your own circuit using IRLZ44N MOSFETs, which I have done before, but I want to try and use as much as this circuit as possible.

What is the board you are using? Does it have a name?

@LookAlterno It does not, unfortunately. It's just a simple controller like these: https://www.google.com/search?q=led+strip+ir+controller&tbm=isch

So you are recycling a consumer product?

@LookAlterno Yeah exactly.

Desolder the IC's connected to the MOSFETs and use your Arduino to drive the MOSFETs.

I tried to do the same thing with hooking up an IR LED on a Raspberry Pi. It worked pretty well but the chip on the receiver board could not take fast switchings. I ended up doing my own PCB.
The problem is that the chips run at voltages higher than you would use for an ESP8266 or Arduino. This will be the biggest problem if you want to supply it with the voltages available on the board.
I think the safest thing is to see if you can simulate IR input by just connecting to GND and OUT of the IR receiver port.

Yeah I did the IR LED as well on my ESP8266 actually. I could both send NEC commands and receive them, meaning I could send commands to the LED strip or another ESP8266 using IR. Worked fine but I don't like IR. What do you think it would take to send the IR commands without the actual IR? Is it just a matter of taking some "IR receiver" code, then plugging that into the IR OUT "connector"? That might work.


text:
Arduino I2C communication between Raspberry Pi
We are trying to communicate between multiple Arduino's (in this example a Nano and a Mega) and a Raspberry Pi. The Raspberry pi is defined as master and the Arduino's as slaves. We would also like that the Arduino's communicate between each other. For example: The Mega requests from the Nano and at some point, the Mega also communicates with the Raspberry Pi. This is the short version of what we are trying to accomplish but we ran into the problem that the i2c communication failed between the Nano and Mega upon the request from the Mega to the Nano.
Schematic

Problem
After some time the i2c bus 'fills' up...

Code Nano
(We send RFID data to Mega)
#include <Wire.h>
#include "SPI.h"
#include "MFRC522.h"

#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8

MFRC522 rfid(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();

  Wire.begin(0x10);
  Wire.onRequest(requestEvent);
}

void loop() {

}

void requestEvent() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
    return;

  // Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  // Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
      piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
      piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  String strID = "";
  for (byte i = 0; i < 4; i++) {
    strID +=
      (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
      String(rfid.uid.uidByte[i], HEX) +
      (i != 3 ? ":" : "");
  }
  strID.toUpperCase();
  //81:AE:1D:29
  Serial.print("Send to Mega: ");
  Serial.println(strID);

  Wire.write(strID.c_str());

  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();

}

Code Mega
#include <Wire.h>

void setup() {
  Wire.begin(0x03);
}

void loop() {
  Wire.requestFrom(0x10, 11);

  String uid = "";

  while (Wire.available()) {
    char c = "";
    c = Wire.read();
    uid += c;
  }

  if (uid.indexOf(':') != 2) {
    return;
  }

  Serial.println(uid);

  delay(500);
}

We would like to communicate between the Arduino's when the Raspberry Pi is master.

Pi and arduino works at different logic level bro. Also you gotta check if pull up resistors are needed in the SDA lines of I2C bus. I suspect internal pullups of UNO are not really useful. Use 2.2K pull-ups externally.

Thanks! Also you gotta check if pull up resistors are needed in the SDA lines of I2C bus. I placed a 1K pull-up between 3.3V and SDA and also a 1K pull-up between 3.3V and SCL according to this article: link. Thank you! Is the 1K between 3.3V and SCL needed?

Yea i guess you need pull up in both SDA and SCL.

Its a long time since I used a Pi, but isn't it a 3.3V device and the Arduinos are 5V devices.  I think you may need a logic level shifter (not a resistor divider).  I suspect that's the reason I2Cdetect isn't working and without that working, nothing else will.

Thanks for the suggestion, we'll try and let you know if it worked!

The level shifter isn't necessary I think (I'm not quite sure). But just to be sure; we'll put them in, thanks!


text:
I am having trouble with fading two LEDs
I've tried to modify this code, but everything I tried failed.
Where am I going wrong?
All I want to happen is when button 1 is pressed LED 1 fades up and when released LED 1 fades out and the same again with the 2nd LED when button 2 is pressed LED 2 fades up and when released it fades out.
int ledpin = 10;
int ledpin1 = 11;
int pushbutton = 7;
int pushbutton1 = 8;

int brightness = 255;
int fadeamount = 1;

void setup ()
{
  pinMode (ledpin,OUTPUT);
  pinMode (ledpin1,OUTPUT);
  pinMode (pushbutton,INPUT);
  pinMode (pushbutton1,INPUT);
}

void loop ()
{
  if( digitalRead(pushbutton)==HIGH && brightness<255 )
  {
    // fade in ledpin
    brightness = brightness + fadeamount;// increase brightness
    if( brightness>255 ) // check that brightness doesn't exceed 255
      brightness = 255;
    analogWrite(ledpin,brightness);
    delay (1);
  }
  else if( digitalRead(pushbutton)==LOW && brightness>0 )
  {
    // fade out ledpin
    brightness = brightness - fadeamount;// decrease brightness
    if( brightness<0 ) // check that brightness doesn't go below 0
      brightness = 0;
    analogWrite(ledpin,brightness);
    delay (1);

  }

  if( digitalRead(pushbutton1)==HIGH && brightness<255 )
  {
    // fade in ledpin1
    brightness = brightness + fadeamount;// increase brightness
    if( brightness>255 ) // check that brightness doesn't exceed 255
      brightness = 255;
    analogWrite(ledpin1,brightness);
    delay (1);
  }
  else if( digitalRead(pushbutton1)==LOW && brightness>0 )
  {
    // fade out ledpin1
    brightness = brightness - fadeamount;// decrease brightness
    if( brightness<0 ) // check that brightness doesn't go below 0
      brightness = 0;
    analogWrite(ledpin1,brightness);
    delay (1);

  }

}

Create 2 sets of brightness, fadeamount variables, one for each button/LED. You can put them in a struct to make things simpler:
struct brightness {
    int brightness;
    int fadeamount;
    int ledpin;
    int pushbutton;
};

struct brightness set1;
struct brightness set2;

Then initialize them in init.
The LED goes from full brightness to off in 0.255 seconds. That is pretty quick.
You can add a millis() based timer on the brightness change:
struct brightness {
    int brightness;
    int fadeamount;
    int ledpin;
    int pushbutton;

    //add the following
    unsigned long timeChanged;
    unsigned long timeBetweenChanges;
};

void handleButtonFade(struct brightness* set){
      if( millis() - set.timeChanged < set.timeBetweenChanges)
          return;

      if(digitalRead(set1.pushbutton)==HIGH && set.brightness<255 )
      {
        // fade in ledpin
        set.brightness = set.brightness + set.fadeamount;// increase brightness
        if( set.brightness>255 ) // check that brightness doesn't exceed 255
          set.brightness = 255;
        analogWrite(set.ledpin,set.brightness);
      }
      else if( digitalRead(set.pushbutton)==LOW && set.brightness>0 )
      {
        // fade out ledpin
        set.brightness = set.brightness - set.fadeamount;// decrease brightness
        if( set.brightness<0 ) // check that brightness doesn't go below 0
          set.brightness = 0;
        analogWrite(set.ledpin,set.brightness);

      }

}

void loop(){
    handleButtonFade(&set1);
    handleButtonFade(&set2);
}

Small Change in these lines :

if( digitalRead(pushbutton)==HIGH && brightness<=255 )
else if( digitalRead(pushbutton)==LOW && brightness>=0 )

Also change delay to delay(10) something, instead of delay(1). Only then, human eye can observe that change in brightness from 0-255 :-)
Also adding a capacitor to reduce the debounce in your pushbutton would be a thing to do.


text:
4-20 milliamp to volts to pressure maths
I am measuring pressure in bars from OsiSense™ XMLP pressure sensor (4-20 milliamp) on a arduino uno.  When the machine im measuring pressure on is switched off the reading is 0v as expected.
When the machine is on (the sensor is on) the reading is 1v as expected.
When the machine is on and hydraulics are active the voltage fluctuates as expected.
The problem I have is I don't know the maths to translate the voltage to bar pressure.
From a previus question i've asked from 0v to 5v the equation is:
// This is for the DEFAULT 5V analog reference.
// v = the voltage at the pin in Volts.
// p = the pressure in bar.
// Note that 'bar' is the pressure relative to the
// normal atmospheric pressure at that moment.
float v = (float) analogRead(A0) / 1024.0 * 5.0;
float p = v / 5.0 * 100.0;"

Is it just a matter of changing the equation to take into account the 1v when the sensor has power and then dividing by 4 instead of 5:
RawVoltage = (float) analogRead(A0) - 1;
float v = RawVoltage / 1024.0 * 4.0;
float p = v / 5.0 * 100.0;"

Please add a circuit (wiring). Also explain how you reasoned about dividing by 4 instead of 5. Last which model are you using; the pressure range depends on the device. 4-20 mA is 0-100% of the pressure range.

Thanks for getting back to me so quick.  I'm sure its something easy im missing.  The pressure sensor reads 0 to 100 bar.  0mA to 20mA is 0v to 5v range which is %100 of the possible readings.  4-20 mA is 1v to 5v and is 80% or 4v.  The first 1v (0 to 4mA) just tells us the sensors has power and is working ok and reading 0 bar.  What I need to workout is 0 bar to 100 bar (4 mA to 20 mA).
you can see a diagram on the post here: https://arduino.stackexchange.com/questions/45485/reading-4-to-20ma-pressure-sensor-using-uno

If I understand your question correctly, when the sensor is on,

4 mA (1 V on the Arduino) means 0 bar
20 mA (5 V on the Arduino) means 100 bar.

Is that correct?
If that is correct, there you just have to linearly map the voltage
range to the pressure range. The equation is
P = (v − 1 V) / 4 V × 100 bar
or, in C++:
float v = analogRead(A0) / 1024.0 * 5.0;  // in volts
float p = (v - 1) / 4 * 100;              // in bars


text:
Arduino GSM shield code help
Trying to run the below code but getting the following error message:

exit status 1 expected unqualified-id before '==' token

Code is as follows:
==============================================
= Arduino SOS CAll sim900 shield Project =
= with Arduino =
= http://www.mrmodder.com =
==============================================
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "call.h"

#define ACTIVE LOW

const int ledPin = 13; // the number of the LED pin

CallGSM call;
boolean started=false;
int buttonState = 1;
const int buttonPin = 7; // the number of the pushbutton pin
boolean calling = false;

void setup()
{

Serial.begin(9600);
pinMode(ledPin,OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin,HIGH);
if (gsm.begin(9600))
{
Serial.println("\nstatus=READY");
started=true;
}
else
Serial.println("\nstatus=IDLE");
}

void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == ACTIVE) {
if(calling)
{
digitalWrite(ledPin,LOW);
calling = false;
call.HangUp();
delay(1000);
}else
{
calling = true;
digitalWrite(ledPin, HIGH);
delay(1000);
call.Call("+300000000000"); //Insert telephone number here

}

https://mrmodder.com/make-a-call-using-gsm-module-arduino/
Have a GSM shield and trying to call a preset number using the above tutorial but I cant get the code to run properly

How could you not know how to write a comment in C?

This is where the problem is:
==============================================
= Arduino SOS CAll sim900 shield Project =
= with Arduino =
= http://www.mrmodder.com =
==============================================
COMMENT THIS WHOLE TEXT using
/* [insert text here] */


text:
Why using static volatile for variables and only static for arrays in TWI library?
I want to know why to declare variables with static volatile and arrays with only static in TWI library?
For example;
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
static volatile uint8_t twi_masterBufferIndex;
static volatile uint8_t twi_masterBufferLength;

Do you understand what volatile does?

I learned something like that this variable maybe changed but in which aspect I don't know.

You use volatile to mark variables that are going to change at any moment, in a ISR (Interrupt Service Routine) typically.
You mark them so the compiler doesn't optimize, and always go and pick its actual value. If you don't do that, then an ISR may update a variable while the main "thread" of execution keeps using its old value. That's is a nasty situation to debug.
You only need to mark variables that are used both in the ISR and in the main sketch. If twi_masterBuffer is not changed inside an ISR, you don't need to mark it with volatile.
static is used inside function to mark variables that need to be permanent, i.e., keep their values between calls, but not accessible from outside that function.

I'm studying the TWI library, to modify my I2C library because my I2C library has a bug, and I went back to learn about the well professional written code for I2C which is the TWI library. So, I'm going through C techniques of coding. And, now this is going more difficult to me because after looking over and over to the code, I looked to the ISR and it has operations of sending and receiving data, then what's going in the actual functions?

static and volatile are a different aspect of programming, but very related, because I have to understand what actual functions do and what ISR also do, and then I may understand what static and volatile means.

static variables are declared at the head of the .c code, they are not inside any function, so how would they be used from outside the function? Or, the static goal here is to keep the values of these variables constant between all the functions inside TWI library, right?

@PerchEagle. You are confusing static with const.

No, I know const means this value is not changed what so ever, but static means the value must not changed to reset between the calls of single or different functions.

@PerchEagle. Static global variable means: this variable is global to all functions in the file (.ino) where it's declared, and unknow in other files (.ino). Non-static global variable means they are global across all files in the project. It's not about the value; it's about where it's allocated (heap vs stack), what is his lifetime (forever vs only during that particular function call), and where it's know (one file vs all files in the project). C/C++ is tricky, tricky.

Yes, so there are advs and disadvs between static and non-static. Static holds its value between all functions but the non-static is not. Non-static is recognized in all files where static is not. But there's no type of variables which can hold the value and also be recognized within other files? Is that possible?

OK, so twi_masterBuffer is not modified, its role is like a storage for TX or RX operations, and copied from the user data in or out.


text:
How can I change the speed of a stepper motor?
I am new to Arduino and am trying to implement different rotational speeds for a stepper motor. I am using a Nema 17 bipolar stepper motor, a Big Easy Driver, and an Arduino Uno.
I've been trying to modify a tutorial code such that it will run at variable speeds:
//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define MS3 6
#define EN  7

//Declare variables for functions
char user_input;
int x;
int y;
int state;

#include <AccelStepper.h>
#include <Stepper.h>
Stepper stepper1 (1,stp,dir);

void setup() {

  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS3, OUTPUT);
  pinMode(EN, OUTPUT);
  resetBEDPins(); //Set step, direction, microstep and enable pins to default states
  Serial.begin(9600); //Open Serial connection for debugging
  stepper1.setSpeed(1);
  Serial.println("Begin motor control");
  Serial.println();
  //Print function list for user selection
  Serial.println("Enter number for control option:");
  Serial.println("1. Turn at default microstep mode.");
  Serial.println("2. Reverse direction at default microstep mode.");
  Serial.println();

}

//Main loop
void loop() {
  while(Serial.available()){
      user_input = Serial.read(); //Read user input and trigger appropriate
function
      digitalWrite(EN, LOW); //Pull enable pin low to set FETs active and allow motor control
      if (user_input =='1')
      {
         StepForwardDefault();
      }
      else if(user_input =='2')
      {
        ReverseStepDefault();
      }
      else
      {
        Serial.println("Invalid option entered.");
      }
      resetBEDPins();
  }
}

//Reset Big Easy Driver pins to default states
void resetBEDPins()
{
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(MS3, LOW);
  digitalWrite(EN, HIGH);
}

//Default microstep mode function
void StepForwardDefault()
{
  Serial.println("Moving forward at default step mode.");
  digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
  for(x= 1; x<4000; x++)  //Loop the forward stepping enough times for motion to be visible
  {
    digitalWrite(stp,HIGH); //Trigger one step forward
    delay(1);
    digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
  Serial.println("Enter new option");
  Serial.println();
}

//Reverse default microstep mode function
void ReverseStepDefault()
{
  Serial.println("Moving in reverse at default step mode.");
  digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
  for(x= 1; x<4000; x++)  //Loop the stepping enough times for motion to be visible
  {
    digitalWrite(stp,HIGH); //Trigger one step
    delay(1);
    digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
  Serial.println("Enter new option");
  Serial.println();
}

Adjusting the potentiometer on the Big Easy Driver will either stop the motor turning completely or run at a constant speed regardless of further adjustment.
Do you have any suggestions for debugging my code, or are there other functions I should be using in addition to/instead of those I have?

Do you understand how a stepper works?  You send it pulses and it steps for each pulse you send it.  The faster you send the pulses the faster it will run.  The slower you send the pulses the slower it will run.  You have lines in your code like this:
void StepForwardDefault()
{
  Serial.println("Moving forward at default step mode.");
  digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
  for(x= 1; x<4000; x++)  //Loop the forward stepping enough times for motion to be visible
  {
    digitalWrite(stp,HIGH); //Trigger one step forward
    delay(1);
    digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
  Serial.println("Enter new option");
  Serial.println();
}

Can you see the part that is sending HIGH and LOW to your step pin?  See the delay that is determining how fast those pulses go out?  Can you imagine how you might change that delay so that the pulses go out faster or slower?  Here's a hint, delay works in milliseconds, but if you want to go faster than this then you'll need to work in microseconds.  Can you find the version of delay that works in microseconds in the Arduino reference?

Thank you, I found the delayMicroseconds() command which helped significantly!


text:
How do I create global pointers with low memory overhead?
Problem
I have a few large objects in my code that need global visibility, but creating them seems to eat up memory significantly faster than simple math would dictate, and furthermore, using them differently in code seems to change how much RAM the compiler tells me I have left for local variables.
How do I optimize the use of my RAM while still being able to use an object in both my setup() and my loop() functions?
Note: Skim the Pseudo-Code section and then skip to the Discussion section if want to get to the meat of my question (I just wanted to provide plenty of background, if needed).
Test Code
To demonstrate that reported global RAM usage changes due not only the number of variables I define globally, but how those variables are used in code, I created a test program.  I compiled this code on the Arduino IDE on a Mac, and ran this on multiple Elegoo Nanos.
Please forgive the particularly long "minimal" complete verifiable code, but it has a bit going on.  Here's the pseudo-code for it.
Pseudo-Code
"T" (for tested) before a line means that I commented out a line or left it be depending on what test I was running:
Define a function to check how much memory remains, freeRam() (taken from https://playground.arduino.cc/Code/AvailableMemory, bottom of page)
Create a class that uses a ton of RAM, about 1024B (GinormousClass)
Create a wrapper for GinormousClass (GinormousClassWrapper) that may or may not construct the GinormousClass, based on how it is implemented

Global scope:
Run freeRAM() to see how much initial RAM is available before setup() is run.
T Create a global pointer to the wrapper class

Local scope (setup):
Report initial RAM
T Create a local pointer to the wrapper class
T Assign a newly constructed wrapper object to the pointer, which does NOT instantiate the huge class
    Report how much RAM remains after creation and after deletion
T Assign a newly constructed wrapper object to the pointer, which DOES instantiate the huge class
    Report how much RAM remains after creation and after deletion

Note: Remaining RAM before creation and after deletion were always the same.
Actual Code
#define ARRAY_SIZE 256

// Method to determine how much memory is left (https://playground.arduino.cc/Code/AvailableMemory, bottom of page)
int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
};

// Define a class that uses a ton of RAM
class GinormousClass {
  public:
    GinormousClass() { // Fill array with (kind of) random numbers
      for (int i = 0; i < ARRAY_SIZE; i++) {
        BIG[i] = random(1000);
      }
    };

    long BIG[ARRAY_SIZE]; // Have this class control a huge array
};

// Define a class that has the potential to use a ton of RAM
class GinormousClassWrapper {
  public:
    GinormousClassWrapper(bool createGiantObject) { // Constructor
      if (createGiantObject) {
        // Create a GinormousClass object (use a ton of RAM)
        gPointer = new GinormousClass();
      } else {
        // Do not create GinormousClass object
      }
    };

    ~GinormousClassWrapper() { // Delete the Ginormous Class, if it exists
      if (gPointer) {
        delete gPointer;
      }
    }

    GinormousClass *gPointer = NULL; // Initialize pointer to NULL
};

int initialFreeRam = freeRam(); // Figure out how much RAM is available before the fun begins
GinormousClassWrapper *ginny; // Declare a GLOBAL pointer to the potentially enormous class

void setup() {
  // Start the Serial connection, and get the initial amount of RAM
  Serial.begin(9600);
  Serial.print("Initial free RAM: ");
  Serial.println(initialFreeRam);

  //  GinormousClassWrapper *ginny; // Declare a LOCAL pointer to the potentially enormous class

  // Try making the class, without defining the array
  Serial.println("Making Ginormous Class (do nothing constructor)...");
  ginny = new GinormousClassWrapper(false);

  Serial.print("Current free RAM: ");
  Serial.println(freeRam());
  Serial.println();

  // Delete the class
  Serial.println("Deleting first class...");
  delete ginny;

  Serial.print("Current free RAM: ");
  Serial.println(freeRam());
  Serial.println();

  // Try making the class, WITH defining the array
  Serial.println("Making Ginormous Class (use a ton of RAM constructor)...");
  ginny = new GinormousClassWrapper(true);

  Serial.print("Current free RAM: ");
  Serial.println(freeRam());
  Serial.println();

  //  Delete the class
  Serial.println("Deleting second class...");
  delete ginny;

  Serial.print("Current free RAM: ");
  Serial.println(freeRam());
  Serial.println();
};

void loop() {

};

Results
I've collected the RAM reported at 4 different times:

after compilation
before setup()
after the "do-nothing" construction of the wrapper
after the "use-space" construction of the wrapper

5 different scenarios were tested:

no pointer declared/wrapper not constructed
pointer declared but object not constructed
do-nothing constructor used
use-space constructor used
do-nothing constructor used, object deleted, and use-space constructor used

Raw Results:
Numbers are amount of RAM left, in bytes
0 Not declared:
    Compiler: 1836
    Initial: 1830
    Do-nothing constructed: N/A
    Use-space constructed: N/A

1 Declared global, not constructed:
    Compiler: 1836
    Initial: 1830
    Do-nothing constructed: N/A
    Use-space constructed: N/A

2 Declared global, constructed do-nothing:
    Compiler: 1734
    Initial: 1728
    Do-nothing constructed: 1724
    Use-space constructed: N/A

3 Declared global, constructed use-space:
    Compiler: 1722
    Initial: 1716
    Do-nothing constructed: N/A
    Use-space constructed: 686

4 Declared global, constructed both:
    Compiler: 1648
    Initial: 1640
    Do-nothing constructed: 1636
    Use-space constructed: 610

Note: Local pointer instead of global pointer had no change except increase all reported RAM by 2 bytes.
Discussion
In the raw results, the GinormousClassWrapper takes 4B when constructed (the pointer it owns) and GinormousClass takes about 1024B when constructed (exactly 1030B, we can assume there's some overhead).  However, one strange thing is of note:
The amount of room stated by the compiler to be available for local variables decreases significantly when the wrapper is constructed  versus when it is not (1836B down to 1640B when both constructors are used).  I would expect that the amount of RAM used by globals would not change, because technically only the pointer is in global scope.  If it is SUPPOSED to change according to how the pointer is used, then why does it change by 196B, of all numbers?
I am assuming that the RAM given by the compiler is fact, and it's one that I would like to know because my project is running on tight memory restrictions.  So where is all of this valuable extra RAM going, and what can I do to reduce it?  Thank you for any help!

I compiled a slightly modified version of your code, with some
#defines for selecting which objects get constructed or not. I also
printed the available RAM right after entering setup(). I found that,
at this point in the program, the version constructing both objects
is using 178 bytes more RAM than the version that constructs none.
I then used the commands avr-nm and avr-objdump to try to figure out
how the program is using its memory. I found the 178 bytes
difference is as follows:
157 bytes of string data
  6 bytes of malloc data
  4 bytes of random state
  2 bytes for the ginny variable
  8 bytes of stack
  1 mystery byte

The difference in string data comes from messages such as “Making
Ginormous Class (do nothing constructor)...”, “Deleting first class...”,
etc., which are #defined out in the version that does not construct
the objects. The malloc() data (heap_start, heap_end and margin) is
not used in the version that does not use new, so it is not included
in that version. For the same reason, the random() state is not
included in the version that does not use random(). The ginny
variable, although declared in both versions, is optimized-out by the
compiler in the version where it remains unused. The 8 bytes of stack
come from the fact that the largest version of setup() needs more
CPU register to do its work, and hence pushes more registers to the
stack. There is only one byte I could not account for, with value 0,
sitting at the end of the .data section, with no associated symbol.
As you can see from this, the difference has little to do with the
objects being constructed. It is mostly due to the string data. A
smaller part of that difference comes from variables (ginny, random
state and malloc() data) which are used only in one version of the
program. The practical conclusions are:

If you want to save RAM, use the F() macro when printing constant
strings.
If you want to benchmark memory consumption, beware your benchmarking
method can have non-obvious effects on the consumption yo are trying
to measure.

I have to confess that I probably doesn't understand what you are asking ...
Regarding the stats giving by the compiler (tested on a Uno, Arduino IDE 1.8.5), I got
Global Wrapper (using global ginny)
Sketch uses 3304 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 400 bytes (19%) of dynamic memory, leaving 1648 bytes for local variables. Maximum is 2048 bytes.

Execution
Initial free RAM: 1640
Making Ginormous Class (do nothing constructor)...
Current free RAM: 1636

Deleting first class...
Current free RAM: 1640

Making Ginormous Class (use a ton of RAM constructor)...
Current free RAM: 610

Deleting second class...
Current free RAM: 1640

Local Wrapper (using local ginny)
Sketch uses 3258 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 398 bytes (19%) of dynamic memory, leaving 1650 bytes for local variables. Maximum is 2048 bytes.

Execution
Initial free RAM: 1642
Making Ginormous Class (do nothing constructor)...
Current free RAM: 1638

Deleting first class...
Current free RAM: 1642

Making Ginormous Class (use a ton of RAM constructor)...
Current free RAM: 612

Deleting second class...
Current free RAM: 1642

Local Wrapper version uses 2 bytes less of global space. That's correct, because space for local ginny comes from the stack, while global ginny comes from the heap.
No compiler will give you how much space (for variables) will you need during execution. That's unsolvable (from Turing's Halting Problem). Just think what happens when you enter in a non-ending recursion.
Comparing execution results, both version show the same amount of memory, off by two bytes, which is due to ginny declared global vs local.
There is a differente between free space declared by compiler (1648, 1650 bytes) and space reported during program execution (1640, 1642 bytes). The difference is 8 bytes in both cases.
Now, setup is a function called from main. That's means you need stack space for the call (return address and whatever), which explains the 8 bytes difference.

I am assuming that the RAM given by the compiler is fact ...

That's about the only thing I wouldn't assume. The compiler can't really know how much RAM your code is going to use, especially when you do dynamic memory allocation. The compiler can certainly report on how much space your global variables take, as it has to allocate addresses for them.
I have a post about what RAM is used in a minimal sketch, for example this:
#include "memdebug.h"

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.print (F("Free memory = "));
  Serial.println (getFreeMemory ());
  }  // end of setup

void loop () { }

Perhaps surprisingly, that sketch already uses 346 bytes of RAM, and it hasn't got any (visible) variables in it.

what can I do to reduce it?

  Serial.println("Making Ginormous Class (do nothing constructor)...");

You could start by using the F macro:
  Serial.println(F("Making Ginormous Class (do nothing constructor)..."));