ESP32 unable to detect interrupt - esp32

The code below is suppose to detect a pulse from a rain gauge. It is working in esp8266 but not in esp32 nodemcu. The logic seems fine I suppose I did something wrong while define the pin?
const int interruptPin = 18;
volatile boolean interrupt = false;
const int led = 2 ;
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), ISR, FALLING);
pinMode(led,OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (interrupt == true) {
Serial.println("An interrupt occurred");
delay(500);
digitalWrite(led, LOW);
interrupt = false;
}
delay(1000);
}
IRAM_ATTR void ISR () {
digitalWrite(led, HIGH);
interrupt = true;
}

Related

How to read from two serial monitors at a time?

Here's my code:
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int SerialPrint = 0;
int SerialMonitor = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
SerialPrint = analogRead(AnalogInPin1);
Serial.print("Sensor = ");
Serial.println(SerialPrint);
if (SerialPrint > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(500);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
delay(500);
}
}
void setup1() {
Serial.begin();
}
void loop1() {
SerialMonitor = analogRead(AnalogInPin2);
Serial.print("Sensor = ");
Serial.println(SerialMonitor);
if (SerialMonitor > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(500);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(500);
}
}
I am trying to make a car guidance system like the ones found in underground parking lots. and I am trying to have two serial monitors read from 2 sensors.
f�������x怘�f�������x怘
The characters above are looping in the serial monitor whilst using 19200 baud but not in 9600 baud why is that?
If your question is about how to display two analogRead values in one SerialMonitor sketch, this should compile and work (if your SerialMonitor is set to 9600, of course):
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int value1 = 0;
int value2 = 0;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
value1 = analogRead(AnalogInPin1);
Serial.print("Sensor1 = ");
Serial.print(value1);
if (value1 > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
value2 = analogRead(AnalogInPin2);
Serial.print("\t Sensor2 = ");
Serial.println(value2);
if (value2 > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
delay(500);
}
I left most of your code as is, just made it compile and work. If this is not what you want, please edit your question.

How to resolve this problem named " 'button' was not declared in this scope " in arduino?

.
.
.
void setup() {
/*
RGB LED 입출력설정
*/
pinMode(R_LED_PIN, OUTPUT);
pinMode(G_LED_PIN, OUTPUT);
pinMode(B_LED_PIN, OUTPUT);
lcd.init(); // LCD 초기화
lcd.backlight(); // LCD 백라이트 켜기(화면 밝아짐)
lcd.setCursor(2,0); // 커서 (2,0)
lcd.print("*INTERRUPT*"); // 글자출력
delay(500);
attachInterrupt(digitalPinToInterrupt(BUTTONPIN), button, FALLING);
/* 인터럽트 설정(아두이노 3번 핀, button 함수 호출, 모드 FALLING */
}
void loop() {
/*
led_color 함수에 Red, Green에 무작위 값과 Blue에는 0을 인자로 전달
delay를 0~99 사이의 무작위 값으로 설정
*/
led_color(random(256), random(256), 0);
int Blue = 0;
delay(random(100));
.
.
.
I can't resolve the problem like a title
in the interrupt zone: attachInterrupt(digitalPinToInterrupt(BUTTONPIN), button, FALLING);
Arduino uno error makes me crazy!!!!!!
Please help me...
You have not shared the whole code but for your understanding I have written code in place of button you have to provide a function checkSwitch.
const byte ledPin = 13;
const byte buttonPin = 2;
// Boolean to represent toggle state
volatile bool toggleState = false;
void checkSwitch() {
// Check status of switch
// Toggle LED if button pressed
if (digitalRead(buttonPin) == LOW) {
// Switch was pressed
// Change state of toggle
toggleState = !toggleState;
// Indicate state on LED
digitalWrite(ledPin, toggleState);
}
}
void setup() {
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Set switch pin as INPUT with pullup
pinMode(buttonPin, INPUT_PULLUP);
// Attach Interrupt to Interrupt Service Routine
attachInterrupt(digitalPinToInterrupt(buttonPin),checkSwitch, FALLING);
}
void loop() {
// 5-second time delay
Serial.println("Delay Started");
delay(5000);
Serial.println("Delay Finished");
Serial.println("..............");
}

Arduino Project (TRAFFIC LIGHTS) *need ideas*

This is the code I have so far and I need help making the button which is on pin2 of the Arduino have an interrupt where if the lights can cycle on there own and if the button is pressed then it must kick on the yellow LED for 3sec and give the red LED for 10sec for the pedestrian to walk across. The cycle I have set but I need the pushbutton to work and im having difficulties making this work, any suggestions?
CODE
int red = 10;
int yellow = 9;
int green = 8;
int button = 2; // switch is on pin 2
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT);
digitalWrite(green, HIGH);
}
void loop() {
if (digitalRead(button) == HIGH){
delay(15); // software debounce
if (digitalRead(button) == HIGH) {
// if the switch is LOW change the lights
changeLights();
delay(1000); // wait for 1 second
}
}
}
void changeLights(){
// RED ON for 5 sec
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
delay(5000);
// GREEN ON for 5 sec
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(5000);
// YELOOW ON for 3 sec
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
delay(3000);
}
This is Arduinos example code for external interrupts:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
If the state of pinput pin interruptPin changes the microcontroller will service that interrupt calling blink. then it jumps back to into the application where it was interrupted.
So attach an interrupt service routine to your button pin and store in a variable the information that you should start the yellow red phase at the next chance.
as the yellow red phase usually cannot be interrupted you can use blocking code like delay to time it if your program is not controlling anything elss in the meantime.

Keep ESP32 AutoConnect Captive Portal Always Running

I have an ESP32 that is running in softap mode with AutoConnect. I'm trying to get the captive portal to always be active, even if the client portion is connected to an AP. The current problem is once the ESP32 establishes the client connection with the router or after the initial timeout period, the captive portal goes away and you have to manually enter the IP address of the of the softap in the web browser.
Is there a setting I'm missing that allows this to happen?
//Config.autoRise = true;
Config.immediateStart = true;
Config.portalTimeout = 30 * 1000;
Config.retainPortal = true;
Config.title = "ESP32 AP";
Config.homeUri = "/";
portal.config(Config);
AutoConnectConfig Config;
//Config.bootUri=URI;
Config.homeUri="/_ac";
Config.apid = "Vedesp32-1";
Config.psk = "vedesp32";
Config.apip=IPAddress(10,1,1,1);
//Config.immediateStart = true;
Config.hostName = "esp32.local";
Config.title = "NBrowser";
Config.autoSave = AC_SAVECREDENTIAL_AUTO;
Config.autoReset = false;
Config.autoReconnect = true;
Config.reconnectInterval = 6;
Config.autoRise = true;
Config.retainPortal = true;
Portal.onDetect(atDetect);
Portal.config(Config);
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <WiFiClient.h>
#include <time.h>
#include <sys/time.h>
#include <AutoConnect.h>
#define IND 2
#define BUZZER 5
#define SWITCH_ON "True"
#define SWITCH_OFF "False"
#define CREDENTIAL_OFFSET 0
const char* URI = "/_ac";
TimerHandle_t wifiReconnectTimer;
AutoConnect Portal;
void pinInit() {
pinMode(BUZZER, OUTPUT);
pinMode(IND, OUTPUT);
}
void connectToWifi() {
Serial.println("Connecting to Wi-Fi... ->");
if (WiFi.status() == WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(BUZZER, HIGH);
digitalWrite(IND, HIGH);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
digitalWrite(IND, LOW);
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void setup() {
Serial.begin(115200);
Serial.println();
pinInit();
digitalWrite(IND, LOW);
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
AutoConnectConfig Config;
//Config.bootUri=URI;
Config.homeUri="/_ac";
Config.apid = "Ved";
Config.psk = "vedesp32";
Config.apip=IPAddress(10,1,1,1);
//Config.immediateStart = true;
Config.hostName = "esp32.local";
Config.title = "NBrowser";
Config.autoSave = AC_SAVECREDENTIAL_AUTO;
Config.autoReset = false; // Not reset the module even by intentional disconnection using AutoConnect menu.
Config.autoReconnect = true; // Reconnect to known access points.
Config.reconnectInterval = 6; // Reconnection attempting interval is 3[min].
Config.autoRise = true;
Config.retainPortal = true; // Keep the captive portal open.
Portal.onDetect(atDetect);
Portal.config(Config);
// Start
if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
digitalWrite(IND, HIGH);
}
connectToWifi();
}
bool atDetect(IPAddress& softapIP) {
Serial.println("Captive portal started, SoftAP IP:" + softapIP.toString());
return true;
}
void loop() {
Portal.handleClient();
}

Program for Blinking led quickly for some time and then slowly on Arduino UNO

I am new to Arduino,i want my led to blink 5 time quickly for time period 1s and then slowly for time period 4s, i tried like this,
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n=1;
while (n<=5)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
while (n<=10)
{
digitalWrite (13, HIGH);
delay (2000) ;
digitalWrite(13, LOW) ;
delay(2000) ;
}
But it's not working, please help me to fix it. Thanks
You are not updating 'n' inside your while loop
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n=1;
while(n++ <= 5)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
while(n++ <= 10)
{
digitalWrite (13, HIGH);
delay (2000) ;
digitalWrite(13, LOW) ;
delay(2000) ;
}
}
This should now work as intended.
Your code has infinite loops. Since you are just counting from 1 to 5 or 10, why not make them for loops?
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n;
for (n = 1; n <= 5; n++)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
for (n = 1; n <= 10; n++)
{
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(2000);
}
}

Resources