I am new to programming and Arduino.
Board - ESP8266 Nodemcu pinout as below,
What I am trying to achieve is send a command based LOW/HIGH value from pin 0.
A two leg switch's one leg is connected to D3 (GPIO0 and in program 0) and other to ground.
The code I am trying is below,
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
byte exampleButtonAvalue = digitalRead(exampleButtonA);
switch(exampleButtonAvalue)
{
case LOW:
Serial.println("ON IT IS");
break;
case HIGH:
Serial.println("OFF IT IS");
break;
default:
Serial.println("error!");
break;
}
}
Issue I am facing is, when I flash this program, Based on physical switch on or off
It continually prints either "ON IT IS" or "OFF IT IS"
The break is really not happening. I only want it to execute once.
I also tried this with if else and face same problem of repeated printing.
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
if(digitalRead(exampleButtonA) == LOW){
Serial.println("ON");
delay(200);
}
else {
Serial.println("OFF");
delay(200);
}
}
Any assistance?
If you want to see message only once you need to write youre code in setup() section.
All code in loop() section is repeated in loop.
Replace you code with this:
#include<BitsAndDroidsFlightConnector.h>
BitsAndDroidsFlightConnector connector = BitsAndDroidsFlightConnector();
const byte exampleButtonA = 0;
int exampleButtonAvalue = 0;
int saved_exampleButtonAvalue = 0;
void setup() {
Serial.begin(115200);
pinMode(exampleButtonA, INPUT_PULLUP);
}
void loop() {
exampleButtonAvalue = digitalRead(exampleButtonA);
if (exampleButtonAvalue != saved_exampleButtonAvalue){
if(exampleButtonAvalue == LOW){
Serial.println("ON");
} else {
Serial.println("OFF");
}
saved_exampleButtonAvalue = exampleButtonAvalue;
}
delay(200);
}
Related
I am new to this shift register. So i created a online simulation of the shift register that hook up to 8 LEDs, the ol' 8bit translate to 8 LED experiment. My design is that when i entered a character like "a" into the serial monitor, it will show in the result in code like 100000, and it should shown in the LEDs too(the sixth LED should light up).
char character;
byte input = 0b01;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
byte input = 0b01;
if (Serial.available() > 0) {
character = Serial.read();
if (character == 'a') {
Serial.print("character received: ");
Serial.println(character);
for (int i = 0; i < 5; i++) {
updateShiftRegister(input);
input = input << 1;
delay(100);
}
Serial.print("Input: ");
Serial.print(input, BIN);
Serial.println();
} else {
Serial.print("Please retry.\n");
}
}
}
void updateShiftRegister(byte input) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, input);
digitalWrite(latchPin, HIGH);
}
Problem is when i run the thing, the result is alright but the shift register in the simulation is wack. Every LEDs turn on,
I found the shiftout function is main cause of this, any idea why it is causing every LED to light up?
I'm super new to programming so apologies if this is a badly phrased question or just confusing code! I have some Arduino code that automates things inside my campervan. The arduino reads data from temperature and battery sensors, and also controls the heating system. I had everything working, but the code was really inefficient and buggy, and I'd frequently get gibberish back when reading from the sensors. The internet suggested I use start and end markers to identify the useful information and discard the rest. Some copy and pasting and editing, I've ended up with the code below. However while the start and end markers are filtering out the information, I can't seem to make it call functions which actually do the controlling of things (e.g., move a servo, switch the heater on etc.).
// Van control system.
// Heater and sensor control code
// 20.03.2021
// This code controls the heater and reads from temp and power sensors.
#include <Servo.h> //Controls the heater air flow
#include <DHT.h> //temp sensor
#define DHT_SENSOR_TYPE DHT_TYPE_11
#include <Adafruit_INA260.h> //this breakout board monitors battery level and power consumption
Adafruit_INA260 ina260 = Adafruit_INA260();
Servo myservo; // servo to turn on heater
int pos = 90;// variable to store the servo position
#define DHTPIN 3 // temp sensor pin
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Set up pins, the controller uses a common ground, so only one
// ground connection is needed. Pin A and B are used as pairs to
// generate a quadrature signal that mimics a rotary encoder (which controllers the heater)
int pinA = 9; //Pin A of encoder output
int pinB = 10; // Pin B of encoder output
int offPin = 11; // Acts as momentary on button
int onPin = 12; // Act as momentary off button
const byte numChars = 32; //serial input buffer
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
dht.begin(); //initalise temp sensor
myservo.attach(5); //attaches servo to pin.
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(onPin, OUTPUT);
pinMode(offPin, OUTPUT);
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(onPin, LOW);
digitalWrite(offPin, LOW);
ina260.begin(); //initalise power sensor
Serial.println("<ATMega is ready>"); //confirms that microcontroller is ready
}
//End of set up process.
void loop() {
recvWithStartEndMarkers(); //for maximum efficiency, loop should be as short as possible.
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; //terminate string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars); //this works and I get the character back in the serial monitor.
if (receivedChars == "H") {
upHeat(); //this doesn't work, if the character is H I want to call this function
}
else if (receivedChars == "C") {
downHeat();
}
else if (receivedChars == "O") {
heatOn();
}
else if (receivedChars == "F") {
heatOff();
}
else if (receivedChars == "a") {
servoPosOne();
}
else if (receivedChars == "s") {
servoPosTwo();
}
else if (receivedChars == "d") {
servoPosThree();
}
newData = false;
}
}
void upHeat() { //this generates a quadrature to simulate a rotary encoder.
digitalWrite(pinB, HIGH);
delay(200);
digitalWrite(pinA, HIGH);
delay(200);
digitalWrite(pinB, LOW);
delay(200);
digitalWrite(pinA, LOW);
delay(200);
digitalWrite(pinB, HIGH);
delay(200);
digitalWrite(pinA, HIGH);
delay(200);
digitalWrite(pinB, LOW);
delay(200);
digitalWrite(pinA, LOW);
}
//I haven't included the rest of the functions because, if one works, the rest will.
Any help would be amazing. I feel like it could be something really simple that I'm missing. Equally it could all be completely terrible!
Kindly help me in the Arduino code as I am new in this field. I have Arduino code that turns the light in bulb ON and OFF using toggle switch. It is successfully running and giving output.
The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
} else {
digitalWrite(relay1, LOW);
}
Serial.println(buttonBulb);
}
Before following suggestion in the comment, the output was:
Issue
Bulb is turning ON and OFF when I toggle switch ON and OFF, and the output is showing on serial monitor continuously. But I want only one value that is not repeated. Like if I toggle the switch ON, then the value shown on serial monitor should be 1 and not 11111111....
Please help me about that. How can I do that?
After following suggestion in the comment, the code is:
int buttonPinBulb = 11;
int relay1 = 10;
int buttonBulb;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
Serial.println(buttonBulb);
}
void loop() {
// put your main code here, to run repeatedly:
buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
}else{
digitalWrite(relay1, LOW);
}
//Serial.println(buttonBulb);
}
And the output was:
You can use a global variable to store the current status of the button.
You may also want to debounce your button (using millis() in the below example, unless the debouncing is already done in hardware) - especially when you want to update a database each time the status changes.
int buttonPinBulb = 11;
int relay1 = 10;
int currentStatus = LOW;
// Debounce
unsigned long lastMillis = 0;
const unsigned long debounceTime = 100; // 100ms wait
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
unsigned long currentMillis = millis();
if ( (currentMillis - lastMillis > debounceTime)
|| (currentMillis < lastMillis)) { // protect against overflow
int buttonBulb = digitalRead(buttonPinBulb);
if (buttonBulb != currentStatus) {
digitalWrite(relay1, buttonBulb);
Serial.println(buttonBulb);
currentStatus = buttonBulb;
// update database here
}
lastMillis = currentMillis;
}
}
As you have been told in comments, just store the current value of the pin and issue digitalWrite () only if a change is required.
ccesfully running and giving output. The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
int curRel1;
void setup()
{
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
curRel1 = digitalRead(relay1);
Serial.begin(115200);
}
void loop()
{
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH)
{
digitalWrite(relay1, HIGH);
}
else
{
digitalWrite(relay1, LOW);
}
if (buttonBulb != curRel1)
{
Serial.println(buttonBulb);
curRel1 = buttonBulb;
}
}
You could also update curRel1 in loop() calling digitalRead().
I need make a traffic light that works on countdown:
when number on timer == 0 or if button pressed green led should turn on
otherwise red led
There is my loop,
I tried without for loop and button worked but if I add for loop (for timer) button not responding
thank you
crosswalk_button = digitalRead(2); //That will read the state of the button, if it's pressed or not
for (int i = numberfor7digit; i >= 0; i--) { //numberfor7digit is = 9
numbers(i); //numbers is a function i wrote which shows int it takes currently i
delay(1000);
if (crosswalk_button == 0) { //If you press the button for the crosswalk on with the green one for the crosswalk
numbers(0);
greenhigh(); // green high is a function too, which turns on green light
}
// when number in 7 segment is 0 it will turn on green
else if (i == 0) {
numbers(0);
greenhigh();
}
// for any other number it turns on red
else {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
}
}
}
Few things which will help you to design this system effectively are:
Use Interrupts to read the value of the button
This will make the turning on Green Light irrespective of what's been executed by the Arduino when the button is pressed.
Example: Illustration to show how to configure a button for interrupt
int interruptPin = 2; //button attached to this pin
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), greenhigh, LOW);
}
void loop() {
//Your code inside this loop
}
void greenhigh() {
//Your code for turning green light high
numbers(0);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
Seven Segment Display
Now, since the button is fixed writing code for seven segment display will be simpler.
Example: Illustration to show SSD working
digitalWrite(RED_LED, HIGH);
for (int i = numberfor7digit; i >= 0; i--) {
numbers(i);
delay(1000);
}
greenhigh();
delay(5000);
Conclusion
Complete code can be written as:
int interruptPin = 2; //button attached to this pin
int numberfor7digit = 9;
int GREEN_LED = 10; //TODO: Input here your actual green led
int RED_LED = 11; //TODO: Input here your actual red led
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), greenhigh, LOW);
}
void greenhigh() {
//TODO: Your code for turning green light high
numbers(0);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
void numbers(int n) {
//Your implementaion of numbers()
}
void loop() {
digitalWrite(RED_LED, HIGH);
for (int i = numberfor7digit; i >= 0; i--) {
numbers(i);
delay(1000);
}
greenhigh();
delay(5000);
}
You have to read the status of your button every time the loop runs i.e. the for loop that you are using.
And I have also removed the delay and replaced it with an if statement similar to blink without delay example.
One more thing, there is no need to create a crosswalk_button variable
You can directly replace it with digitalRead(2)
Use this code, it should work.
void loop() {
for(int i = numberfor7digit; ; i >= 0; i--) {
static long previousMillis = 0;
if(millis() - previousMillis >= 1000) {
numbers(i);
previousMillis = millis();
}
crosswalk_button = digitalRead(2);
if(crosswalk_button == false || i == 0) {
i = 0;
numbers(i);
greenhigh();
} else {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW):
}
}
}
I use 'if-else' with serial communication in Arduino. I am sending data in 'if' and 'else' sections using serial communication. But I can not stop sending data in the serial communications section. How can I provide it?
For example,
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop() {
if (Serial.avaliable() > 0) {
if (digitalRead(2) == HIGH)
{
Serial.print("Yes");
}
else {
Serial.print("No");
}
}
}
When I give number 5 to the number pin 2 , "Yes" flows continuously through the serial port. I know it's in the 'loop' section. But how do I stop it? So when I give the relevant pin to 5 V, can I get one response?
Save your state into a volatile global variable and print only if the state is being updated.
This is because the loop() function is called an infinite number of times.
You have two options:
Move the code from loop() to setup() and let the loop() function be empty
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
if (Serial.avaliable() > 0) {
if (digitalRead(2) == HIGH)
{
Serial.print("Yes");
}
else {
Serial.print("No");
}
}
}
void loop() {
}
Add some control logic like this
boolean alreadySent = false;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop() {
if (Serial.avaliable() > 0) {
if(alreadySent == false) {
if (digitalRead(2) == HIGH)
{
Serial.print("Yes");
}
else {
Serial.print("No");
}
alreadySent = true;
}
}
}