Overview

Here we introduce several ways to enjoy GR-COTTON LED lighting.

Simply copy and paste the sample programs to the Web compiler and IDE for GR.


Preparation

You will need a GR-COTTON board and a USB cable (Micro B type).

Make sure the white jumper on the reverse side of GR-COTTON is moved to the “3V3 USB” side. If it is set to the BATT side, remove and reinsert it into the USB side.


Programming the LED to Blink

The following sample is extremely easy to follow, and only makes the blue LED blink.

The blue LED is allocated to pin 24 on the GR-COTTON board. It is output from pin 24 in pinMode, and turned ON and OFF with digitalWrite.

Pin 22 is red and pin 23 is green, so set the #define area to 22 or 23 to change LED colors.


#include <arduino.h>

#define LED 24 // blue led
void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
}



void loop() {
  digitalWrite(LED, LOW);
  delay(500);
  digitalWrite(LED, HIGH);
  delay(500);
}

Programming the LED to Flicker Like a Firefly

The sample shows how to make the red and green LEDs flicker like a firefly. The intensity of the LED light can be set by configuring area 0 - 255 using analogWrite; digitalWrite was used only to turn the LEDs ON and OFF.


#include <arduino.h>

#define LED_R 22 // red led
#define LED_G 23 // green led
void setup() {
}

void loop() {
  for(int i = 0; i < 255; i++){
    analogWrite(LED_R, i);
    analogWrite(LED_G, i);
    delay(5);
  }
  for(int i = 0; i < 255; i++){
    analogWrite(LED_R, 255 - i);
    analogWrite(LED_G, 255 - i);
    delay(5);
  }
}

Rainbow

The sample below shows how to make the LEDs flicker in a rainbow of colors.


#include <arduino.h>

void setup() {
}

void loop() {
  static double t = 0.0;
  analogWrite(22, int(255.0 * pow(0.5 - 0.49 * cos(t / 2.0), 2.0)));
  analogWrite(23, int(255.0 * pow(0.5 - 0.49 * cos(t / 3.0), 2.0)));
  analogWrite(24, int(255.0 * pow(0.5 - 0.49 * cos(t / 5.0), 2.0)));
  t = fmod(t + (2 * M_PI / 1000 * 10), 60.0 * M_PI);
  delay(1);
}