Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By alex_g
#65348 Complete Arduino newbie here, dropping in to say "hi".

I have been using the ESP8266 for a couple of months now, but always with NodeMCU and Lua.
I have never used Arduino before, not in any form whatsoever, but I was tempted to try it because of the huge amount of available libraries. I was specifically interested in receiving 433 MHz signals, which is currently not supported by Lua/NodeMCU. Even though it's been a very long time since I coded C, I do enjoy it and it's not alien to me, so I was tempted to try the ESP8266 Arduino interface.

I am absolutely delighted to say that my first experiments with rc-switch have worked as well or better than I expected, everything is working fine and all my signals are getting decoded correctly. Yay! Which is quite unexpected, as I certainly don't have much of an idea of what I'm doing. :D

For instance, is there no makefile involved in building a sketch/project? The sketch I ran had two files, but they wouldn't play well initially, in the end I'm not quite sure what I did, a bit of trying to compile each file separately, a bit of manual file deletions behind its back, in the end it built and worked fine, just don't ask me how...

My other main question is -
How does the Arduino way of working compare to the ESP idiom and to Lua? I see a lot of delay() loops - do they really just sit and busy-wait? Is there an equivalent of the timer triggers used by NodeMCU, and how do these correspond the "interrupts" mentioned in Arduino parlance? In other words any good tutorial information beyond the initial "how to set up a system", yeah, I sort of got that, I need a bit more now...

Ultimately, I'd like to be able to port some of these ideas to the ESP environment and to NodeMCU/Lua, but for now I'll be happy to spend the next while just learning the Arduino way.

Any pointers hints links info most welcome...

Really great asset this Arduino stuff.
Cheers!
User avatar
By mrburnette
#70977
alex_g wrote:{...}
I am absolutely delighted to say that my first experiments with rc-switch have worked as well or better than I expected, everything is working fine and all my signals are getting decoded correctly. Yay! Which is quite unexpected, as I certainly don't have much of an idea of what I'm doing. :D

For instance, is there no makefile involved in building a sketch/project? The sketch I ran had two files, but they wouldn't play well initially, in the end I'm not quite sure what I did, a bit of trying to compile each file separately, a bit of manual file deletions behind its back, in the end it built and worked fine, just don't ask me how...
{...}


Alex:
Part of the misunderstanding is that you are unfamiliar with how Arduino works on the AVR line of official hardware. Arduino evolved from an academic environment and quickly found its way into the HOBBY scene. While the environment is C/C++ the Arduino IDE hides the make file and does a lots of work that sometimes fails: such as automatic prototyping.

With ESP8266, even more is hidden. There are two threads running on one core. One thread is the ESP code and libraries which was linked into the Arduino compiled code. Also, the ESP side sets up a task manager named non-OS which essentially alternates between the ESP code for the RF and protocol stacks and the Arduino. The Arduino side must release control no longer than every 50mS to keep things working properly.

Therefore, loop(), delay(n), delay(0), delay(1), and yield() all release the Arduino code so that the ESP can perform work necessary. When the ESP side is ready, it returns to the Arduino execution.

Therefore, some stock Arduino code must be modified so that loop() executes in less than 50mS or some delay element must inform the non-OS that it is time to switch threads. This can often be hit-miss situation if your Arduino code is waiting on a non-modified library.

Ray

Calculate prime numbers (delay(0) is necessary):
Code: Select all/*
  PrimeNos: by Nick Gammon
  ESP8266 Arduino port m. ray burnette
  OS version Linux Mint 17.3 64-bit tested on 20160409
  Compiled under Arduino 1.6.8
    Sketch uses 231,399 bytes (22%) of program storage space. Maximum is 1,044,464 bytes.
    Global variables use 31,718 bytes (38%) of dynamic memory, leaving 50,202 bytes for local variables. Maximum is 81,920 bytes.
  PUBLIC DOMAIN EXAMPLE
*/

#define BAUD 115200
#define BOARD_LED_PIN 2

// just add more primes to the prime table for larger search
// byte data type to save memory - use a larger datatype with prime table entries above 255 :)
byte primes[]={
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
    102, 107, 109, 113, 127, 131,  137 , 139, 149, 151, 157, 163, 167, 173,  179, 181, 191, 193, 197,
    199, 211, 223, 227, 229, 233, 239, 241, 251 };

// if you change the datatype of primes array to int, change next line to
// "const int TopPrimeIndex = (sizeof(primes)/2) - 1;"

const unsigned int TopPrimeIndex = sizeof(primes) - 1;     
const unsigned long TopPrimeSquared = (long)primes[TopPrimeIndex] * (long)primes[TopPrimeIndex];
int primeFlag;


extern "C" {
#include "user_interface.h"
}

void setup()                   
{
  system_update_cpu_freq(80);
  // system_update_cpu_freq(160);
  // initialize the digital pin as an output.
  pinMode(BOARD_LED_PIN, OUTPUT);
  Serial.begin(BAUD);  // BAUD has no effect on USB serial: placeholder for physical UART

  Serial.println("Prime Number Generator");
  Serial.print("Number of primes in prime table = ");
  Serial.println(TopPrimeIndex);
  Serial.println();
  Serial.print("Last prime in table =  ");
  Serial.println((unsigned int)primes[TopPrimeIndex]);
  Serial.println();

  Serial.print("Calculating primes through ");
  Serial.println(TopPrimeSquared);
  Serial.println();


}
void loop()                     // run over and over again
{
    for (long x = 1; x < TopPrimeSquared; x+=2){  // skips even numbers, including 2, which is prime, but it makes algorithm tad faster

            for (long j=0; j < TopPrimeIndex; j++){
            primeFlag = true;

            if (x == primes[j]) break;

            if (x % primes[j] == 0){     // if the test number modolo (next number from prime table) == 0
                primeFlag = false;       //  then test number is not prime, bailout and check the next number
                break;
            }
            delay(0);
        }
        if (primeFlag == true){           // found a prime - print it
            Serial.println(x);
       }
    }
}