Recent Posts

Pages: [1] 2 3 ... 10
Can you explain in toddler language the micros approach? If you have time (and knowledge) to spare with this humble servant?

Start with a round clock with only the hour hand and the number 12 replaced by 0.
The clock goes 0 to 11 and back to zero, I can time up to 11 hours long with this clock.

The neat part is that the start hour can be any hour. if the start hour is 10 and now it is 7, the hand moved 9 hours you can see. If the hand starts at 7 and moves backwards 10 (the start time) then it will end up at 9, the number of hours between 7 and 10.

The numbers on the clock are all positive and roll-over at the top. The unsigned variables we use to time on Arduino are the same only bigger.

Arduino millis() and micros() functions return either of 2 clock values that start and run when the board does, they go around like hours and seconds but much smaller. Millis() is okay but not for short times when it matters.
Using millis() or micros(), always use unsigned integers and the same formula works up to how big the variable can count.

The way to do it is to get a start time (perhaps when a pin changed)

unsigned long startMicros, elapsedMicros = 1000;

....... down in the running the next line executes;
    startMicros = micros();  // start time is recorded

........ and later on I want to do something IF a millisecond has passed or at least very close to then

  if ( micros() - startMicros >= elapsedMicros )
    // time is up, put the do something code here

If I collect a lot of times quickly in an array I can tell how long between any 2 by subtracting the earlier time from the later time, the round clock nature of unsigned variables makes it work -- it's a great way to compare time-stamped log lines for instance.

Ardiono millis() is good for most things but as it counts, it makes 0 to 249 fit in the low 8 bits of the 32-bit millis() return. The low 8 bits that can hold 256 values has 6 values that it skips. When you do the unsigned subtract with millis, you can be off by 1 so if it's important to right on time, use micros() instead.

Arduino micros() is not perfect but only that the smallest it tells is every 4 microseconds, not 1. I think that is pretty good, it is millisecond/250 with no skipped counts. You just use bigger numbers for the same time, micros() is good for over 71 minute intervals while millis() can count just over 49.7 days.

So you have 2 more tools now?

Deutsch / Re: Höhere Auflösung als 480x3...
Last post by qualidat - Today at 07:34 am
Wenn man sich intensiv damit beschäftigt, bekommt man solche Anzeigen wie in dem Bild oben auch auf dem Nextion hin.

Man kann Göße und Position u. Farbe z.B. von vordefinierten Balken per Kommando ändern. Es gibt auch Befehle zum dynamischen Zeichnen von Linien und Pixeln.

Was wegen der Steuerung über die serielle Schnittstelle nicht geht, ist echtes Live-Video oder das Anzeigen von nicht vorher abgespeicherten Fotos größer als ein Icon in vertretbarer Geschwindigkeit.

Nextion ist extrem cool für Bedienoberflächen und Statusanzeigen. Extrem dynamisches "Gezappel" ist dagegen eher nicht der optimale Einsatzzweck.
Sir I would like to have a couple of boards. Any chance to have one. Please send me a mail if so. Thanks
Project Guidance / Re: Robot to turn left and mov...
Last post by ngjy - Today at 07:26 am
Thankyou everyone for the reply! I only post a snippet of the codes because I was still editing some parts of it as the previous person working on this project added some codes that were not used in the program but I really thanked all of you for willing to guide me with my project. The codes below are the full coding:
Code: [Select]
#include <SoftPWM.h>

const byte numChars = 100;
char receivedChars[numChars]; // an array to store the received data

const int numbers = 5;
int fiveNumber[numbers];

boolean newData = false;
int i=0;

int count;

// save data sent in by bluetooth through android phone
int dataNumber = 0;
char distance[100] = {
char angle[100] = {

// declare variables
double x;
double angleDouble;

double delayTime;

/* declare motor pins
 M1 |in2,3      in4,5| M2
 |                |
 M3 |in9,10   in11,12| M4

// motor 1
int in2 = 2;
int in3 = 3;
// motor 2
int in4 = 4;
int in5 = 5;
// motor 3
int in9 = 9;
int in10 = 10;
// motor 4
int in11 = 11;
int in12 = 12;

void setup() {
  // arduino default serial

  // enable softPWM

  // change bluetooth serial port to 115200

  Serial1.begin(19200); // 19200 for bluetooth RN42
  Serial.println("<Arduino is ready>");

void loop() {

void recvWithEndMarker() {
  static byte ndx = 0;   //local variable to store up to 8 digit numbers from 0 to 255
  char endMarker = '\n'; //break line
  char rc;

  if (Serial1.available() > 0) {  //Bluetooth connected
    rc =;  //storing value for connected Bluetooth

    if (rc != endMarker) {    //connected Bluetooth is not equals to break line
      receivedChars[ndx] = rc; //store the connected Bluetoothh values into an array
      ndx++; //increment

      if (ndx >= numChars) {   //if index is more than or equals to 100
        ndx = numChars - 1; //index = 100 - 1
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0; //index = 0
      newData = true;  //there is newData

void showData() {
  if (newData == true) { //if there is newData
    dataNumber = 0; // new for this version
    dataNumber = atoi(receivedChars); // new for this version , atoi(receivedchars) is to convert all Bluetooth data stored in array to int

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(receivedChars,","); // get the first part - the string , split the values by ,
    strcpy(distance, strtokIndx); // copy value to distance
    Serial.print("Distance: ");

    x = atof(distance); // in meters , convert string to float/double

    // get angle
    strtokIndx = strtok(NULL,","); // continue  , split the values by ,
    strcpy(angle, strtokIndx); // copy value to angle
    angleDouble = atof(angle); //convert string to float/double

    Serial.print("Angle: ");
    count = 0;   
    if (count < 5){
      fiveNumber[count] = angleDouble; //storing angle into array
      count++; //increment
      count = 0;

    if (x > 0){ //if distance is more than 0
      double delayTime = x * 10;  //distance * 10
      if(i > 1){
        // 1 second * distance // move for amount of seconds 10ms // delay time 10 = 1s
    else {
    newData = false;

// start of motor wheel movements
void forward(){
  SoftPWMSetPercent(in2, 200);  //motor 1
  SoftPWMSetPercent(in4, 200);  //motor 2
  SoftPWMSetPercent(in9, 199);  //motor 3
  SoftPWMSetPercent(in11, 199);  //motor 4

void backward(int speed) {
  SoftPWMSetPercent(in3, speed);  //motor 1
  SoftPWMSetPercent(in5, speed);  //motor 2
  SoftPWMSetPercent(in10, speed);  //motor 3
  SoftPWMSetPercent(in12, speed);  //motor 4

void turnLeft(int speed){
  SoftPWMSetPercent(in4, speed);  //motor 2 forward
  SoftPWMSetPercent(in11, speed); //motor 4 forward
  SoftPWMSetPercent(in3, speed);  //motor 1 backward
  SoftPWMSetPercent(in10, speed); //motor 3 backward

void turnRight(int speed){
  SoftPWMSetPercent(in2, speed);  //motor 1 forward
  SoftPWMSetPercent(in9, speed);  //motor 3 forward
  SoftPWMSetPercent(in5, speed);  //motor 2 backward
  SoftPWMSetPercent(in12, speed); //motor 4 backward

int clear = 0; //set clear to 0
void clearAll(){  //set all speed of the motors to 0
  SoftPWMSetPercent(in2, clear);
  SoftPWMSetPercent(in3, clear);
  SoftPWMSetPercent(in4, clear);
  SoftPWMSetPercent(in5, clear);

  SoftPWMSetPercent(in9, clear);
  SoftPWMSetPercent(in10, clear);
  SoftPWMSetPercent(in11, clear);
  SoftPWMSetPercent(in12, clear);
// end of motor wheel movements
Please edit your post and correct the links. The forum sticky post will tell you how to post a link correctly.

(If I were you, I would not brag about the amount of code I had written: better programmers write less code!)
Oh great, yet another way to do markup without detection of URLs... Anyway, done.

As for the amount of code, I'm glad you reviewed the code, found out what I had to write, and what I was able to reuse from other people's work, because why rewrite what's already been written, and how much shorter what I wrote could have been if written by a better programmer :)
Project Guidance / Re: Multiple I2C Buses
Last post by Wawa - Today at 07:21 am
If the sensor has an address pin exposed, then you can change the I2C address of the device with a digital pin.

Use the address pin as chip select pin.
Set all devices to address B, and only one device to address A, talk to it, switch to the next device.

An I2C multiplexer can be used if the device has no address pin, or 3.3volt<>5volt level shifting is needed.
As GrumpyMike said. Better answers if we know which devices OP is talking about.
Post a link.
One last comment - just in case anyone comes here from a Google search...

The reception quality of the receiver is easily improved to that of most chinese-made commercial 433mhz products by winding up some .6 mm (~22-24 ga) wire into a coil as shown here:

The reception went from horrible-to-barely okay to.. just as I would expect any 433mhz device to perform in an average home.  Easily reaches over 30 meters now - and even penetrates multiple concrete walls.

All in all, hope the next person pulling their hair out finds this useful.  My journey today took me from about to throw all the receivers out to considering buying another 5-pack for the next project.
Project Guidance / Re: Multiple I2C Buses
Last post by wvmarle - Today at 07:14 am
It's more like the other way around. An I2C mux will work but depending on the sensor and the number of sensors better options may be available.

Another thing: multiple sensors may mean distance - how long wires are we looking at?
Programming Questions / Re: Split uint-32 to bytes
Last post by demkat1 - Today at 07:10 am
@CodingBadly : I understand the limits of 2560.

FYI, the calculation is a multiply of 2 trigonometric (sin of 2 angles). I do not use float, because of slow procedure. First I "mapped" 0..1 to 0..255 using int(255*sin(x))  (so the max product is 65535- uint16) and putting values for all interested angles in array . Working this way whole main calculation runs in about 16usec.,  but there is some error because of low resolution of "mapping". Then I used uint16, so the product goes to uint32 know what happens. I think its a dissaster of loosing 8usec in shifting. (in respect to the time for the rest calculation). Thats the story (and of course "thats the limits" is accepted)

@DKWatson : Ill give a try to your proposal and measure.
Français / Re: Serrure 12v
Last post by supercc - Today at 07:08 am
Si tu penses au 5V en provenance de l'arduino, je ne pense pas que les quelques dizaines de milli ampères fournies par une sortie de l'arduino soient suffisants pour faire bouger ta serrure.

Qu'elle est l'intensité nécessaire à ta serrure ?
Pages: [1] 2 3 ... 10