Classic code on express board

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
catcoffee
 
Posts: 26
Joined: Thu Jun 04, 2015 12:31 pm

Classic code on express board

Post by catcoffee »

Hi, I have some arduino code that's running fine on a CircuitPlayground Classic but it's not working when I upload it onto an Express board. Are there compatibility issues? Is there a different library I should include than Adafruit_CircuitPlayground.h?

Thanks much. :)

User avatar
catcoffee
 
Posts: 26
Joined: Thu Jun 04, 2015 12:31 pm

Re: Classic code on express board

Post by catcoffee »

The code in question:

Code: Select all

/*

#include <Adafruit_NeoPixel.h>
#include <Adafruit_CircuitPlayground.h>

int NUM_LEDS = 10;
int LED_PIN = 17;        //Circuit PlayGround Pin 17
int sensorPin = 12;      //Circuit PlayGround Pin 12

bool leftButtonPressed;
bool rightButtonPressed;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

const int HIGH_STRIKE_LIKELIHOOD = 4;   //The magic number is 2 to make a strike happen
int LOW_STRIKE_LIKELIHOOD = 10;   //Get low likelihood number from sensor, INCREASE this number to make strikes LESS likely
int currentDataPoint = 0;
int chance = HIGH_STRIKE_LIKELIHOOD;

// Simple moving average plot
int NUM_Y_VALUES = 17;

float yValues[] = {
  0,
  7,
  10,
  9,
  7.1,
  7.5,
  7.4,
  12,
  15,
  10,
  0,
  3,
  3.5,
  4,
  1,
  7,
  1
};

float simple_moving_average_previous = 0;
float random_moving_average_previous = 0;

float (*functionPtrs[10])(); //the array of function pointers
int NUM_FUNCTIONS = 2;

void setup() {

  Serial.begin(9600);
  
  // Neopixel setup
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  // initializes the array of function pointers.
  functionPtrs[0] = simple_moving_average;
  functionPtrs[1] = random_moving_average;
}

void loop() {

  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

    if (chance > 20) {
      chance = 20;
    }

    if (chance < 3) {
      chance = 3;
    }

    if (leftButtonPressed) {
      Serial.print("UP");
      chance = chance + 1;
      CircuitPlayground.playTone(500, 100);
      Serial.print(chance);
    }
    else {
      Serial.println(chance);
    }
        
    if (rightButtonPressed) {
      //Serial.print("DOWN");
      chance = chance - 1;
      CircuitPlayground.playTone(500, 100);
      //Serial.print(chance);
    }
    else {
      Serial.println(chance);
    }

  if (random(chance) == 2) {
    int led = random(NUM_LEDS);
    for (int i = 0; i < 10; i++) {
      lightningStrike(random(NUM_LEDS));  //spread out lightnight among multiple LEDs.
    }
    // Once there's been one strike, I make it more likely that there will be a second.
    //chance = HIGH_STRIKE_LIKELIHOOD;
  //} else {
    //chance = LOW_STRIKE_LIKELIHOOD;
  }
  turnAllPixelsOff();
  delay(500);
}

void turnAllPixelsOff() {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, 0);
  }
  strip.show();
}

void lightningStrike(int pixel) {
  float brightness = callFunction(random(NUM_FUNCTIONS));
  float scaledWhite = abs(brightness*500);
  
  strip.setPixelColor(pixel, strip.Color(scaledWhite, scaledWhite, scaledWhite));
  strip.show();
  delay(random(5, 80));      // Read value from sensor to change length of possible delays between flashes
  currentDataPoint++;
  currentDataPoint = currentDataPoint%NUM_Y_VALUES;
}

float callFunction(int index) {
  return (*functionPtrs[index])(); //calls the function at the index of `index` in the array
}

// https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
float simple_moving_average() {
  uint32_t startingValue = currentDataPoint;
  uint32_t endingValue = (currentDataPoint+1)%NUM_Y_VALUES;
  float simple_moving_average_current = simple_moving_average_previous + 
                                  (yValues[startingValue])/NUM_Y_VALUES - 
                                  (yValues[endingValue])/NUM_Y_VALUES;

  simple_moving_average_previous = simple_moving_average_current;
  return simple_moving_average_current;
}


// Same as simple moving average, but with randomly-generated data points.
float random_moving_average() {
  float firstValue = random(1, 10);
  float secondValue = random(1, 10);
  float random_moving_average_current = random_moving_average_previous +
                                  firstValue/NUM_Y_VALUES -
                                  secondValue/NUM_Y_VALUES;
  random_moving_average_previous = random_moving_average_current;

  return random_moving_average_current;
}

User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: Classic code on express board

Post by jerryn »

comparing the Pinouts
https://learn.adafruit.com/adafruit-cir ... ss/pinouts
and
https://learn.adafruit.com/introducing- ... nd/pinouts
It tools like the Neopixels are on pin 8 for the CPX and 17 for the Classic.

Note: I do not represent Adafruit. Just trying to help.

Edited to add: there are also some notes about pin 12 (D0) for the CPX in the guide.

User avatar
catcoffee
 
Posts: 26
Joined: Thu Jun 04, 2015 12:31 pm

Re: Classic code on express board

Post by catcoffee »

Doh! Thanks so much for that, I had just assumed the pinouts were the same. Won't make that mistake twice.

Thanks again!

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”