Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created July 10, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liz-miller/4747c4eb39d48ac5d0fde289a4f402f4 to your computer and use it in GitHub Desktop.
Save liz-miller/4747c4eb39d48ac5d0fde289a4f402f4 to your computer and use it in GitHub Desktop.
Test I/O connected to NodeMCU
/*
* TEST I/O On NodeMCU
* Written by Liz Miller
* Provided by Learn Robotics (www.learnrobotics.org)
* Version 1.0
* 7-10-18
* ---
* The Software is provided "AS IS" and "WITH ALL FAULTS,"
* without warranty of any kind, including without limitation
* the warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
* --- DEVICES & SENSORS ---
* Red LED - D1 / GPIO pin 5
* Yellow LED - D5 / GPIO pin 14
* LDR - AD0 / A0
*/
#include <Arduino.h>
// Button & LED Pin Numbers
const int yellowButton = 15; // the number of the pushbutton pin
const int yellowLED = 14; // the number of the LED pin
const int redButton = 4; // the number of the pushbutton pin
const int redLED = 5; // the number of the LED pin
const int ldr = A0; // the number of the ldr pin
int ldrData;
// Debouncing Variables
int ledStateY = LOW; // the current state of the output pin
int buttonStateY; // the current reading from the input pin
int lastButtonStateY = LOW; // the previous reading from the input pin
int ledStateR = LOW; // the current state of the output pin
int buttonStateR; // the current reading from the input pin
int lastButtonStateR = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
/*
* Configuration - runs once
*/
void setup() {
// put your setup code here, to run once:
pinMode(ldr,INPUT);
pinMode(redButton,INPUT);
pinMode(yellowButton,INPUT);
pinMode(redLED,OUTPUT);
pinMode(yellowLED,OUTPUT);
//NodeMCU runs @ 9600 baud
Serial.begin(9600);
}
/*
* Read LDR brightness from sensor
*/
void getLDRdata(){
ldrData = analogRead(ldr);
Serial.print("LDR Reading = ");
Serial.println(ldrData);
}
/*
* set LED's on or off using a String command
*/
void setLEDs(String status){
if(status=="on"){
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, HIGH);
Serial.println("LEDs on!");
}
else if(status=="off"){
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
Serial.println("LEDs off!");
}
}
/*
* Loop - executable code forever
*/
void loop(){
Serial.println("Next reading..."); //open the serial monitor to view print statements
getLDRdata(); //retrieve data from LDR
setLEDs("on"); //turn both LEDs on
delay(1000); //wait 1 sec
setLEDs("off"); //turn both LEDs off
delay(5000); //wait 5 sec
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment