Arduino RGB LED code issues - arduino

I'm having some trouble with my arduino code. My code has 3 potentiometers that alter each individual colors in a LED, and a button that will fade through red, green, blue (when pressed, it cycles once and when held it continuously cycles). It used to work, but when I tried to add a LCD screen that displays each RGB value, it stopped working. I know it's not written the best, it's just an experimental code as I just got an arduino and am just trying to familiarize myself. When I plug it in, the LCD prints all white squares on the top row and the led is red with flashing blue, and nothing happens when I press the button or turn the pots. Here is the code, any help is appreciated:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin1 = 14 ; // select the input pin for the potentiometer, ANALOG
int potPin2 = 15;
int potPin3 = 16;
int potVal1 = 0; // variable to store the value coming from the sensor
int potVal2 = 0;
int potVal3 = 0;
int ledPin1 = 6; // select the pin for the LED, PWM for analogWrite capability?
int ledPin2 = 9;
int ledPin3 = 10;
int buttonPin = 7;
int buttonState= LOW;
String printCycleVal1 = (""); //print values when cycling colors
String printCycleVal2 = ("");
String printCycleVal3 = ("");
String printVal1 = ("");//print values when not cycling colors
String printVal2 = ("");
String printVal3 = ("");
int val1 = 0; //values to store rgb colors in
int val2 = 0;
int val3 = 0;
void setup()
{
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(potPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(2,16);
}
void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
potVal1 = analogRead(potPin1);
val1 = map(potVal1, 0, 1023, 0, 255);
analogWrite(ledPin1, val1);
printVal1 += ("V1: ");
printVal1 += (val1);
printVal1 += (" | ");
potVal2 = analogRead(potPin2);
val2 = map(potVal2, 0, 1023, 0, 255);
analogWrite(ledPin2, val2);
printVal2 += ("V2: ");
printVal2 += (val2);
printVal2 += (" | ");
potVal3 = analogRead(potPin3);
val3 = map(potVal3, 0, 1023, 0, 255);
analogWrite(ledPin3, val3);
printVal3 = ("V3: ");
printVal3 += (val3);
buttonState = digitalRead(buttonPin);
while (buttonState == HIGH)
cycle();
}
void cycle() {
setColourRgb(0, 0, 0);
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
printCycleVal1 += ("V1: ");
printCycleVal1 += (rgbColour[0]);
printCycleVal1 += (" | ");
printCycleVal2 += ("V2: ");
printCycleVal2 += (rgbColour[1]);
printCycleVal2 += (" | ");
printCycleVal3 += ("V3: ");
printCycleVal3 += (rgbColour[2]);
delay(5);
}
buttonState = LOW; n
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(ledPin1, red);
analogWrite(ledPin2, green);
analogWrite(ledPin3, blue);
}

Related

Value of variable doesn't change

I am writing this code for a photoresistor on the arduino. I am supposed to attach servos to the photoresistor so that it will work as a moving solar panel. However, upon running the code I note that the value of variable pos (which is supposed to store the angle having max amount of light) does not change. What can I do about it?
int val1, val2, temp = 1000;
int pos = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
int val = map(sensorValue, 0, 1023, 0, 100);
Serial.println(val);
for(int i=0; i<180; i++){
val1 = map(sensorValue, 0, 1023, 0, 100);
if(val1 <= temp){
temp = val1;
pos = i;
}
delay(15);
}
Serial.println(pos);
delay(1000);
}
As #datafiddler suggested you need to ad the analogRead into the for loop.
Some things I would change in order to have a faster and cleaner code:
change the initialized temp to 100
delete val2
delete code before for loop
longer delay in for loop
here the edited code:
int val = 0;
int temp = 100;
int pos = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
//move servo to pos = 0
for(int i = 0; i < 180; i++){
val = analogRead(A0)
val = map(val, 0, 1023, 0, 100);
if(val <= temp){ //save minimum
temp = val;
pos = i; //save position
}
//move servo
delay(100); //try out what works best with the used servo
}
Serial.println(pos);
delay(1000);
}

Code to change color on led strip not working switching from Arduino Uno to NodeMCU esp8266

I wrote code to switch color on a LED strip depending on a value that I set (code below).
When I tried to load the same code on a NodeMCU ESP8266, this code doesn't work anymore. I used the library Adafruit Neopixel, which I thought is supported by the mentioned board.
Any help and/or advice is really welcome.
#include <Adafruit_NeoPixel.h>
//#define ATTINY
#define DIMCOLORE 3
#define NUMCOLORI (sizeof(colori) / ((DIMCOLORE)*sizeof(byte)))
#define DELTRANSIZIONE (tempoTransizione) / (numOfPixels)
#define SPENTO 0
#define ROSSO 1
#define VERDE 2
#define BLU 3
#define GIALLO 4
#define CIANO 5
#define MAGENTA 6
#define BIANCO 7
#define CELESTINO 8
const byte pinLuci = 4;
const byte numOfPixels = 51;
const int tresh_1 = 10; // white
const int tresh_2 = 20; // blue
const int tresh_3 = 30; // yellow
const int tresh_4 = 40; // red
const unsigned long tempoCritico = 5000;
const unsigned long tempoLampeggio = 300;
const unsigned long tempoTransizione = 270;
byte colori[][DIMCOLORE] = {
{ 0, 0, 0}, // 0 OF
{255, 0, 0}, // 1 ROSSO
{ 0, 255, 0}, // 2 VERDE
{ 0, 0, 255}, // 3 BLU
{255, 130, 0}, // 4 GIALLO
{ 0, 255, 255}, // 5 CIANO
{255, 0, 255}, // 6 MAGENTA
{255, 130, 50}, // 7 BIANCO
};
unsigned long inizioAlto = 0;
byte colore = BIANCO;
byte ultimoColore = colore;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfPixels, pinLuci, NEO_GRB + NEO_KHZ800);
void impostaColore(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
}
void impostaColore(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
}
void impostaSequenza(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) {
pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void impostaSequenza(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels ; i++) {
pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void lampeggia(byte indiceColore, byte volte, unsigned long tempoLamp) {
for (byte i = 0 ; i < volte ; i++) {
impostaColore(0);
delay(tempoLamp);
impostaColore(indiceColore);
delay(tempoLamp);
}
}
void setup() {
#ifndef ATTINY
Serial.begin(115200);
#endif
pixels.begin();
for (byte i = 0; i < 54; i++) {
pixels.setPixelColor(i, pixels.Color(colori[2][0], colori[2][1], colori[2][2]));
pixels.show();
}
for (byte j = 0 ; j <= NUMCOLORI ; j++) {
impostaColore(j);
delay(50);
}
impostaColore(BIANCO);
}
void loop() {
int value = 5;
#ifndef ATTINY
Serial.print(millis());
Serial.print('\t');
Serial.print(value);
#endif
if (value < tresh_1) {
#ifndef ATTINY
Serial.print(" 0 ");
#endif
inizioAlto = 0;
colore = BIANCO;
} else if ((value >= tresh_1) && (value < tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" BASSA ");
#endif
inizioAlto = 0;
colore = BLU;
} else if ((value >= tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" MEDIA ");
#endif
inizioAlto = 0;
colore = GIALLO;
} else if ((value >= tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" ALTA ");
#endif
inizioAlto = 0;
colore = ROSSO;
} else if (value >= tresh_4) {
#ifndef ATTINY
Serial.print(" MAX ");
#endif
if (inizioAlto == 0) {
#ifndef ATTINY
Serial.print(" ----- ");
#endif
inizioAlto = millis();
colore = ROSSO;
} else if ((millis() - inizioAlto) > tempoCritico) {
#ifndef ATTINY
Serial.print(" +++++ ");
#endif
lampeggia(ROSSO, 1, tempoLampeggio / 2);
}
}
if (colore != ultimoColore) {
impostaSequenza(colore);
ultimoColore = colore;
}
#ifndef ATTINY
Serial.println();
#endif
delay(tempoLampeggio / 2);
}
The unique line I changed it is about the pin name:
const byte pinLuci = D5;
I attached some photos about the hardware connection I made, maybe the problem could be there.
I want to make clear that with Arduino Uno everything works fine.
If the error is that i does not compile then post the error from the console.
Else it is properly the wrong pin you are using.
The pin numbers and GPIO numbers isn't the same.
If you want to use GPIO04 you should use pin D2 in your sketch. See the following pin mapping
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;

Arduino LED Controller hanging at startup

I'm trying to code an LED controller that controls the intensity via PWM. However, my issue is that I can't even get to the loop portion, it seems to hang at when I declare my class. I've tried checking to see if any of my functions in my class are causing the issues, but since I can't even get to loop, there must be something wrong within the class. I've written the class and placed it into a library called LED.
The code is somewhat long, but here it is:
#ifndef LED_H
#define LED_H
#include <LiquidCrystal.h>
#include <Button.h>
#include <EEPROM.h>
#include <TimeLib.h>
#include <PWM.h>
class LED
{
public:
LED();
int read_encoder(); //Reads rotary encoder
void clearLCD();
void setAllLed();
void printLCD();
void setOneLed(int);
int setLed(int, // current time in minutes
int, // pin for this channel of LEDs
int, // start time for this channel of LEDs
int, // photoperiod for this channel of LEDs
int, // fade duration for this channel of LEDs
int, // max value for this channel
bool // true if the channel is inverted
);
void menuWizard();
int subMenuWizard(int, int, bool, bool);
void displayMainMenu();
void printMins(int, bool);
void printHMS(byte,byte,byte);
long EEPROMReadlong(long);
void EEPROMWritelong(int, long);
bool pressSelect();
bool pressBack();
void rotateCheck(int&, int, int);
//variables for the LED channels
int minCounter = 0; // counter that resets at midnight.
int oldMinCounter = 0; // counter that resets at midnight.
int ledPins[5]={2,3,5,6,7};
int ledVal[5]={0,0,0,0,0};
// Variables making use of EEPROM memory:
int variablesList[20];
bool invertedLEDs[5]={false,false,false,false,false};
//Backlight Variables
unsigned long backlightIdleMs = 0;
private:
};
#endif // LED_H
And here is the .cpp file:
#define LCD_RS 35 // RS pin
#define LCD_ENABLE 34 // enable pin
#define LCD_DATA4 33 // d4 pin
#define LCD_DATA5 32 // d5 pin
#define LCD_DATA6 31 // d6 pin
#define LCD_DATA7 30 // d7 pin
#define LCD_BACKLIGHT 9 // backlight pin
// Backlight config
#define BACKLIGHT_DIM 10 // PWM value for backlight at idle
#define BACKLIGHT_ON 70 // PWM value for backlight when on
#define BACKLIGHT_IDLE_MS 10000 // Backlight idle delay
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
#include <LiquidCrystal.h>
#include <Button.h>
#include <EEPROM.h>
#include <TimeLib.h>
#include <PWM.h>
#include "LED.h"
LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_DATA4, LCD_DATA5, LCD_DATA6, LCD_DATA7);
Button goBack=Button(12, PULLDOWN);
Button select=Button(13, PULLDOWN);
LED::LED()
{
InitTimersSafe();
pinMode(LCD_BACKLIGHT, OUTPUT);
lcd.begin(16, 2);
digitalWrite(LCD_BACKLIGHT, HIGH);
lcd.print("sEx LED, V1");
clearLCD();
delay(5000);
analogWrite(LCD_BACKLIGHT, BACKLIGHT_DIM);
if (variablesList[0] > 1440 || variablesList[0] < 0) {
variablesList[0] = 720; // minute to start this channel.
variablesList[1] = 400; // photoperiod in minutes for this channel.
variablesList[2] = 100; // max intensity for this channel, as a percentage
variablesList[3] = 100; // duration of the fade on and off for sunrise and sunset for
// this channel.
variablesList[4] = 720;
variablesList[5] = 400;
variablesList[6] = 100;
variablesList[7] = 100;
variablesList[8] = 720;
variablesList[9] = 400;
variablesList[10] = 100;
variablesList[11] = 100;
variablesList[12] = 720;
variablesList[13] = 400;
variablesList[14] = 100;
variablesList[15] = 100;
variablesList[16] = 720;
variablesList[17] = 400;
variablesList[18] = 100;
variablesList[19] = 100;
}
else {
variablesList[0] = EEPROMReadlong(0); // minute to start this channel.
variablesList[1] = EEPROMReadlong(4); // photoperiod in minutes for this channel.
variablesList[2] = EEPROMReadlong(8); // max intensity for this channel, as a percentage
variablesList[3] = EEPROMReadlong(12); // duration of the fade on and off for sunrise and sunset for
// this channel.
variablesList[4] = EEPROMReadlong(16);
variablesList[5] = EEPROMReadlong(20);
variablesList[6] = EEPROMReadlong(24);
variablesList[7] = EEPROMReadlong(28);
variablesList[8] = EEPROMReadlong(32);
variablesList[9] = EEPROMReadlong(36);
variablesList[10] = EEPROMReadlong(40);
variablesList[11] = EEPROMReadlong(44);
variablesList[12] = EEPROMReadlong(48);
variablesList[13] = EEPROMReadlong(52);
variablesList[14] = EEPROMReadlong(56);
variablesList[15] = EEPROMReadlong(60);
variablesList[16] = EEPROMReadlong(64);
variablesList[17] = EEPROMReadlong(68);
variablesList[18] = EEPROMReadlong(72);
variablesList[19] = EEPROMReadlong(76);
}
}
void LED::printLCD(){lcd.print("test");clearLCD();delay(2000);lcd.print("testing");clearLCD();}
bool LED::pressSelect(){
if (select.uniquePress()){return 1;}
else {return 0;}
}
bool LED::pressBack(){
if (goBack.uniquePress()){return 1;}
else {return 0;}
}
void LED::clearLCD(){
lcd.clear();
}
void LED::displayMainMenu(){
oldMinCounter = minCounter;
minCounter = hour() * 60 + minute();
for (int i=0;i<17;i=i+4){
if (variablesList[i+3] > variablesList[i+1] / 2 && variablesList[i+1] > 0) {
variablesList[i+3] = variablesList[i+1] / 2;
}
if (variablesList[i+3] < 1) {
variablesList[i+3] = 1;
}
}
//check & set any time functions
if (minCounter > oldMinCounter) {
lcd.clear();
}
lcd.setCursor(0, 0);
printHMS(hour(), minute(), second());
lcd.setCursor(0, 1);
lcd.print(ledVal[0]);
lcd.setCursor(4, 1);
lcd.print(ledVal[1]);
lcd.setCursor(8, 1);
lcd.print(ledVal[2]);
}
int LED::read_encoder()
{
static int enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static int old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
int LED::setLed(int mins, // current time in minutes
int ledPin, // pin for this channel of LEDs
int start, // start time for this channel of LEDs
int period, // photoperiod for this channel of LEDs
int fade, // fade duration for this channel of LEDs
int ledMax, // max value for this channel
bool inverted // true if the channel is inverted
) {
int val = 0;
//fade up
if (mins > start || mins <= start + fade) {
val = map(mins - start, 0, fade, 0, ledMax);
}
//fade down
if (mins > start + period - fade && mins <= start + period) {
val = map(mins - (start + period - fade), 0, fade, ledMax, 0);
}
//off or post-midnight run.
if (mins <= start || mins > start + period) {
if ((start + period) % 1440 < start && (start + period) % 1440 > mins )
{
val = map((start + period - mins) % 1440, 0, fade, 0, ledMax);
}
else
val = 0;
}
if (val > ledMax) {
val = ledMax;
}
if (val < 0) {
val = 0;
}
if (inverted) {
pwmWrite(ledPin, map(val, 0, 100, 255, 0));
}
else {
pwmWrite(ledPin, map(val, 0, 100, 0, 255));
}
return val;
}
void LED::printMins(int mins, //time in minutes to print
bool ampm //print am/pm?
) {
int hr = (mins % 1440) / 60;
int mn = mins % 60;
if (hr < 10) {
lcd.print(" ");
}
lcd.print(hr);
lcd.print(":");
if (mn < 10) {
lcd.print("0");
}
lcd.print(mn);
}
void LED::printHMS (byte hr,
byte mn,
byte sec //time to print
)
{
if (hr < 10) {
lcd.print(" ");
}
lcd.print(hr, DEC);
lcd.print(":");
if (mn < 10) {
lcd.print("0");
}
lcd.print(mn, DEC);
lcd.print(":");
if (sec < 10) {
lcd.print("0");
}
lcd.print(sec, DEC);
}
//EEPROM write functions
long LED::EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void LED::EEPROMWritelong(int address, long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
void LED::setAllLed(){
int j=0;
for (int i=0;i<17;i=i+4){
int a=i;int b=i+1;int c=i+2;int d=i+3;
ledVal[j] = setLed(minCounter, ledPins[j], variablesList[a], variablesList[b], variablesList[c], variablesList[d], invertedLEDs[j]);
j++;
}
}
void LED::setOneLed(int channel){
int j=channel;
int i=0;
if(channel==1){i+=4;}
if(channel==2){i+=8;}
if(channel==3){i+=12;}
if(channel==4){i+=16;}
int a=i;int b=i+1;int c=i+2;int d=i+3;
ledVal[j] = setLed(minCounter, ledPins[j], variablesList[a], variablesList[b], variablesList[c], variablesList[d], invertedLEDs[j]);
}
void LED::rotateCheck(int& menuCount, int minMenu, int maxMenu){
while (menuCount!=0){
int rotateCount;
rotateCount=read_encoder();
if (rotateCount) {
menuCount+=rotateCount;
if (menuCount<minMenu){menuCount==maxMenu;}
if (menuCount>maxMenu){menuCount==minMenu;}
clearLCD();
}
}
}
void LED::menuWizard(){
int menuCount=1;
String menuList[6]={"Time","LED Max","LED Start","LED End","Fade Length","Ch Override"};
String channelList[5]={"1","2","3","4","5"};
while (menuCount!=0){
rotateCheck(menuCount,1,6);
lcd.setCursor(0, 0);
lcd.print(menuList[menuCount-1]);
clearLCD();
if (goBack.isPressed()){
menuCount=0;
}
if (pressSelect() && menuCount!=0){
int timeMode=1;
int channelCount=0;
bool goBack=0;
while (goBack!=1){
if (menuCount==1){
if (pressSelect()){
timeMode++;
if (timeMode>2){timeMode=1;}
}
int timeAdjDetect=read_encoder();
if (timeMode==1){
if (timeAdjDetect){
if (timeAdjDetect>0){adjustTime(SECS_PER_HOUR);}
if (timeAdjDetect<0) {adjustTime(-SECS_PER_HOUR);}
}
lcd.setCursor(0, 0);
lcd.print("Set Time: Hrs");
lcd.setCursor(0, 1);
printHMS(hour(), minute(), second());
}
else{
if (timeAdjDetect){
if (timeAdjDetect>0){adjustTime(SECS_PER_MIN);}
if (timeAdjDetect<0) {adjustTime(-SECS_PER_MIN);}
}
lcd.setCursor(0, 0);
lcd.print("Set Time: Mins");
lcd.setCursor(0, 1);
printHMS(hour(), minute(), second());
}
clearLCD();
}
else{
rotateCheck(channelCount,0,4);
lcd.setCursor(0,0);
lcd.print("Select Channel");
lcd.setCursor(0,1);
lcd.print(channelList[channelCount]);
clearLCD();
if (pressSelect()){
if (menuCount==2){
subMenuWizard(2,channelCount,0,0);
}
if (menuCount==3){
subMenuWizard(0,channelCount,1,0);
}
if (menuCount==4){
subMenuWizard(1,channelCount,1,1);
}
if (menuCount==5){
subMenuWizard(3,channelCount,1,0);
}
}
}
if (pressBack()){goBack=1;}
}
}
}
for (int i=0;i<20;i++){
int j=0;
EEPROMWritelong(j, variablesList[i]);
j+=4;
}
}
int LED::subMenuWizard(int i, int channel, bool time, bool truetime){
if (channel==1){i=i+4;}
if (channel==2){i=i+8;}
if (channel==3){i=i+12;}
if (channel==4){i=i+16;}
while (!pressBack()){
if (time==0){
rotateCheck(variablesList[i],0,100);
lcd.setCursor(0,0);
lcd.print("Set:");
lcd.setCursor(0,1);
lcd.print(variablesList[i]);
setOneLed(channel);
clearLCD();
}
else{
if (truetime){
rotateCheck(variablesList[i],0,1439);
lcd.setCursor(0,0);
lcd.print("Set:");
lcd.setCursor(0,1);
printMins(variablesList[i] + variablesList[i-1], true);
clearLCD();
}
else {
rotateCheck(variablesList[i],0,1439);
lcd.setCursor(0,0);
lcd.print("Set:");
lcd.setCursor(0,1);
printMins(variablesList[i], true);
clearLCD();
}
setOneLed(channel);
}
}
}
and finally, the .ino file:
#define LCD_BACKLIGHT 9 // backlight pin
#define BACKLIGHT_DIM 10 // PWM value for backlight at idle
#define BACKLIGHT_ON 70 // PWM value for backlight when on
#define BACKLIGHT_IDLE_MS 10000 // Backlight idle delay
#include <LED.h>
//Initialize buttons
int buttonCount = 1;
LED main;
void setup() {
};
void loop() {
/* main.setAllLed();
//turn the backlight off and reset the menu if the idle time has elapsed
if (main.backlightIdleMs + BACKLIGHT_IDLE_MS < millis() && main.backlightIdleMs > 0 ) {
analogWrite(LCD_BACKLIGHT, BACKLIGHT_DIM);
main.clearLCD();
main.backlightIdleMs = 0;
}
if (buttonCount == 1) {
main.displayMainMenu();
}
if (buttonCount == 2) {
main.menuWizard();
buttonCount = 1;
}
*/
main.printLCD();
};
Also, in the loop portion, I've commented the part of code that is intended to run, and I'm running a function that tests to see if I've successfully entered the loop by printing "test" on screen.
I'm using a Mega for this.
LED::LED()
{
InitTimersSafe();
pinMode(LCD_BACKLIGHT, OUTPUT);
lcd.begin(16, 2);
digitalWrite(LCD_BACKLIGHT, HIGH);
lcd.print("sEx LED, V1");
clearLCD();
delay(5000);
analogWrite(LCD_BACKLIGHT, BACKLIGHT_DIM);
You have to understand that this constructor is running when the object is created and that is probably before init() is run from main. So the hardware isn't ready at that point and pinMode and digitalWrite and stuff isn't going to work. The lcd code can't really work there and I bet that is the part that is hanging things.
A constructor should only do things like initialize variables. Any code that relies on the hardware should go into a begin() or init() or whatever method that you can call from setup once it is safe to do those things. The Serial object is a great example of another class that has to do this.

Arduino-4X4 matrix keypad with I/O shift register

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}

Unexpected behavior in my RGB-strip driver code

I'm getting wrong output on the pins 9, 10 and 11. They are meant to be inputs for my RGB strip driver circuit. Which is basically an array of NPN transistors pulling current from the strip.
The basic idea is to get only 2 controls to set R, G, B and brightness. I'm using a button and a potenciometer. Potenciometer is used to set the value and the button to skip to next value setting. There is one setting state which is like the default one. It is the one to set the brightness and I will be using this most of the time. The othe ones are for setting the colors and when in one of those setting the strip will be blinking in between only the color I'm currently setting and the result with alle three colors set. The whole code was working just fine until I added the brightness setting and I think that I am kinda lost in my own code.
I even added a serial to read the outputs but I don't understand why are the numbers the way they are :(
int pinR = 9;
int pinG = 10;
int pinB = 11;
int potPin = A0;
const int buttonPin = 2;
int brightR = 0;
int brightG = 0;
int brightB = 0;
int brightness = 50; //
int R;
int G;
int B;
int potValue = 0;
int blinky = 0;
boolean blinking = false;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
int setting = 0; //0=R 1=G 2=B 3=Brightness
void setup() {
// put your setup code here, to run once:
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
Serial.begin(9600);
}
void RGBset(int r, int g, int b){
analogWrite(pinR, r);
analogWrite(pinG, g);
analogWrite(pinB, b);
}
void loop() {
// put your main code here, to run repeatedly:
potValue = analogRead(potPin);
potValue = map(potValue, 0, 1023, 0, 255); //read pot --> map to values from 0 - 255
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
setting++;
}
}
}
lastButtonState = reading;
if(setting > 3){ // 0=R 1=G 2=B 3=Brightness
setting = 0; // cant get 4 cause there is no 4
}
if(setting == 0){
brightR = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(brightR, 0, 0);
}
}
if(setting == 1){
brightG = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(0, brightG, 0);
}
}
if(setting == 2){
brightB = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(0, 0, brightB);
}
}
if(setting == 3){
brightness = potValue;
brightness = map(brightness, 0, 255, 1, 100); //mapping brightness to values from 1 - 100
R = brightR * brightness / 100; //set value * brightness / 100
G = brightG * brightness / 100; //that leads to get % of set value
B = brightB * brightness / 100; //255 * 50 / 100 = 127,5 ==> 128
RGBset(R, G, B); //it wont blink in thiss setting
}
if(setting != 3){
blinky++;
if(blinky > 1000){
blinking = !blinking;
blinky = 0;
}
}
String output = (String(brightR) + " " + String(R) + " " + String(brightG) + " " + String(G) + " " + String(brightB) + " " + String(B) + " " + String(brightness) + " " + String(potValue) + " " + String(blinking));
Serial.println(output);
delay(1);
}
First, in setup:
pinMode(buttonPin , INPUT);
Second, what are you expected when setting==3? You aren't reloading/updating the variables for brightR brightG brightB. So, when you change setting, you will lost the change of brightness

Resources