Adding Controlled RGB Leds to RAMPS

The RAMPS 1.4 EFB Pin Connections

As the LCD is connected to to AUX-3 and AUX-4 we must use the Servos, AUX-1 or AUX-2 pins to connect the RGB led.   The unused pins D11, D6 and D5 from the Servos Header via appropriate resistors will then connect to the PWA via the J7  flat ribbon cable as shown below:

*The RGB on the front panel can also be connected to D0, D1 and D11.

In Configuration.h  ( line 2348) change the following lines to read

* LED Type. Enable only one of the following two options.
 */
#define RGB_LED
//#define RGBW_LED

#if EITHER(RGB_LED, RGBW_LED)
  #define RGB_LED_R_PIN D4
  #define RGB_LED_G_PIN D5
  #define RGB_LED_B_PIN D6
  //#define RGB_LED_W_PIN -1
#endif
 
plus 
 
/**
 * Printer Event LEDs
 *
 * During printing, the LEDs will reflect the printer status:
 *
 *  – Gradually change from blue to violet as the heated bed gets to target temp
 *  – Gradually change from violet to red as the hotend gets to temperature
 *  – Change to white to illuminate work surface
 *  – Change to green once print has finished
 *  – Turn off after the print has finished and the user has pushed a button
 */
#if ANY(BLINKM, RGB_LED, RGBW_LED, PCA9632, PCA9533, NEOPIXEL_LED)
  #define PRINTER_EVENT_LEDS
#endif

Testing the A4988 Stepper Motor Driver

The Arduino Mega 2560 sketch is used to test a single Driver using the code below:

/* Demo of A4988 micro-stepping motor driver
   Arduino Uno
   Arduino IDE 1.8.10

   Uses button debounce library  https://github.com/thomasfredericks/Bounce2

   Gadget Reboot
*/
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 22, en = 24, d4 = 26, d5 = 28, d6 = 30, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <Bounce2.h>

// stepper control outputs
#define motStepPin     2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           // White
#define motDirPin      3 // Yellow

#define motMS1Pin      4 // Green
#define motMS2Pin      5 // Blue
#define motMS3Pin      6 // Purple

// button inputs
#define buttonDir      7 // Brown
#define buttonRun      8 // Grey
#define buttonMS       9 // Purple

#define motMS1Led      10 // Green
#define motMS2Led      11 // Amber
#define motMS3Led      12 // Red

// speed control pot
#define speedPot       A8 // Purple

// create debounce instances for 3 buttons
Bounce dirIn   = Bounce();  // motor direction toggle
Bounce runIn   = Bounce();  // motor run for 200 steps
Bounce msIn    = Bounce();  // cycle through microstep options

// list of microstep config pin outputs in binary notation B[MS1][MS2][MS3]
byte microSteps[5] = {
  B000,  // full step
  B100,  // 1/2  step
  B010,  // 1/4  step
  B110,  // 1/8  step
  B111,  // 1/16 step
};

// start with full stepping:  microSteps[0] gives MS1=0 MS2=0 MS3=0
byte msMode = 0;

void setup() {
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Stepper Testing");
  lcd.setCursor(0,1);
  lcd.print("push run button");
  Serial.begin(9600);
  Serial.print("Stepper Testing");
  // assign actual input pins to debounce instances with a certain debounce time
  dirIn.attach(buttonDir, INPUT_PULLUP);
  dirIn.interval(10);
  runIn.attach(buttonRun, INPUT_PULLUP);
  runIn.interval(10);
  msIn.attach(buttonMS, INPUT_PULLUP);
  msIn.interval(10);

  // configure initial motor settings
  digitalWrite(motStepPin, LOW);
  digitalWrite(motDirPin, HIGH);
  digitalWrite(motMS1Pin, LOW);
  digitalWrite(motMS2Pin, LOW);
  digitalWrite(motMS3Pin, LOW);

  pinMode(motStepPin, OUTPUT);
  pinMode(motDirPin, OUTPUT);
  pinMode(motMS1Pin, OUTPUT);
  pinMode(motMS2Pin, OUTPUT);
  pinMode(motMS3Pin, OUTPUT);
  pinMode(motMS1Led, OUTPUT);
  pinMode(motMS2Led, OUTPUT);
  pinMode(motMS3Led, OUTPUT);

  for(int x = 3; x < 8; x++) {
    digitalWrite(motMS1Led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                      // wait for a second
    digitalWrite(motMS1Led, LOW);    // turn the LED off by making the voltage LOW
    delay(100);                      // wait for a second
    digitalWrite(motMS2Led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                      // wait for a second
    digitalWrite(motMS2Led, LOW);    // turn the LED off by making the voltage LOW
    delay(100);               
    digitalWrite(motMS3Led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                      // wait for a second
    digitalWrite(motMS3Led, LOW);    // turn the LED off by making the voltage LOW
    delay(100); 
  }                
 }void loop() {
  // update button debounce status
  dirIn.update();
  runIn.update();
  msIn.update();

  // if any buttons are pressed, perform actions
  // toggle motor direction output pin
  if (dirIn.fell()) {
    digitalWrite(motDirPin, !digitalRead(motDirPin));
  }

  // run motor for 200 steps
  if (runIn.fell()) {
    for (int i = 0; i < 200; i++) {
      // read pot and set a delay to control speed
      int motSpeed = map(analogRead(speedPot), 0, 1023, 5000, 200);
      Serial.println("Feed Rate");
      lcd.setCursor(0,1);
      lcd.print("Step Number  ");
      lcd.setCursor(13,1);
      lcd.print(i);
      lcd.setCursor(0,0);
      lcd.print("Feed Rate     ");
      lcd.setCursor(12,0);
      lcd.print(motSpeed);
      
      //Serial.print(i);
      // advance motor one step at the pot delay setting/speed
      digitalWrite(motStepPin, HIGH);
      delayMicroseconds(motSpeed);
      digitalWrite(motStepPin, LOW);
      delayMicroseconds(motSpeed);
    }
  }

  // cycle through micro step configuration options
  if (msIn.fell()) {
    // reset all leds
    digitalWrite(10, LOW); // Green
    digitalWrite(11, LOW); // Amber
    digitalWrite(12, LOW); // Red
    
    msMode++;
    if (msMode >= 5) msMode = 0;
    digitalWrite(motMS1Pin, bitRead(microSteps[msMode], 2));
     digitalWrite(motMS1Led, bitRead(microSteps[msMode], 2));
    digitalWrite(motMS2Pin, bitRead(microSteps[msMode], 1));
     digitalWrite(motMS2Led, bitRead(microSteps[msMode], 1));
    digitalWrite(motMS3Pin, bitRead(microSteps[msMode], 0));
     digitalWrite(motMS3Led, bitRead(microSteps[msMode], 0));
  }
}
The Wiring is show below but does not include the 3 MS Leds