Connecting hall sensor to photon board and testing a neopixel

photon
Tags: #<Tag:0x00007f1cae6218e0>

#1

Hello
I’m trying to connect a hall sensor to the photon board which would then act as a trigger for increasing the brightness of a neopixel/s. I have managed to connect the neopixel and set up a code to change its color using the IFTTT app. I now want to integrate a hall sensor into it which would then notch up the brigtness of the neopixel by a set amount as it detects a magnetic field. I am unsure as to how to combine the hardware as well as the code. I tried locating some code for hall sensors but couldnt find any for photon and I am not familiar with coding as much. Please help me!

Here are my two sets of codes, first one for setting the color and second one for the hall sensor.

#include "neopixel/neopixel.h"
 
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

// vars to split array
const unsigned int BUFFER_SIZE = 64; 
char paramBuf[BUFFER_SIZE];
 
// Init the LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
 

void setup() {
//   Particle.function("red", setRed);
//   Particle.function("green", setGreen);
//   Particle.function("blue", setBlue);
  Particle.function("setColor",setColor);
  Serial.begin(9600);
  strip.begin();
}
 
void loop() {
}
 
void setStripColor(int red, int green, int blue) {
  for (int i=0; i < PIXEL_COUNT; i++)
    strip.setColor(i, red, green, blue);
  strip.show();
}

// int setRed(String r) {
//   red = r.toInt();
//   setStripColor();
//   return red;
// }
// int setGreen(String g) {
//   green = g.toInt();
//   setStripColor();
//   return green;
// }
// int setBlue(String b) {
//   blue = b.toInt();
//   setStripColor();
//   return blue;
// }

int setColor(String paramStr) {
    // Convert String to char array.
    paramStr.toCharArray(paramBuf, BUFFER_SIZE);
    // Split string by the ',' delimiter. Expect three int.
    char *pch = strtok(paramBuf, ",");  // Create string tokenizer.
    int red = atoi(pch);  // Convert first token to int.
    
    pch = strtok (NULL, ",");
    int green = atoi(pch);
    
    pch = strtok (NULL, ",");
    int blue = atoi(pch);
    
    setStripColor(red, green, blue);
    return 1;
}
#include <neopixel.h>

//#include <Adafruit_NeoPixel.h>

//define neopixel number
#define NUM_LEDS 1

int waitTime=400; // delay before it turns into red again
 #define PIN D6 
#define PIXEL_TYPE WS2812//neopixel 
#define hallPin D0 
//output of hall sensor
 int hallState = 0; //initial state 
 
 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS,PIN,PIXEL_TYPE);
   
 
 
 void setup()
 {
  Serial.begin(115200);
  pinMode(hallPin, INPUT); //setting the hall sensor as the input for the neopixel
 
  strip.begin(); //initiates the  data going to the neopixel 
  strip.show();
 }
 
 void loop() //loop to detect the input from the sensor
 
 {
  hallState = digitalRead(hallPin);
  Serial.println(hallState); 
  
  
  if (hallState == 1 ) 
  {
    int i;
    uint32_t c;
    c= strip.Color(10,10,10);
    for(uint16_t i=0; i<strip.numPixels(); i++)
    {
      strip.setPixelColor(i,c);
    }
    strip.show();
  }
   
   
  // turn the LED on at a regular state
    //strip.setPixelColor(0, 10,10, 10); //dull white
  
  else {
   
//strip.setPixelColor(0, 255, 255, 255); //bright white


      int i;
    uint32_t c;
    c= strip.Color(255,255,255);
    for(uint16_t i=0; i<strip.numPixels(); i++)
    {
      strip.setPixelColor(i,c);
    }
    strip.show();
  }

  
  //strip.show();
  //delay(waitTime);
  // wait
 }

#2

In what way does the second code not do what you outlined above?

Apart from some style flaws of that code I’d assume this already should do what you want.

As for the wiring (and code) you can look at Arduino implementations and can take them almost one-to-one
e.g. this one

If your issue is not actually the Hall sensor nor the Neopixel stuff but rather how to control brighness you’d probably want to get yourself accustomed with different colour spaces.
While RGB is easy to comprehend and deal with as it maps one-to-one to the RGB design of Neopixels, it’s not the best for brightness control.
For that HSL would be a better approach.

There is a library that provides functions to translate a specific colour from one space to the other.
https://build.particle.io/libs/Color/1.2.1/tab/Color.h