Go Down

Topic: [Beginner] Using Max/MSP to Control NeoPixels with Arduino  (Read 581 times) previous topic - next topic

drooplet

Hi, I recently acquired my first Arduino Uno board along with a 60 strip of NeoPixel LED lights. I'm modifying the "simple" example sketch to receive serial data from Max. It's writing to Arduino properly at 9600 baud rate, but I don't think the write(or read) function is set up properly. Probably best to explain by just posting my code. The max patch is just sending integers through serial data. I am just trying to test the connection out by using Serial.write to change the red value. When uploading the code the only thing that happens is that my strip starts lighting up at a lesser luminosity in red. The serial monitor just reports backwards question marks followed by an integer at a certain interval. Maybe it's the fact that I put the serial.write function in a for loop?(I barely know what that means).

Thanks so much. This is my first foray into Arduino and I have no idea what I'm doing





Code: [Select]
#include <Adafruit_NeoPixel.h>

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 250; // delay for half a second
int value = Serial.read()
            ;

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  Serial.begin(9600);

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {
  if (Serial.available() > 0) {

    value = Serial.read();
    Serial.println(value);
  }
  // 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 < NUMPIXELS; i++) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color (Serial.write(value), 0, 0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}

PaulS

Code: [Select]
int value = Serial.read()
            ;

Why
is
this
spread
over
more
than
one
line?

It is pointless to call Serial.read() before you call Serial.begin().

Code: [Select]
pixels.Color (Serial.write(value), 0, 0)
Why are you calling Serial.write() there? Do you KNOW what Serial.write() returns? If you did, you'd KNOW that that value is meaningless in defining a color.
The art of getting good answers lies in asking good questions.

Go Up