Why is this program not preforming as it should?

This program is supposed to…

  1. Make the RGB on the LCD flash through a variation of colors using the PWM pins.
  2. Display some straight forward text on the display.
  3. Read an input from a pot, and change an interval at which an LED flashes a pattern.
#include <LiquidCrystal.h>
#include <Wire.h>
 
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
// you can change the overall brightness by range 0 -> 255
int brightness = 255;
 
 
 
void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print("R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  digitalWrite(REDLITE, r);
  digitalWrite(GREENLITE, g);
  digitalWrite(BLUELITE, b);
}

// This 39kHz loop from http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino
/* Modulate pin at 39 kHz for give number of microseconds */
void on(int pin, int time) {
  static const int period = 25;
  // found wait_time by measuring with oscilloscope
  static const int wait_time = 9;  

  for (time = time/period; time > 0; time--) {
    digitalWrite(pin, HIGH);
    delayMicroseconds(wait_time);
    digitalWrite(pin, LOW);
    delayMicroseconds(wait_time);
  }
}

void cameraSnap(int pin)
{
  // These Timing are from: http://www.bigmike.it/ircontrol/
  on(pin,2000);
  //This Delay is broken into 3 lines because the delayMicroseconds() is only accurate to 16383. http://arduino.cc/en/Reference/DelayMicroseconds
  delayMicroseconds(7830);
  delayMicroseconds(10000);
  delayMicroseconds(10000);
  on(pin,390);
  delayMicroseconds(1580);
  on(pin,410);
  delayMicroseconds(3580);
  on(pin,400);
}

int CameraIrPin = 4;   // LED connected to digital pin 13
int configPin = 2;      // potentiometer to pin analogic 2
int selection = 0;      // inizializzo la variabile di attesa

#define LENARRAY 14 // number of element of array
int confArray[LENARRAY] = {0, 5, 10, 15, 20, 24, 25, 30, 35, 40, 45, 50, 55, 60}; // wait time in seconds
long previousMillis = 0; 

byte newChar[8] = {  //copyraght character
	B00000,
	B01110,
	B10001,
	B11111,
	B11001,
	B11111,
	B10001,
	B01110
};

void setup()  
{
  pinMode(CameraIrPin, OUTPUT); // config digital pin as output
  lcd.begin(16,2) ;  // inizializza LCD
  
  lcd.createChar(0, newChar);
  lcd.setCursor(0,1);
  lcd.print("By ...");
  lcd.write(byte(0));
  lcd.setCursor(0,0);
  lcd.print(" Intervalometer");
  delayMicroseconds(7000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  Designed to");
  lcd.setCursor(0,1);
  lcd.print(" NIKON D SERIES");
  delayMicroseconds(6000);
  
  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);
 
  brightness = 254;
  
}

void loop()     
{
  selection = analogRead(configPin); //read pot value (from 0 to 1023)
  selection = map(selection,0, 1023, 1, LENARRAY) ; // resize the value to other like array

  lcd.setCursor(0,0) ;
  lcd.print("INTERVAL:");
  lcd.print(confArray[selection]); //show the set of second
  lcd.print(" sec      "); // keep this space
  
  lcd.setCursor(0,1); 
  lcd.print("RELEASE:");
  unsigned long currentMillis = millis();
  lcd.print(confArray[selection] - ((currentMillis - previousMillis)/1000)); // countdown 
                                                                             // 
  lcd.print("       ");  // clear lcd
  
  // time control
  if((currentMillis - previousMillis) > (confArray[selection]*1000)) {
    previousMillis = currentMillis;   // i save the last shot
    cameraSnap(CameraIrPin);
      for (int i = 0; i < 255; i++) {
    setBacklight(i, 0, 255-i);
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(255-i, i, 0);
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(0, 255-i, i);
    delay(5);
  }
  }
}

It is currently…
-Not displaying text on the display.
-LCD comes up with a dull red, the balanced red, and does not change.
-Using a camera, we cannot see the IR light blinking the pattern it should be at the set time from the pot.

Thanks for any help, this is needed on Tuesday, so any type of suggestion to fix this would be great! :slight_smile:
If their are no problems in the program, however, I will continue to look over the hardware, as I am currently.

As it is homework, you can do the work, but here are some tips to get you started:

First things first, have a look at the last 3 lines of the setBacklight() function and tell me (and yourself) what is wrong with them :)

Second, if nothing is printing on the display, try uploading a simple sketch, such as on of the LiquidCrystal examples and see if that works. If it doesn't you likely have a hardware issue. If it does, go through the lcd.___() calls in your main sketch and see if you are doing anything differently.

[Also: Have a quick look in setup, do you really expect to be able to see text if it is cleared after 7 milliseconds]

I am checking the LCD, in which is an RGB display, do you ground the R G or B and it should light that color. Therefore, I have them hooked up to 3 PWM ports. In the program I just uploaded to check the LCD, the display lights up red and does not show any text, even though the program does not set any PWM ports but does send text. Any idea why, besides hardware issue?

  digitalWrite(REDLITE, r);
  digitalWrite(GREENLITE, g);
  digitalWrite(BLUELITE, b);

In what universe will this set anything PWM-wise on the LEDs?

Forget the backlight, I will ground it manually and possibly add toggles later. Help me get the text to show up on the LCD.

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

So what pins on the LCD are you wiring to these arduino pins?

Grumpy_Mike: LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

So what pins on the LCD are you wiring to these arduino pins?

Arduino 7-RS Arduino 8-EN Arduino 9-DB4 Arduino 10-DB5 Arduino 11-DB6 Arduino 12-DB7

Is RW on the LCD grounded?

Yes.

  selection = map(selection,0, 1023, 1, LENARRAY) ; // resize the value to other like array

You do realise that your array goes from 0 to 13, not 1 to 14, don't you?

Small thing. You can switch the IR led for a red one to test.
Red led puts out lots of IR, you can confirm on your camera view the red led looks white.

majenko:   selection = map(selection,0, 1023, 1, LENARRAY) ; // resize the value to other like array

You do realise that your array goes from 0 to 13, not 1 to 14, don't you?

When I change the number to 13, the compiler gives me an error.

FAIH9TOGSLF38FA:45: error: too many initializers for 'int [13]'

int x[ 10 ]; // has 10 ints.

x[0], x[1], x[2], ... x[9]

If I want the first it is x[0] and the last is x[9].

GoForSmoke:
int x[ 10 ]; // has 10 ints.

x[0], x[1], x[2], … x[9]

If I want the first it is x[0] and the last is x[9].

I’m not very good at C, and I don’t understand.

Here is what I have changed (updated version)

#include <LiquidCrystal.h>
#include <Wire.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
// you can change the overall brightness by range 0 -> 255
int brightness = 255;

// This 39kHz loop from http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino
/* Modulate pin at 39 kHz for give number of microseconds */
void on(int pin, int time) {
  static const int period = 25;
  // found wait_time by measuring with oscilloscope
  static const int wait_time = 9;  

  for (time = time/period; time > 0; time--) {
    digitalWrite(pin, HIGH);
    delayMicroseconds(wait_time);
    digitalWrite(pin, LOW);
    delayMicroseconds(wait_time);
  }
}

void cameraSnap(int pin)
{
  // These Timing are from: http://www.bigmike.it/ircontrol/
  on(pin,2000);
  //This Delay is broken into 3 lines because the delayMicroseconds() is only accurate to 16383. http://arduino.cc/en/Reference/DelayMicroseconds
  delayMicroseconds(7830);
  delayMicroseconds(10000);
  delayMicroseconds(10000);
  on(pin,390);
  delayMicroseconds(1580);
  on(pin,410);
  delayMicroseconds(3580);
  on(pin,400);
}

int CameraIrPin = 4;   // LED connected to digital pin 13
int configPin = 2;      // potentiometer to pin analogic 2
int selection = 0;      // inizializzo la variabile di attesa

#define LENARRAY 14 // number of element of array
int confArray[LENARRAY] = {0, 5, 10, 15, 20, 24, 25, 30, 35, 40, 45, 50, 55, 60}; // wait time in seconds
long previousMillis = 0; 

byte newChar[8] = {  //copyraght character
	B00000,
	B01110,
	B10001,
	B11111,
	B11001,
	B11111,
	B10001,
	B01110
};

void setup()  
{
  pinMode(CameraIrPin, OUTPUT); // config digital pin as output
  lcd.begin(16,2) ;  // inizializza LCD
  
  lcd.createChar(0, newChar);
  lcd.setCursor(0,1);
  lcd.print("By ...");
  lcd.write(byte(0));
  lcd.setCursor(0,0);
  lcd.print(" Intervalometer");
  delayMicroseconds(7000000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  Designed to");
  lcd.setCursor(0,1);
  lcd.print(" NIKON D SERIES");
  delayMicroseconds(6000000);

  brightness = 254;
  
}

void loop()     
{
  selection = analogRead(configPin); //read pot value (from 0 to 1023)
  selection = map(selection,0, 1023, 1, LENARRAY) ; // resize the value to other like array

  lcd.setCursor(0,0) ;
  lcd.print("INTERVAL:");
  lcd.print(confArray[selection]); //show the set of second
  lcd.print(" sec      "); // keep this space
  
  lcd.setCursor(0,1); 
  lcd.print("RELEASE:");
  unsigned long currentMillis = millis();
  lcd.print(confArray[selection] - ((currentMillis - previousMillis)/1000)); // countdown 
                                                                             // 
  lcd.print("       ");  // clear lcd
  
  // time control
  if((currentMillis - previousMillis) > (confArray[selection]*1000)) {
    previousMillis = currentMillis;   // i save the last shot
    cameraSnap(CameraIrPin);
      for (int i = 0; i < 255; i++) {
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    delay(5);
  }
  }
}

What else do I need to do?

It has been mentioned before but just load in the hello world sketch in the IDE, change the pins to the ones you use. If that doesn't work then it is your hardware. Only then is it worth looking at the software.

      for (int i = 0; i < 255; i++) {
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    delay(5);
  }
  for (int i = 0; i < 255; i++) {
    delay(5);
  }

What’s all that about?

I know, I know. The microcontroller circuit I had built did not work, and I decided to just go pick up another arduino uno r3 board. I'm going to plug into this board and hopefully it will work!

AWOL, it was part of the backlight, I'll remove it.

Beatz1337:

GoForSmoke: int x[ 10 ]; // has 10 ints.

x[0], x[1], x[2], ... x[9]

If I want the first it is x[0] and the last is x[9].

I'm not very good at C, and I don't understand.

What else do I need to do?

Learn what is going on with arrays. Slow Down and Take The Time.

If I have an array of 14 ints, they will be numbered from 0 to 13. That is 14 ints. We start counting from 0. We don't start with 1 unless we're doing something special.

Use paper and pencil to draw boxes or whatever you need and count the pieces because that is so basic you really need to understand.

There are basics you need to understand the way you need to know the alphabet before you start spelling words. But you want to write a book instead of learn ABC.

I do not currently have time to learn code, and am only responsible for hardware. The code I need help getting to work was not created by me.

So you say show me show me show me till it's done?