Programming RGB LED's with Arduino - arduino

we are trying to code an RGB LED strip that has digital input using the arduino UNO. Our code verifies, but when we upload we get a lot of "Invalid library found in ..."
We are just trying to upload sample code off of instructables, and this is what it looks like:
/* Arduino Tutorial - How to use an RGB LED Strip
Dev: Michalis Vasilakis // Date 3/6/2016 // Ver 1.0
Info: www.ardumotive.com */
//Library
#include <Adafruit_NeoPixel.h>
//Constants
const int dinPin = 4; // Din pin to Arduino pin 4
const int numOfLeds = 8; // Number of leds
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfLeds, dinPin, NEO_GRB + NEO_KHZ800);
// Color takes RGB values, from 0,0,0 up to 255,255,255
// e.g. White = (255,255,255), Red = (255,0,0);
int red = 255; //Value from 0(led-off) to 255().
int green = 00;
int blue = 0;
void setup() {
pixels.begin(); // Initializes the NeoPixel library
pixels.setBrightness(80); // Value from 0 to 100%
}
void loop() {
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for(int i=0;i<numOfLeds;i++){
pixels.setPixelColor(i, pixels.Color(red,green,blue));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(10); // Delay for a period of time to change the next led
}
}
Our LED's arent lighting up and we have updated all libraries, our IDE is version 1.8.1, and our board and still nothing. Does anyone know why?

Related

How to decrease the set temperature

I've been following this tutorial to create an Arduino PID Temperature Controller.
I can increase the set temperature by rotating the rotary encoder clockwise however I can't decrease the set temperature even when I rotate the encoder anticlockwise. Can someone help explain what I'm doing wrong :)
EDIT: the enconder works fine, I've tested it seperately but this code doesn't work. Thanks
The code is:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PIDController.h>
#include "max6675.h"
// Define Rotary Encoder Pins
#define CLK_PIN 3
#define DATA_PIN 4
#define SW_PIN 2
// MAX6675 Pins
#define thermoDO 8
#define thermoCS 9
#define thermoCLK 10
// Mosfet Pin
#define mosfet_pin 11
// Serial Enable
#define __DEBUG__
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
/*In this section we have defined the gain values for the
* proportional, integral, and derivative controller I have set
* the gain values with the help of trial and error methods.
*/
#define __Kp 30 // Proportional constant
#define __Ki 0.7 // Integral Constant
#define __Kd 200 // Derivative Constant
int clockPin; // Placeholder por pin status used by the rotary encoder
int clockPinState; // Placeholder por pin status used by the rotary encoder
int set_temperature = 1; // This set_temperature value will increas or decreas if when the rotarty encoder is turned
float temperature_value_c = 0.0; // stores temperature value
long debounce = 0; // Debounce delay
int encoder_btn_count = 0; // used to check encoder button press
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO); // Create an instance for the MAX6675 Sensor Called "thermocouple"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);// Create an instance for the SSD1306 128X64 OLED "display"
PIDController pid; // Create an instance of the PID controller class, called "pid"
void setup() {
#ifdef __DEBUG__
Serial.begin(9600);
#endif
pinMode(mosfet_pin, OUTPUT); // MOSFET output PIN
pinMode(CLK_PIN, INPUT); // Encoer Clock Pin
pinMode(DATA_PIN, INPUT); //Encoder Data Pin
pinMode(SW_PIN, INPUT_PULLUP);// Encoder SW Pin
pid.begin(); // initialize the PID instance
pid.setpoint(150); // The "goal" the PID controller tries to "reach"
pid.tune(__Kp, __Ki,__Kd); // Tune the PID, arguments: kP, kI, kD
pid.limit(0, 255); // Limit the PID output between 0 and 255, this is important to get rid of integral windup!
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
#ifdef __DEBUG__
Serial.println(F("SSD1306 allocation failed"));
#endif
for (;;); // Don't proceed, loop forever
}
//
display.setRotation(2); //Rotate the Display
display.display(); //Show initial display buffer contents on the screen -- the library initializes this with an Adafruit splash screen.
display.clearDisplay(); // Cleear the Display
display.setTextSize(2); // Set text Size
display.setTextColor(WHITE); // set LCD Colour
display.setCursor(48, 0); // Set Cursor Position
display.println("PID"); // Print the this Text
display.setCursor(0, 20); // Set Cursor Position
display.println("Temperatur"); // Print the this Text
display.setCursor(22, 40); // Set Cursor Position
display.println("Control"); // Print the this Text
display.display(); // Update the Display
delay(2000); // Delay of 200 ms
}
void set_temp()
{
if (encoder_btn_count == 2) // check if the button is pressed twice and its in temperature set mode.
{
display.clearDisplay(); // clear the display
display.setTextSize(2); // Set text Size
display.setCursor(16, 0); // set the diplay cursor
display.print("Set Temp."); // Print Set Temp. on the display
display.setCursor(45, 25); // set the cursor
display.print(set_temperature);// print the set temperature value on the display
display.display(); // Update the Display
}
}
void read_encoder() // In this function we read the encoder data and increment the counter if its rotaing clockwise and decrement the counter if its rotating counter clockwis
{
clockPin = digitalRead(CLK_PIN); // we read the clock pin of the rotary encoder
if (clockPin != clockPinState && clockPin == 1) { // if this condition is true then the encoder is rotaing counter clockwise and we decremetn the counter
if (digitalRead(DATA_PIN) != clockPin) set_temperature = set_temperature - 3; // decrmetn the counter.
else set_temperature = set_temperature + 3; // Encoder is rotating CW so increment
if (set_temperature < 1 )set_temperature = 1; // if the counter value is less than 1 the set it back to 1
if (set_temperature > 150 ) set_temperature = 150; //if the counter value is grater than 150 then set it back to 150
#ifdef __DEBUG__
Serial.println(set_temperature); // print the set temperature value on the serial monitor window
#endif
}
clockPinState = clockPin; // Remember last CLK_PIN state
if ( digitalRead(SW_PIN) == LOW) //If we detect LOW signal, button is pressed
{
if ( millis() - debounce > 80) { //debounce delay
encoder_btn_count++; // Increment the values
if (encoder_btn_count > 2) encoder_btn_count = 1;
#ifdef __DEBUG__
Serial.println(encoder_btn_count);
#endif
}
debounce = millis(); // update the time variable
}
}
void loop()
{
read_encoder(); //Call The Read Encoder Function
set_temp(); // Call the Set Temperature Function
if (encoder_btn_count == 1) // check if the button is pressed and its in Free Running Mode -- in this mode the arduino continiously updates the screen and adjusts the PWM output according to the temperature.
{
temperature_value_c = thermocouple.readCelsius(); // Read the Temperature using the readCelsius methode from MAX6675 Library.
int output = pid.compute(temperature_value_c); // Let the PID compute the value, returns the optimal output
analogWrite(mosfet_pin, output); // Write the output to the output pin
pid.setpoint(set_temperature); // Use the setpoint methode of the PID library to
display.clearDisplay(); // Clear the display
display.setTextSize(2); // Set text Size
display.setCursor(16, 0); // Set the Display Cursor
display.print("Cur Temp."); //Print to the Display
display.setCursor(45, 25);// Set the Display Cursor
display.print(temperature_value_c); // Print the Temperature value to the display in celcius
display.display(); // Update the Display
#ifdef __DEBUG__
Serial.print(temperature_value_c); // Print the Temperature value in *C on serial monitor
Serial.print(" "); // Print an Empty Space
Serial.println(output); // Print the Calculate Output value in the serial monitor.
#endif
delay(200); // Wait 200ms to update the OLED dispaly.
}
}
If you can increment the set temperature it means that the statement
(clockPin != clockPinState && clockPin == 1)
is true and:
(digitalRead(DATA_PIN) != clockPin)
is false. So if you rotate the enconder anticlockwise you should guarantee that
(clockPin != clockPinState && clockPin == 1)
is still true and
(digitalRead(DATA_PIN) != clockPin)
is true.
Perhaps this is a little bit obvious but you can start debugging there. Sorry if my english is not clear.

LCD doesn't work in a simulated enviroment

I'm using Tinkercad, and since it's my first time programming an LCD I just copied the procedure to connect the pins and make it work.
The thing is that it just lights up without displaying anything, I tried both wiring and unwiring the R/W pin but that doesn't work either, nothing will be displayed.
What did I miss? The other functions of the code works normally.
Image of the circuit:
This is the code:
#include <LiquidCrystal.h>
const int pin = 0; // analog pin
float celsius = 0, farhenheit =0; // temperature variables
float millivolts; //Millivolts from the sensor
int sensor;
const int G_LED = 13;
const int Y_LED = 12;
LiquidCrystal lcd(10, 9, 5, 4, 3, 2); // Building the LCD
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C="); // "C=", "F=" and "mV" should be printed
lcd.setCursor(0, 1); // on the LCD in a column
lcd.print("F=");
lcd.setCursor(0, 2);
lcd.print("mV=");
pinMode(G_LED, OUTPUT);
pinMode(Y_LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensor = analogRead(pin); // Reading the value from the LM35 sensor using the A0 ingress
millivolts = (sensor / 1023.0) * 5000; // Converting the value in a number that indicates the millivolts
celsius = ((sensor * 0.00488) - 0.5) / 0.01; // Celsius value (10 mV for each degree, 0°=500mV)
farhenheit = celsius * 1.8 + 32; // Fahrenheit value
lcd.setCursor(4, 2); // Set the cursor at the right of "mV="
lcd.print(millivolts); // Print the mV value
lcd.setCursor(4, 0); // Same here for °C and °F
lcd.print(celsius);
lcd.setCursor(4, 1);
Serial.print(farhenheit);
if (millivolts < 700) { // Green LED is on when the temperature is under or equal to 20°
// if (celsius < 20) { // Alternative
analogWrite(G_LED, 255);
analogWrite(Y_LED, 0); }
else {
analogWrite(G_LED, 0);
analogWrite(Y_LED, 255); // Yellow LED is on when the temperature is above of 20°C
}
delay(1000);
}
Fix - I could not find the error, but I suspect it was due to the strange layout of the connections. You also tried to set the cursor to line 3, but the LCD you were using did not have 3 lines, it was a 16x2 LCD.
What I Did - So what I did was I re-did the entire project, I linked up a new LCD, this time with a digital contrast so that it could be dynamically changed. I also made sure to include the sensor you used in your last project. Over-all the project is an Arduino controlling an LCD and outputting the temperature in Fahrenheit and millivolts.
Here is the project link (Tinkercad).
Code:
#include <LiquidCrystal.h>
// Adds the liquid crystal lib
int contrast = 40; // Set the contrast of the LCD
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); // Instantiate the LCD
float fahrenheit;
float millivolts;
void setup ()
{
analogWrite(6, contrast); // Wrjte the contrast to the LCD
lcd.begin(16, 2); // Init the LCD
}
void loop ()
{
int sensor = analogRead(0);
millivolts = (sensor/1023.0)*5000;
fahrenheit = (((sensor*0.00488)-0.5)/0.01)*1.8+32;
lcd.setCursor(0, 0);
lcd.print("Temp(F):");
lcd.setCursor(11, 0);
lcd.print(fahrenheit);
lcd.setCursor(0, 1);
lcd.print("Volts(mV):");
lcd.setCursor(12, 1);
lcd.print(millivolts);
}
Diagram

Data transferred via RF 433MHz between two Arduinos doesn't show up on TFT LCD1.8 without ttyUSB Serial connection

The Transmitter is working fine. But the receiver isn't working without a ttyUSB serial connection. I mean whenever I disconnected the PC from the receiver it's not showing the DATA it's getting from the Transmitter. I want it to work without the PC with just external power. What possibly went wrong?
It shows "Not Connected" when I add an external power source disconnecting the USB plug from PC
/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 3 (default 11)
*/
#include <VirtualWire.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
//char temperatureChar[4];
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen=VW_MAX_MESSAGE_LEN;
const int buzzer = 2; //buzzer to arduino pin 2
void setup()
{
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);
// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(1);
// write the text to the top left corner of the screen
TFTscreen.text("Temp :\n ", 5, 5);
// ste the font size very large for the loop
TFTscreen.setTextSize(2);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 2 as an output
// Initialize the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if(vw_get_message(buf, &buflen)) //non-blocking
{
// set the font color
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text((char *)buf, 20, 20);
// wait for a moment
delay(1000);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text((char *)buf, 20, 20);
tone(buzzer, 2000); // Send 1KHz sound signal...
delay(1000);
noTone(buzzer);
}
else {
// set the font color
TFTscreen.stroke(255, 255, 0);
TFTscreen.setTextSize(1);
// print the sensor value
TFTscreen.text("Not connected", 20, 20);
// wait for a moment
delay(500);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.setTextSize(1);
TFTscreen.text("Not connected", 20, 20);
}
}
Solved. The USB cable was actually working as an antenna (radio). When I disconnect it, it loses the connection.

Problems with interrupt arduino nano with NRF24le1

I am trying to build a wireless control of an LED Stripe.
For this I use 2 Arduino Nano's with a NRF24le1 chip to communicate with each other.
Arduino 1 serves as a controller / master and has 3 buttons and 1 fader for the choice of the respective light mode / blackout, as well as the to serve light intensity.
I would like the values ​​as 6 bytes (button1, button2, button3,
faderintensiät) and received on the 2nd Arduino.
Not constant values ​​are sent, only at change of values.
On the 2nd Nano should then be activated by interrupt flags, if an button press what and the current value from Fader Held so that the loop stays clear and there via the RGBW Led Stripe, ever after which flag is selected, can be iterated.
How can I attach the interrupt? On my Transmitter Adruino the stream is running
This is my code:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define LED_PIN 3
#define LED_COUNT 228
uint8_t BRIGHTNESS = 60;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
RF24 wirelessSPI(7, 8); // CE, CSN
#define PinIRQ 3 // Arduino Uno, Mega und Nano: Pin 3
#define IRQ 1
const byte address[6] = "00001";
boolean light_on = false;
boolean mode_a_active = false;
boolean mode_b_active = false;
boolean receivedMessage = false;
int interruptcounter = 0;
uint8_t settings[6] = {0, 0, 0, 0, 0, 0}; //Saving the incoming data
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
wirelessSPI.begin();
//wirelessSPI.setAutoAck(1); //new
//wirelessSPI.enableAckPayload(); //new
//wirelessSPI.maskIRQ(1,1,0); //new
wirelessSPI.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
wirelessSPI.openReadingPipe(0, address); //Setting the address at which we will receive the data
wirelessSPI.startListening(); //This sets the module as receiver
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
attachInterrupt(IRQ, incomingmessage, HIGH);
}
void loop()
{
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
if(receivedMessage == true){
Serial.println("messange on interupt received");
Serial.print("interruptcounter counts: ");
Serial.println(interruptcounter);
Serial.println();
Serial.print(settings[0]);
Serial.print(settings[1]);
Serial.print(settings[2]);
Serial.print(settings[3]);
Serial.print(settings[4]);
Serial.println(settings[5]);
receivedMessage = false;
}
}
void incomingmessage()
{
interruptcounter++;
while (wirelessSPI.available()) {
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
receivedMessage = true; // Variable setzen, dass eine neue Nachricht zur Auswertung bereit steht
}
}

iMac Apple remote ir decoding

I am trying to modify an Arduino sketch to use an old Apple remote IR transmitter. It works, and I have a list of the HEX codes for the various buttons. What is confusing me is that the sketch won't compile with the HEX codes included, but will do so if I convert them to DEC equivalent. And, the Serial outputs as defined in sketch lines 102 to 126 work, but the LEDs do not seem to perform as suggested. I don't know if it is tied to the HEX/DEC issue, or where to look. The code, as it now stands, is below. It includes comments referring to the remote's frequency , which I have not addressed. Thanks for helping me to understand this.
/*
This sketch uses Ken Shirriff's *awesome* IRremote library:
https://github.com/shirriff/Arduino-IRremote
Hardware setup:
* The output of an IR Receiver Diode (38 kHz demodulating
version) should be connected to the Arduino's pin 11.
* The IR Receiver diode should also be powered off the
Arduino's 5V and GND rails.
* A common cathode RGB LED is connected to Arduino's pins
5, 9, and 6 (red, green, and blue pins).
*/
#include <IRremote.h> // Include the IRremote library
/* Setup constants for SparkFun's IR Remote: */
#define NUM_BUTTONS 6 // The remote has 6 buttons
/* Define the IR remote button codes. We're only using the
least signinficant two bytes of these codes. Each one
should actually have 0x10EF in front of it. Find these codes
by running the IRrecvDump example sketch included with
the IRremote library.*/
const uint16_t BUTTON_PLUS = 2011254893; // i.e. 0x10EFD827
const uint16_t BUTTON_MINUS = 2011246701;
const uint16_t BUTTON_LEFT = 2011271277;
const uint16_t BUTTON_RIGHT = 2011258989;
const uint16_t BUTTON_MENU = 2011283565;
const uint16_t BUTTON_STARTSTOP = 2011275373;
//const uint16_t BUTTON_LEFT = 0x10EF;
//const uint16_t BUTTON_RIGHT = 0x807F;
//const uint16_t BUTTON_CIRCLE = 0x20DF;
/* Connect the output of the IR receiver diode to pin 11. */
int RECV_PIN = 11;
/* Initialize the irrecv part of the IRremote library */
IRrecv irrecv(RECV_PIN);
decode_results results; // This will store our IR received codes
uint16_t lastCode = 0; // This keeps track of the last code RX'd
/* Setup RGB LED pins: */
enum ledOrder // Make an enum to add some clarity in the code
{
RED, // 0
GREEN, // 1
BLUE // 2
};
const int rgbPins[3] = {5, 9, 6}; // Red, green, blue pins respectively
byte rgbValues[3] = {55, 23, 200}; // This keeps track of channel brightness
byte activeChannel = RED; // Start with RED as the active channel
boolean ledEnable = 1; // Start with the LED on.
void setup()
{
Serial.begin(9600); // Use serial to debug.
irrecv.enableIRIn(); // Start the receiver
/* Set up the RGB LED pins: */
for (int i=0; i<3; i++)
{
pinMode(rgbPins[i], OUTPUT);
analogWrite(rgbPins[i], rgbValues[i]);
}
}
// loop() constantly checks for any received IR codes. At the
// end it updates the RGB LED.
void loop()
{
if (irrecv.decode(&results))
{
/* read the RX'd IR into a 16-bit variable: */
uint16_t resultCode = (results.value & 65535); //0xFFFF
/* The remote will continue to spit out 0xFFFFFFFF if a
button is held down. If we get 0xFFFFFFF, let's just
assume the previously pressed button is being held down */
if (resultCode == 65535) //0xFFFF
resultCode = lastCode;
else
lastCode = resultCode;
// This switch statement checks the received IR code against
// all of the known codes. Each button press produces a
// serial output, and has an effect on the LED output.
switch (resultCode)
{
case BUTTON_PLUS:
Serial.println("+");
if (ledEnable) ledEnable = 0;
else ledEnable = 1; // Flip ledEnable
break;
case BUTTON_MINUS:
Serial.println("-");
activeChannel = RED;
break;
case BUTTON_LEFT:
Serial.println("<-");
activeChannel = GREEN;
break;
case BUTTON_RIGHT:
Serial.println("->");
activeChannel = BLUE;
break;
case BUTTON_MENU:
Serial.println("Menu");
rgbValues[activeChannel]++; // Increment brightness
break;
case BUTTON_STARTSTOP:
Serial.println("-> =");
rgbValues[activeChannel]--; // Decrement brightness
break;
// case BUTTON_LEFT:
// Serial.println("Left");
// rgbValues[activeChannel] = 0; // Min brightness (off)
// break;
// case BUTTON_RIGHT:
// Serial.println("Right");
// rgbValues[activeChannel] = 255; // Max brightness
// break;
// case BUTTON_CIRCLE:
// Serial.println("Circle");
// rgbValues[activeChannel] = 127; // Medium brightness
// break;
default:
Serial.print("Unrecognized code received: 0x");
Serial.println(results.value, HEX);
break;
}
irrecv.resume(); // Receive the next value
}
// Every time through the loop, update the RGB LEDs:
if (ledEnable)
{
for (int i=0; i<3; i++)
{
analogWrite(rgbPins[i], rgbValues[i]);
}
}
else
{
for (int i=0; i<3; i++)
{
analogWrite(rgbPins[i], 0);
}
}
}
If you say your code does not compile if you use hex notation for those numbers it would help to provide the actual code that does not compile, because the code you posted here compiles even if I enter hex numbers instead of decimals.
As gre_gor already pointed out in his comment you also have a problem with your values.
const uint16_t BUTTON_PLUS = 2011254893;
Here you're trying to store 2011254893 in a 16bit unsigned integer.
If all 16 bits are 1 you end up with 2^16 -1 which is 65535.
So that is the maximum number you can store in a variable of type uint16_t.
If you assign larger values to that variable you will cause a so called integer overflow. The actual value stored in your variable will be 2011244893 modulus 65536, which is 20589. That's not the value you were supposed to assign.
If you read the comments in that code carefully:
Define the IR remote button codes. We're only using the least
signinficant two bytes of these codes. Each one should actually
have 0x10EF in front of it.
Also read this on integer overflow and I guess it wouldn't hurt if you make your self familiar with data types in general.
https://en.wikipedia.org/wiki/Integer_overflow
http://www.cplusplus.com/articles/DE18T05o/

Resources