Interrupts function not called with ESP32 Arduino - arduino

I am using the ESP32 dev board with the Arduino and wish to use an interrupt to determine if a master device has sent the Slave Select flag to the ESP32.
Here is what I've tried so far
1)
#define SS 34
void enableSend() {
Serial.println("SS Enabled");
}
void setup(){
pinMode(SS, INPUT);
attachInterrupt(digitalPinToInterrupt(SS), enableSend, RISING);
}
2)
#define SS 34
void IRAM_ATTR enableSend() {
Serial.println("SS Enabled");
}
void setup(){
pinMode(SS, INPUT);
attachInterrupt(digitalPinToInterrupt(SS), enableSend, RISING);
}
I've used both versions without digitalPinToInterrupt.
What do I need to do to get the interrupts to work? Currently, the enableSend function does not get called when pin 34 is set to high.
If it helps, here is the pinout for the ESP dev bord that I have.

You definitely need the IRAM_ATTR (see 2) in order to place the ISR (interrupt service routine) in the Internal RAM (IRAM) of the ESP32.
I think that the Serial.println("SS Enabled"); that you wish to execute in the ISR
is too complex for an ISR.
The ESP32 stops the regular execution flow to execute the ISR, which is supposed to
be executable in minimal time, only a few CPU instructions.
Sending data over the Serial Port takes quite a few milliseconds, which is too long for an ISR.
You can use the ISR to change a variable / flag that you evaluate within the regular
flow of execution, that way you minimize the risk of messing up the devices flow of
execution... you can send the "SS Enabled" from there.

Related

trouble triggering relay with Bluetooth on an Arduini Mega

I'm planning to make a Bluetooth controlled relay. but i keep having problems triggering the relay with my Bluetooth entries.The data i write on my phone is displayed in the serial monitor but i just cant get the relay to turn off or on.i am using a single channel relay module and the HC-06 Bluetooth module.
can someone please tell me what the problem is?? this is driving me crazy. my Bluetooth module is connected to TX and RX respectively and my relay is connected to pin 13 of my arduino mega.
Thank You in advance!
#include <SoftwareSerial.h>
SoftwareSerial bt(1,0);
int RelayStateOn =0;
void setup() {
bt.begin(9600);
pinMode(13,OUTPUT);
}
char val=bt.read();
void loop() {
if(bt.available()) {
bt.println(val);
}
if(val==1) {
digitalWrite(13,HIGH);
}
else if(val==0) {
digitalWrite(13,LOW);
}
}
Firstly, you don't have a call to read from the bt in the loop anywhere. You have a call to bt.read() at global scope, so val gets a value that was read before anything was ready to read. So it most likely gets a -1.
You also have a call to avaialable before you have a call to print which implies that you don't understand what available is for. It tells if there is more available to read. So that should be around the line in loop that you're going to add that reads from bt not the one that prints to it.
To start with, where you have the bt.println(val), change that to val = bt.read().

Problems on ESP8266 programming based on Arduino IDE for ESP8266

I decided to use Arduino IDE for ESP8266 to program on my ESP8266, targeting to read data (bytes array from a TTL camera). ESP8266-01 (8Mbits Flash ROM) has 8 pins so I decided to use GPIO16 (TXD) and GPIO2 (RXD) as SoftwareSerial pins to achieve this. But the ESP8266 printed an exception and I am not sure what happened to it.
So I have a few question about this crash.
I want to know whether I can run SoftwareSerial on ESP8266 (ESP8266 has 2 UARTs, Serial0 for Serial print and we could not send data through Serial1), so I decided to use SoftwareSerial.
I don't know what the exception info means for I can't understand assembly language.
I read documents on Github for Arduino IDE for ESP8266, but I don't understand well about the pin definitions on ESP8266 when programming with Arduino IDE. For example, when using GPIO16 (TXD) and GPIO2 (RXD) as SoftwareSerial, we might use Constructor SoftwareSerial Camera(int Pin1, int Pin2). I want to know what the corresponding Pin1 and Pin2 for GPIO2 and GPIO16) is. The document really confused me.
Here is my key code.
#include <Arduino.h>
#include "camera_VC0706.h"
#include <SoftwareSerial.h>
HTTPClient httpClient;
//define camera and bytes array, I am not sure whether the pins are correct or not
int rxPin=2;
int txPin=16;
SoftwareSerial cameraSerial(rxPin,txPin); //RX,TX
camera_VC0706 Camera = camera_VC0706(&cameraSerial);
//take photo
bool takePhoto() {
byte time=0;
writeRawSerial(F("<STATUS>WAITING</STATUS>"),true);
while(!Camera.begin(115200)) {
if(+time<=5){//try 5 times
writeRawSerial(F("."),false);
} else {
writeRawSerial("",true);
writeSerial(F("<STATUS>NO_CAMERA</STATUS>"));
return false;
}
}
writeRawSerial(F("<STATUS>CAMERA_ON</STATUS>"), false);
writeRawSerial(F("<VERSION>"), false);
writeRawSerial(Camera.getVersion(), false);
writeSerial(F("</VERSION>"));
Camera.setImageSize(VC0706_320x240);
if (!Camera.takePicture()) {
writeSerial(F("<STATUS>TAKE_PHOTO_FAIL</STATUS>"));
return false;
} else {
byte imgSize = Camera.frameLength();
writeSerial(F("<STATUS>TAKE_PHOTO_SUCCESS</STATUS>"));
writeRawSerial(F("<IMAGE_SIZE>"),false);
writeRawSerial(String(imgSize,DEC),false);
writeSerial(F("</IMAGE_SIZE>"));
freeHeap();//It was defined, but not key function, only for showing free heap of esp8266
imgBuffer=Camera.readPicture(imgSize);
freeHeap();
Camera.resumeVideo();
writeSerial(F("<STATUS>SAVE_PHOTO_SUCCESS</STATUS>"));
return true;
}
}
Thank you for reading my questions.
You need to add the standard Arduino functions of setup() and loop() or it won't know where to start.
If you have a look at the example sketches you should be able to get something running, and then you can start adding your takePhoto code.
So, the stack trace shown is pretty standard for any system, and it gives you pretty much everything you need to track it down - It is not assembly code, it is hexadecimal addresses of your binary.
First, you have an Exception 28 - If we look at the ESP8266 Reference, you can see 28 means you either have a bad pointer, or you're trying to access non-cached data from an interrupt/cache is turned off.
Next, you have an address, epc1. This is where the code crashes, which is a hexadecimal address to the binary you uploaded. Also, ctx: cont is helpful, as it indicates the program crashed in your code and not the system code.
You also have excvaddr, which is the addressed you tried to access, 0x10.
Given that, you almost certainly have a NULL pointer somewhere in your code that you are dereferencing. You should use the ESP Exception Decoder to determine what line has the offending crash and bad pointer usage.
In regards to using the software serial - You'd just use the pin numbers on the datasheet. GPIO 14 would be number 14 in the constructor. I found it worked, but caused very bizarre crashes in the production products I worked with after 1+ days of usage, so do not recommend it at all. We ended up using Serial1 for debug printing, and freed up Serial0 for general UART communications - Way more reliable.

Adafruit Feather Huzzah ESP8266 pull-up resistor using Arduino IDE

I am working on the Adafruit Feather Huzzah ESP8266 and I would like to add an internal pull-up to a push button. When I do the pull-up manually with a resistor, my wiring works perfectly but when I add an internal pull-up using the following line in my Arduino IDE code, it does not work properly.
pinMode(BOUTON,INPUT_PULLUP)
Here is my full code:
const int LED = 13;
const int BOUTON = 16;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BOUTON, INPUT);
}
void loop()
{
int etat = digitalRead(BOUTON);
if(etat==LOW)
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}
Here is my wiring with the pull-up:
Here is my wiring with supposed internal pull-up:
When writing a program, you had erroneously assumed that GPIO16 had a pullup resistor. It does not.
At startup, pins are configured as INPUT.
GPIO0-GPIO15 can be INPUT, OUTPUT, or INPUT_PULLUP. GPIO16 can be INPUT, OUTPUT, or INPUT_PULLDOWN_16. It is also XPD for deepSleep() (perhaps via a small capacitor.)
Note that GPIO6-GPIO11 are typically used to interface
with the flash memory ICs on most esp8266 modules, so these pins
should not generally be used.
Reference: http://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
Have a look at Adafruits overview of the Feather HUZZAH ESP8266. You are able to use the GPIO's 0, 2, 4, 5, 12, 13, 14, 15, 16 but:
These pins are general purpose and can be used for any sort of input
or output. Most also have the ability to turn on an internal pullup.
Many have special functionality.
Having detailed look at GPIO 16 shows us it is very special. The GPIO is used to wake up the controller from deep sleep. Therefore it is the only GPIO have a built in pull down resistor which is described in this beginners guide. This allows to directly connect with RESET. This means GPIO 16 has no pull up resistor like assumed.

Not able to upload to my Arduino Micro board

I uploaded the following program to my Arduino micro:
#include <Mouse.h>
int buttonPin;
void setup() {
pinMode(buttonPin, INPUT);
Mouse.begin();
}
void loop() {
if(digitalRead(buttonPin) == HIGH) {
Mouse.click(MOUSE_LEFT);
}
}
First of all, yes I already know that I haven't defined buttonPin (I realized after the fact) but, this is the code as I uploaded it. Now when ever I plug my Arduino in to try to upload a program it spam clicks, causing the Arduino IDE to overload and not upload my program. It should also be noted that one time it also overloaded my computer forcing me to unplug the Arduino and reset my computer. My question is: #1: is there any way to actually fix the Arduino (I also have an Arduino Uno if I need to hook it up to that for some reason) and #2: when or if I get the Arduino working again, how would I fix my code. (I'm guessing the answer to #2 would be changing int buttonPin; to int buttonPin = 2)
Connect pull-down resistor to pin 0 because global variables are initialized to theirs default values. This should stop spam from mouse and it should be possible to upload code.
Otherwise you need another Arduino as Arduino ISP and upload new code over 6pin serial interface.

Arduino dealing with multiple pins

I'm new at arduino, Here is a problem in inputting data from multiple pins and writing in to other pins, The input comes from transmitter's reciever and it writes data to KK board pins.The code is simple however when it takes an input from one pin the other pin is disabled and the button is not working. Here is the code:
Servo ale, ele;
Void setup()
{
ale.attach(11);
ele.attach(12);
........
.....
}
Void loop()
{
a = pulseIn(6, HIGH, 20000);
b = pulseIn(7, HIGH, 20000);
ale.writeMicroseconds(a);
ele.writeMicroseconds(b);
..........
......
}
Is there something that i'm doing wrong?
The pulseIn function waits for the pin to go high, than waits for the pin to go low and only then it returns and the execution of the program continues for the next line.
If you want to be able to receive input from several pins concurrently, you need to simulate pulseIn behavior over several cycles of loop function.
There are two options (simplified pseudo code):
For each pin separately, every loop cycle you read pin value, if it is transitioning from low to high, a flag a set and you save the current millis. When it goes low again, you measure the current millis and subtract the previous reading. This will give you the a or b.
Same as 1 but instead of checking every loop cycle, you can attach interrupts for low to high and high to low transitions.

Resources