A sketch that worked and now it doesn't

Hi everyone! I need help!

I finally managed to finish a sketch (I opened a topic called "How to merge several sketches") and everything worked perfectly. I decided to put the circuit back on a matrix board but once completed nothing worked anymore. I thought I made the connections wrong, so I rebuilt the circuit on a breadboard identical to the previous one, but nothing works. I noticed that on Arduino, once the sketch has been loaded, the Rx LED flashes for a few seconds and then the L LED stays on.

I haven't changed anything in the sketch and I don't understand why it stopped working

I know it's very messed up, I'm still new to programming, but the fact is it worked the way I wanted and now it doesn't work anymore

I had made a video of the working prototype in which I also framed the code, which is the one below. I rewrote it the same, 3 times the one that worked in the video.

I tried to connect one sensor at a time

I tried to change the jumpers

I tried 2 breadboards

I tried 3 different arduinos

In this sketch all sensor are powered by arduino correctly

I tried to upload verification sketches for each sensor and everything works

I have tried uninstalling and reinstalling all libraries


#include <Adafruit_NeoPixel.h>
#include <RTClib.h>
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>
#include <easyRun.h> 

#include <LiquidCrystal_I2C.h>

DHT dht(11, DHT11);
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);

const byte modePin = 3; // wire : pin---btn---GND
button modeButton(modePin);
const uint32_t autoChangePeriod = 10000ul; // every 10s

enum : byte {LIGHT, SOIL, TEMPERATURE} mode = LIGHT;

#define LDR_PIN A0

#define USOIL A2

#define PIN 2   // input pin Neopixel is attached to

#define NUMPIXELS    24// number of neopixels in Ring

#define secondsSinceMidnight(h, m,s) (3600ul * (h) + 60ul * (m) + (s))

int light = 0;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

uint32_t dayStart = secondsSinceMidnight(6, 0, 0);  // 6:00:00 am
uint32_t dayEnd   = secondsSinceMidnight(22, 0, 0); // 10:00:00 pm

void dieHere(const char * errorMessage) {
  Serial.println(errorMessage);
  while (true) yield();
}

void selectMode() {
  static unsigned long lastChange = 0;
  easyRun();
  if (modeButton || (millis() - lastChange >= autoChangePeriod)) {
    
    // need to switch to next mode (and do other stuff if needed upon transition)
    
   for (int i = 0; i <NUMPIXELS; i ++) pixels.setPixelColor (i, pixels.Color (0,0,0));      // turn LEDs off
    pixels.show ();
    
    lastChange = millis ();
    switch (mode) {
      case LIGHT: mode = SOIL; break;
      case SOIL: mode = TEMPERATURE; break;
      case TEMPERATURE: mode = LIGHT; break;
    }
  }
}

void handleLight() {
  
  light =analogRead(LDR_PIN);
  Serial.println(light);
  pixels.begin();
  lcd.init();
  lcd.backlight();
  
    if (light < 700) { 
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(0,255,0)); 
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Luce OK");
         delay (100);
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Luce no OK");
          delay (100);
}
}

void handleSoil() {

  int a = analogRead(USOIL); //sensore umiditĂ 

  if (a < 600) { //condizione per far partire neopixel con umiditĂ  suolo
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(51,153,255)); 
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Acqua OK");
         delay(100);
         
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Acqua no OK");
          delay (100);

     }

}

void handleTemperature() {

  dht.begin();
  pixels.begin();
  
  int t = dht.readTemperature();
  
 if (t>=23  ) { 
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(153,51,255));  // violet
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Temp OK");
         delay (100);
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Temp no OK");
          delay (100);
}
}


void setup() {
  Serial.begin(115200); Serial.println();

  if (! rtc.begin()) dieHere("Couldn't find RTC");
  if (! rtc.isrunning()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // careful will be the time when you compiled the sketch
}

void loop () {
  selectMode ();
  DateTime now = rtc.now ();
  uint32_t sNow = secondsSinceMidnight (now.hour (), now.minute (), now.second ());
  if ((sNow >= dayStart) && (sNow <= dayEnd)) {
    switch (mode) {
      case LIGHT: handleLight (); break;
      case SOIL : handleSoil (); break;
      case TEMPERATURE: handleTemperature (); break;
    }
  }
}

So you have either a hardware or a software problem, possibly both. If you changed the hardware but left the code alone then you had a hardware problem but you have now rewritten the code which may complicate things

Have you still got the code that you say worked and wahat exactly does "it stopped working" mean ?

1 Like

Yes, the code that worked I copied from the video I was saying, this I meant by "rewritten".

Before, as soon as I loaded the sketch, the Neopixel strip immediately turned on, the mode text appeared on the LCD and the RX LED flashed very quickly.

Now when I connect the cable, all the sensors turn on and as soon as I load the sketch on the arduino, the RX LED flashes 1 time and then turns off and the L LED lights up.

Squares appear on the LCD and nothing more. Obviously the Neopixel strip remains off.

I have also noticed that sometimes, if I open the serial monitor, this "??" and nothing more appears.

Hi,
Do you have a DMM?

Tom... :grinning: :+1: :coffee: :australia:

Ok, I understand now what you mean by DMM hahah

Yes I have

Hi,

Start checking your circuit against your schematic, first check gnds and power supplies to each device.

Tom.. :grinning: :+1: :coffee: :australia:

I did it 2 times and everything works fine

Do you have a pot for adjusting the display contrast ? Often cause of the display squares

You could upload a few simple examples to just check each of the sensor/display items is working.
Then add print statements to your sketch and view on the serial monitor to check the sketch is doing what you think it should

Yes, the LCD display has a potentiometer to adjust the contrast but nothing changes.

I tried to upload sketches to test each sensor individually and everything works.

One thing I omitted to mention is that on the matrix board I had uploaded the sketch to an arduino nano, but it was broken.

Now I have put the right board in the IDE, Arduino Uno, but nothing changes

I also tried to insert Serial.prints but the only thing that appears are "??"

Hi,

Can you please post this code that gave you "??"?

Tom.... :grinning: :+1: :coffee: :australia:

The ?? Typically appears when the baud rate on the monitor doesn’t match that set in the sketch

I have an update.

This morning I tried to reconstruct the schematic on the breadboard for the hundredth time.

I checked that all the cables were connected correctly with the DMM and once the Arduino was connected to the USB, everything started as if by magic.

I did multiple tests, plugging and unplugging arduino and everything worked. So I decided to bring the circuit back to the matrix board. Once finished, nothing was working anymore, only the sensors turned on but nothing more (the Neopixel LED strip always remains off).

Now I have still reported everything on breadboard but I don't understand what happens

The obvious conclusion is that the circuit as built on the breadboard is different from the one you built on the matrix board

Start by checking that 5V and GND are present where needed on the matrix board version

That could be it, but the thing I don't explain is that yesterday the circuit I reported on the breadboard was wrong. I left it there all night, this morning I connected the cable to the arduino and everything worked.

Hi,
While your project is working, using your DMM and circuit diagram.

  1. Measure your power supply voltages as each of the hardware module connections.
  2. Measure the voltage at the analog input from the LDR.
  3. Check that you have power and ground to th3e LDR.

In other words construct a check list of vital points on your working model, to use as reference when troubleshooting.

Can you post image(s) of your matrix project?

Tom.. :grinning: :+1: :coffee: :australia:

Update after 10 days.

January 30

On the breadboard, everything works. I tried to test every single connection with a DMM and everything was perfect.

January 31

I decide to move all the electronics into a 3D printed box.
I connect all the cables together with their equals (VCC with VCC, GND with GND, SDA with SDA and SCL with SCL)
I plug in the power and nothing works more.

February 1

I report everything on breadboard. Connecting similar jumpers (as seen in the circuit photo at the first post)
But nothing works.
The serial monitor does not print anything.
The sensors turn on, like Arduino, but the sketch is not executed (the LED strip does not turn on)
I decide to change the Arduino Uno. Still nothing works.
I try to upload some sketches that I had made to test sensors, disconnecting and reconnecting every single component each time. Everything works fine.

February 2

The sketch I need is not working yet, I decide to change all the sensors and also all the jumpers once again.
Nothing works.

I leave everything connected and I dedicate myself to something else.

February 7

Back to work on the project. As soon as I connect the Arduino to the PC, the LED lights up and the serial monitor starts printing the photoristance values.

I am incredulous.

I do all the necessary tests and once I understand that everything was as it should be, I move it into the box.

I connect Arduino and ... usual mistake.

I don't know what to do anymore

When you move the project into the box how are you making the connections between the components ?

Whoever is supposed to be smiling and bringing you good luck and success is failing.

Sounds like it is time to choose a new hobby. :expressionless:

a7

The jumpers of the LDR, RTC, DHT11 and the BUTTON are soldered, connected to Arduino through normal jumpers.

All VCC, GND, SDA, SCL are connected together with terminal blocks.

LCD and GROUND HUMIDITY sensor are connected with normal jumpers.

I wish I could but it is a university project for which I have a deadline in 8 days

1 Like