Skip to content

Instantly share code, notes, and snippets.

@danaabs
Created October 28, 2015 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danaabs/e07c3e032c843f86a774 to your computer and use it in GitHub Desktop.
Save danaabs/e07c3e032c843f86a774 to your computer and use it in GitHub Desktop.
Arduino to p5/Neopixel
// Adafruit_NeoMatrix example for single NeoPixel Shield.
// Scrolls 'Howdy' across the matrix in a portrait (vertical) orientation.
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
String readString; // creates the string scrolling text will be held in
#define PIN 6
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// Example for NeoPixel Shield. In this application we'd like to use it
// as a 5x8 tall matrix, with the USB port positioned at the top of the
// Arduino. When held that way, the first pixel is at the top right, and
// lines are arranged in columns, progressive order. The shield uses
// 800 KHz (v2) pixels that expect GRB color data.
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255)
};
#define INLENGTH 16
#define INTERMINATOR 13
const int potInput = A0;
const char* city;
const char* oldCity;
int fill = matrix.Color(0,0,0);
void setup() {
Serial.begin(9600);
while (Serial.available() <= 0) {
Serial.println("hello"); // send a starting message
delay(300); // wait 1/3 second
}
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(40);
matrix.show();
matrix.setTextColor(matrix.Color(255, 0, 0));
Serial.setTimeout(10);
}
int x = matrix.width(); //holds the width of the matrix for counting colums
int pass = 0; //used to calculate the number of characters read from the serial input
void loop () {
matrix.setCursor(x, 0); //set the begining cursor position
matrix.fillScreen(fill);
//City Selector
//Potentiometer split into 5 different ranges of potValues
// for 5 different cities:
// 1) 0-204 = New York, USA,
// 2) 205-408 = Rio de Janeiro, Brasil,
// 3) 409-612 = Santiago, Chile,
// 4) 613-816 = Washington DC, USA,
// 5) 817-1023 = Rome, Italy,
int potValue = analogRead(potInput);
if (city != oldCity) {
Serial.println(city);
oldCity = city;
}
if (potValue <= 1023/25) {
city = "New York";
fill = matrix.Color(255,255,0);
matrix.setTextColor(matrix.Color(127.5, 127.5, 127.5));
matrix.show();
}
else if (potValue > 1023/25 && potValue <= ((1023/25)*2)) {
city = "Rio de Janeiro";
fill = matrix.Color(0, 255 , 0);
matrix.setTextColor(matrix.Color(255, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*2) && potValue <= ((1023/25)*3)) {
city = "Santiago";
fill = matrix.Color(0, 0, 255);
matrix.setTextColor(matrix.Color(255, 0, 0));
matrix.show();
}
else if (potValue > ((1023/25)*3) && potValue <= ((1023/25)*4)) {
city = "Washington DC";
fill = matrix.Color(255, 0 , 0);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*4) && potValue <= ((1023/25)*5)) {
city = "Rome";
fill = matrix.Color(0, 255 , 0);
matrix.setTextColor(matrix.Color(255, 0, 0));
matrix.show();
}
else if (potValue > ((1023/25)*5) && potValue <= ((1023/25)*6)) {
city = "Cairo";
fill = matrix.Color(255, 255 , 0);
matrix.setTextColor(matrix.Color(255, 110, 0));
matrix.show();
}
else if (potValue > ((1023/25)*6) && potValue <= ((1023/25)*7)) {
city = "Tokyo";
fill = matrix.Color(255, 255 , 255);
matrix.setTextColor(matrix.Color(255, 0, 0));
matrix.show();
}
else if (potValue > ((1023/25)*7) && potValue <= ((1023/25)*8)) {
city = "Cape Town";
fill = matrix.Color(255, 0 , 0);
matrix.setTextColor(matrix.Color(0, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*8) && potValue <= ((1023/25)*9)) {
city = "London";
fill = matrix.Color(255, 100 , 255);
matrix.setTextColor(matrix.Color(0, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*9) && potValue <= ((1023/25)*10)) {
city = "Mexico City";
fill = matrix.Color(255, 0 , 0);
matrix.setTextColor(matrix.Color(0, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*10) && potValue <= ((1023/25)*11)) {
city = "Delhi";
fill = matrix.Color(0, 255 , 0);
matrix.setTextColor(matrix.Color(255, 140, 0));
matrix.show();
}
else if (potValue > ((1023/25)*11) && potValue <= ((1023/25)*12)) {
city = "Moscow";
fill = matrix.Color(255, 0 , 175);
matrix.setTextColor(matrix.Color(0, 0, 255));
matrix.show();
}
else if (potValue > ((1023/25)*12) && potValue <= ((1023/25)*12)) {
city = "Lagos";
fill = matrix.Color(0, 255 , 0);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*13) && potValue <= ((1023/25)*14)) {
city = "Seoul";
fill = matrix.Color(255, 0 , 0);
matrix.setTextColor(matrix.Color(0, 0, 255));
matrix.show();
}
else if (potValue > ((1023/25)*14) && potValue <= ((1023/25)*15)) {
city = "Beijing";
fill = matrix.Color(255, 0 , 0);
matrix.setTextColor(matrix.Color(255, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*15) && potValue <= ((1023/25)*16)) {
city = "Paris";
fill = matrix.Color(255, 255 , 255);
matrix.setTextColor(matrix.Color(0, 0, 255));
matrix.show();
}
else if (potValue > ((1023/25)*16) && potValue <= ((1023/25)*17)) {
city = "Buenos Aires";
fill = matrix.Color(0, 255 , 255);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*17) && potValue <= ((1023/25)*18)) {
city = "Tegucigalpa";
fill = matrix.Color(0, 0 , 255);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*18) && potValue <= ((1023/25)*19)) {
city = "Kigali";
fill = matrix.Color(255, 255 , 0);
matrix.setTextColor(matrix.Color(0, 200, 255));
matrix.show();
}
else if (potValue > ((1023/25)*19) && potValue <= ((1023/25)*20)) {
city = "Caracas";
fill = matrix.Color(100, 0 , 60);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*20) && potValue <= ((1023/25)*21)) {
city = "Barcelona";
fill = matrix.Color(0, 0 , 200);
matrix.setTextColor(matrix.Color(150, 0, 70));
matrix.show();
}
else if (potValue > ((1023/25)*21) && potValue <= ((1023/25)*22)) {
city = "Astana";
fill = matrix.Color(0, 255 , 255);
matrix.setTextColor(matrix.Color(255, 255, 0));
matrix.show();
}
else if (potValue > ((1023/25)*22) && potValue <= ((1023/25)*23)) {
city = "Amsterdam";
fill = matrix.Color(255, 140 , 0);
matrix.setTextColor(matrix.Color(255, 255, 255));
matrix.show();
}
else if (potValue > ((1023/25)*23) && potValue <= ((1023/25)*24)) {
city = "Berlin";
fill = matrix.Color(140, 0 , 0);
matrix.setTextColor(matrix.Color(255, 140, 0));
matrix.show();
}
else if (potValue > ((1023/25)*24) && potValue <= 1023) {
city = "Los Angeles";
fill = matrix.Color(255, 255 , 0);
matrix.setTextColor(matrix.Color(150, 0, 215));
matrix.show();
}
delay(1);
serialtochar();
chartoscreen();
} //end loop
void serialtochar()
{
// while (Serial.available()) //if serial data is available, run the loop
{
while (Serial.available() > 0) //if there is serial data continue
{
char c = Serial.read(); //read the next byte of data
readString += c; //append the data to the char variable readString
}
}
}
void chartoscreen()
{
// matrix.fillScreen(matrix.Color(0, 0 , 255));
if (readString.length() > 0) //if the number of bytes in readString are greater than 0
{
matrix.println(readString ); //print the data in readString
//Serial.println(city);
pass = -abs(readString.length()); //set pass to the negative value of the number of characters
if (--x < pass * 6) //reserve 6 spaces for each character - continue looping until x = pass * 6
{
x = matrix.width(); //set x to the number of colums in the matrix
readString = ""; //set the data to blank
//if(++pass >= 3) pass = 0;
// matrix.setTextColor(colors[pass]);
}
matrix.show(); //show the data stored
delay(75); //wait a bit - sets scrolling speed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment