Script using neopixels and buzzer through temperature

This script is supposed to activate a rainbow with neopixels and a sound with a buzzer using temperature as an input. All the three scripts work separately and this one is verified with no errors. When flashed, nothing happens.
Anyone know what the problem can be? Thank you.

    // This #include statement was automatically added by the Particle IDE.
    #include <Adafruit_DHT_Particle.h>

    // This #include statement was automatically added by the Particle IDE.
    #include <neopixel.h>

    /*
     * This is a minimal example, see extra-examples.cpp for a version
     * with more explantory documentation, example routines, how to
     * hook up your pixels and all of the pixel types that are supported.
     *
     */

    #include "Particle.h"
    #include "neopixel.h"
    // Declare some variables

    int timeCounter = 0;
    int publishCounter = 0;
    int publishTemp = 0;
    double tempC = 0;

    // Create a software timer to publish at an interval

    Timer publishTemperature(1500, publish);
    SYSTEM_MODE(AUTOMATIC);

    // IMPORTANT: Set pixel COUNT, PIN and TYPE
    #define PIXEL_PIN D6
    #define PIXEL_COUNT 12
    #define PIXEL_TYPE WS2812B

    Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

    // Prototypes for local build, ok to leave in for Build IDE
    void rainbow(uint8_t wait);
    uint32_t Wheel(byte WheelPos);

    void setup()
    {
      strip.begin();
      strip.show(); 
       // Make the A0 pin an INPUT
        pinMode(A0, INPUT);
        // Particle Cloud Subscribe to Event
        Particle.subscribe("PublishTemp", subscribeHandler);
        // Publish temperature reading
        publishTemperature.start();// Initialize all pixels to 'off'
    }

    void loop()
    {
         publishTemp = analogRead(A0);
        //Convert to Celcius
        tempC = (((publishTemp *3.3)/4095)-0.5)*100;
        
        //update Temp every .2 seconds
        delay(200);
        
      if (tempC < -20)
        {
      rainbow(20);
        }
      
      if (tempC > -65)
      {
        sound();  
      }
      
      if (tempC > -21)
      {
          rainbow(20);
            

      }

    }

    void rainbow(uint8_t wait) {
      uint16_t i, j;

      for(j=0; j<256; j++) {
        for(i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, Wheel((i+j) & 255));
        }
        strip.show();
        delay(wait);
      }
    }

    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    uint32_t Wheel(byte WheelPos) {
      if(WheelPos < 85) {
       return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
      } else if(WheelPos < 170) {
       WheelPos -= 85;
       return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
      } else {
       WheelPos -= 170;
       return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
      }
    }



    void sound (){
        analogWrite(D3, 200);
    }

    void publish(){
        
        // Publish something
        Particle.publish("PublishTemp", String(tempC));
        // Increment counter
        publishTemp;
    }


    // Handler function for event subscription

    void subscribeHandler(const char *event, const char *data){
        

    }

Hello,

@wgbartley, I know you have experience with neopixels, is this something that you are able to help with?

Kyle

Is this really the code you try to flash?

Because this line looks questionable :wink:

  void publish(){
        
        // Publish something
        Particle.publish("PublishTemp", String(tempC));
        // Increment counter
        publishTemp;  // <-- ????
    }

Also for analogRead() no pinMode() is required and should be omitted.
I can’t see any use of Adafruit_DHT_Particle, but if you had the lib might be blocking the code if the sensor didn’t respond.

A bit more exact please. It’s hardly ever the case that nothing happens inside the device.
What does the RGB LED do?
You can always add some Serial.print() statements to narrow down what does happen and what not - how far your code is running, which branches are executed with what values and which branches fail to be executed.

Thanks for helping.
I have several scripts that work for all those parts separately. There is one for temperature senor to get the temperature show as Celsius. One for neopixels to shine in rainbow colors and another one to make the sound buzzer to make a song. All these code worked when i try them separately and i have trouble when trying to combine them together.

TEMPERATURE SENSOR SCRIPT

// Declare some variables

int timeCounter = 0;
int publishCounter = 0;
int publishTemp = 0;
double tempC = 0;

// Create a software timer to publish at an interval

Timer publishTemperature(1500, publish);

void setup() {

// Make the A0 pin an INPUT
pinMode(A0, INPUT);
// Particle Cloud Subscribe to Event
Particle.subscribe("PublishTemp", subscribeHandler);
// Publish temperature reading
publishTemperature.start();
}

void loop() {

// Pin SetUp
publishTemp = analogRead(A0);
//Convert to Celcius
tempC = (((publishTemp *3.3)/4095)-0.5)*100;
//update Temp every .2 seconds
delay(200);


}

// The publish function is called every 1.5s

void publish(){

// Publish something
Particle.publish("PublishTemp", String(tempC));
// Increment counter
publishTemp;
}


// Handler function for event subscription

void subscribeHandler(const char *event, const char *data){


}

NEROPIXEL SCRIPT

#include "Particle.h"
#include "neopixel.h"

SYSTEM_MODE(AUTOMATIC);

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D6
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

// Prototypes for local build, ok to leave in for Build IDE
void rainbow(uint8_t wait);
uint32_t Wheel(byte WheelPos);

void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop()
{
  rainbow(10);
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

SOUND BUZZER SCRIPT

/*
 * Project DCIV-SP17-BUZZER
 * Description: Makes some noise with a piezo buzzer on pin D3
 * Author: Brett Ian Balogh
 * Date: March 20, 2017
 */

#define BUZZ_PIN 3

void setup() {

  pinMode(BUZZ_PIN, OUTPUT);

}


void loop() {

for(int i = 0; i < 5; i++){
  tone(BUZZ_PIN, i*500, 400);
  delay(500);
  }

}

When i combine them and flash it, i got a negative result in the Conner for temperature and the number gets lower if i touch the sensor. And even the result is in the range of if statement, the buzzer didn’t make any sound and the neopixel just stay on one color and didn’t do the rainbow effect.

The fist things you need to do are fix the problems pointed out by @ScruffR. You should delete the pinMode(A0, INPUT) line, because you’re not supposed to set that when you want to do analogRead (see the docs here). Second, the last line in the publish function is wrong,

void publish(){
// Publish something
Particle.publish("PublishTemp", String(tempC));
// Increment counter
publishTemp; // should be publishCounter++ ?
}

In you combined program, your if statements don’t make sense. You have an if (tempC < -20) and an if (tempC > -21); those two statements cover all possible temperatures and they both call the same function, so they’re useless (but they should be calling rainbow(20) all the time).

As far as getting a negative number for temperature, I can’t see any reason that would be different in the individual vs. combined code. Given your formula, you would get a negative number for any analogRead value of 620 or less, so perhaps there’s something different with your sensor hookup. Same for the value going down when you touch it; something must be wrong with the sensor wiring or the device itself (what device are you using?).

1 Like

Another point that stands against the mere combination of the three parts is that they are all containing blocking code preventing any “parallel” task from running.

e.g. while you sound the buzzer you won’t do any rainbows or measuring

1 Like