I had written a code to calculate rpm of the motors using IR sensor and Arduino nano and displaying it on OLED . With that I want an LED to blink always. Here is the code what I did:
You can skip the declarations and setup()
part. Just move directly
to loop()
. I have problem in loop. In loop()
, only LED goes on
blinking and further rpm calculation with OLED displaying codes are not
performed.
#include "avr/sleep.h"
#include "avr/power.h"
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
int led = 12;
int in = 13;
int pushbutton=10;
unsigned long duration = 0;
float rpm = 0;
float rpm_a = 0;
int counter = 0;
int present = 0;
int previous = 0;
unsigned long elapsed = 0;
unsigned long elapsed_prev = 0;
int disabled = 0;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(in,INPUT);
pinMode(pushbutton,INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35,1);
display.print("xyz");
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25,13);
display.print("abc");
display.display();
delay(5000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("RPMmeter");
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,19);
display.println("RPM:");
display.setCursor(80,19);
display.println(rpm);
display.display();
elapsed = micros();
}
void loop()
{
/** led blink part **/
digitalWrite(led, HIGH);
delay(70);
digitalWrite(led, LOW);
delay(70);
digitalWrite(led, HIGH);
delay(70);
digitalWrite(led, LOW);
delay(900);
/** led blink part over and rpm calculation and displaying on
* oled part starts **/
if(digitalRead(pushbutton))
{
//Arduino low power enabled
if(disabled==0)
{
sleep_disable();
disabled = 1;
}
if (digitalRead(in) == 1 && previous == 0)
{
previous = 1;
duration = elapsed - elapsed_prev;
elapsed_prev = micros();
}
if (digitalRead(in) == 1 && previous == 1)
{
previous = 1;
}
if (digitalRead(in) == 0 && previous == 1)
{
previous = 0;
}
if (digitalRead(in) == 0 && previous == 0)
{
previous = 0;
elapsed = micros();
}
rpm = 60000000/duration;
//We add a small error in the rpm value (in this case +-2)
if ((rpm_a-2) < rpm && rpm < (rpm_a+2))
{
rpm_a = rpm;
counter = counter + 1;
if (counter == 50)
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("JARVIS RPMmeter");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,19);
display.println("RPM:");
display.setCursor(80,19);
display.println(rpm);
display.display();
counter = 0;
}
}
if (!( (rpm_a-2) < rpm && rpm < (rpm_a+2)))
{
rpm_a=rpm;
}
}//end if pushbutton=1
else {
display.display();
display.clearDisplay();
delay(10);
duration = 0;
rpm = 0;
rpm_a = 0;
counter = 0;
present = 0;
previous = 0;
//Arduino low power enabled
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
disabled = 0;
}
}
This was the code. The problem is without that led blinking code part in the loop, the code works fine, but if we include that LED blinking part, the code doesn't move further. I mean only LED goes on blinking and further codes are not performed. Why is it so? And how to make both rpm calculation with display on OLED as well as LED blinking happen through a single code simultaneously?
BlinkWithoutDelay
example of the Arduino IDE. How this is done is described in many many tutorials and also questions on this site.