Hello to all,
I am currently working on a project for my CNC machine.
I am building a "keyboard" which will be used as an interface for sending shortcuts and macros on my PC G-code sender. My keyboard is controlled by Arduino ProMicro.
The function for buttons is tested and is working. Also, the LEDs are working, but the issue is when I want to be a little bit more "hipster".
I want that when press a certain button, the color is changed and blinks. I get it for the first one when the LEDs blink ORANGE. In my code is void test(). But when I add a second one void test2(), everything is messed up.
To explain a little bit of bacground.
When you press button C_Hold, i want that LED from 0-19 are blkinking ORANGE until you pres same button again.
When you press button C_Stop, I want that LEDs from 0-19 are blinking red, but only n cycles and then colors are return to IDLE setting
Orange function does not work anymore and red is not limited.
/*
*/
// --------------------------------------------------------------
// Library adding
// --------------------------------------------------------------
#include <Keypad.h>
#include <Keyboard.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <Encoder.h>
//Matrix rows and columns
const byte numRows = 5; //No. of rows
const byte numCols = 5; //No. of columns
// --------------------------------------------------------------
// Arduino Pins definition
// --------------------------------------------------------------
//Keyboard pins
byte rowPins[numRows] = {21, 20, 19, 18, 15}; //vrstice
byte colPins[numCols] = {6, 7, 8, 9, 10}; //stolpci
//LEDs pin and No. Definition
#define LED_PIN 14
#define NUM_LEDS 24
//Encoder pins definition
Encoder encoder2(3, 2);//Enkoder 1 - Jog enkoder
Encoder encoder1(5, 4);//Enkoder 2 - Feed enkoder
// --------------------------------------------------------------
// Keyboard Matrix
// --------------------------------------------------------------
//Keyboard matrix
char keyMap[numRows][numCols] = {
{'1', '2', '3', '4', '5'},
{'6', '7', '8', '9', '0'},
{'A', 'B', 'C', 'D', 'E'},
{'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O'}
};
//Keyboard initialization
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, numRows, numCols);
//Color definition
// Red, Grn, Blu
#define BLACK 0, 0, 0
#define CHARCOAL 50, 50, 50
#define DARK_GREY 140, 140, 140
#define GREY 185, 185, 185
#define LIGHT_GREY 250, 250, 250
#define WHITE 255, 255, 255
#define RED 255, 0, 0
#define PINK 255, 130, 208
#define GREEN 0, 255, 0
#define DARK_GREEN 0, 30, 0
#define OLIVE 30, 30, 1
#define BLUE 0, 0, 255
#define LIGHT_BLUE 55, 68, 85
#define NAVY 0, 0, 30
#define PURPLE 140, 0, 255
#define LAVENDER 218, 151, 255
#define ORANGE 255, 80, 0
#define CYAN 0, 255, 255
#define MAGENTA 255, 0, 255
#define YELLOW 255, 255, 0
// Definition of buttons "shortcut/macro"
char C_Start[] = { KEY_LEFT_CTRL, 'i', '\0'};
char C_Hold[] = { KEY_LEFT_CTRL, KEY_TAB, '\0'};
char C_Stop[] = { KEY_LEFT_CTRL, KEY_ESC, '\0'};
char S_Start[] = { KEY_LEFT_CTRL, 'u', '\0'};
char Ref_Home[] = { KEY_LEFT_CTRL, KEY_HOME, '\0'};
char Go_To_Zero[] = { KEY_LEFT_CTRL, KEY_DELETE, '\0'};
char Zero_X[] = { KEY_LEFT_CTRL, 'x', '\0'};
char Zero_Y[] = { KEY_LEFT_CTRL, 'y', '\0'};
char Zero_Z[] = { KEY_LEFT_CTRL, 'z', '\0'};
char Cnc_Res[] = { KEY_LEFT_CTRL, KEY_END, '\0'};
char Probe_Start[] = { KEY_LEFT_CTRL, 'p', '\0'};
char Step_Chose[] = { KEY_LEFT_CTRL, 'c', '\0'};
char X_Plus[] = { KEY_LEFT_CTRL, KEY_RIGHT_ARROW, '\0'};
char X_Minus[] = { KEY_LEFT_CTRL, KEY_LEFT_ARROW, '\0'};
char Y_Plus[] = { KEY_LEFT_CTRL, KEY_UP_ARROW, '\0'};
char Y_Minus[] = { KEY_LEFT_CTRL, KEY_DOWN_ARROW, '\0'};
char Z_Plus[] = { KEY_LEFT_CTRL, KEY_PAGE_UP, '\0'};
char Z_Minus[] = { KEY_LEFT_CTRL, KEY_PAGE_DOWN, '\0'};
char Free1[] = { KEY_LEFT_CTRL, 'a', '\0'};
char Free2[] = { KEY_LEFT_CTRL, 'j', '\0'};
char Jog_Reset[] = { KEY_LEFT_CTRL, 't', '\0'};
char Feed_Reset[] = { KEY_LEFT_CTRL, 'r', '\0'};
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int colorUpdate = 0;
//Variables
int stepCount =1; // Counter for jog feed distance (0.1mm, 1mm, 10mm, 100mm)
int buttonState = 0; // Initial Button state
int lastButtonState = 0; // Previous Button state
int ledBrightnes=150; //LED Brightness setup
int colorModeSet=0; // Color mode
long positionEncoder1 = -999; //encoderA LEFT position variable
long positionEncoder2 = -999; //encoderB RIGHT position variable
//LED blinking setup
const long periodsH[] = {100, 300, 100, 300, 100, 300, 1000, 0};
const long periodsS[] = {100, 500, 100, 500, 100, 500, 1000, 0};
byte index;
unsigned long startTime;
unsigned long currentTime;
bool ledState;
byte indexS;
unsigned long startTimeS;
unsigned long currentTimeS;
bool ledStateS;
int cycleCounter = 0;
void setup() {
Keyboard.begin(); //Keyboard Initialisation
Serial.begin(9600); //Serial monitor - optional
pixels.begin(); // This initializes the NeoPixel library.
StepChooseInitial(); // Calibration of keybard and g-code sender
}
void loop() {
mainKeyFunction();
setColorsMode0();
encoder1_Jog();
encoder2_Feed();
pixels.setBrightness(ledBrightnes);
}
// --------------------------------------------------------------
// Button function - Main function
// --------------------------------------------------------------
void mainKeyFunction(){
char key = keypad.getKey(); //Pressed button check
if (key != NO_KEY) { //Check if buton is pressed
switch(key) {
case '1': Keyboard.press(C_Start);
Keyboard.releaseAll();
delay(50);
break;
case '2': Keyboard.press(C_Hold);
Keyboard.releaseAll();
delay(50);
break;
case '3': Keyboard.press(C_Stop);
Keyboard.releaseAll();
delay(50);
break;
case '4': Keyboard.press(S_Start);
Keyboard.releaseAll();
delay(50);
break;
case '5': Keyboard.press(Ref_Home);
Keyboard.releaseAll();
delay(50);
break;
case '6': Keyboard.press(Go_To_Zero);
Keyboard.releaseAll();
delay(50);
break;
case '7': Keyboard.press(Zero_X);
Keyboard.releaseAll();
delay(50);
break;
case '8': Keyboard.press(Zero_Y);
Keyboard.releaseAll();
delay(50);
break;
case '9': Keyboard.press(Zero_Z);
Keyboard.releaseAll();
delay(50);
break;
case '0': Keyboard.press(Cnc_Res);
Keyboard.releaseAll();
delay(50);
break;
case 'A': Keyboard.press(Probe_Start);
Keyboard.releaseAll();
delay(50);
break;
case 'B': Keyboard.press(Step_Chose);
Keyboard.releaseAll();
delay(50);
break;
case 'C': Keyboard.press(X_Plus);
Keyboard.releaseAll();
delay(50);
break;
case 'D': Keyboard.press(X_Minus);
Keyboard.releaseAll();
delay(50);
break;
case 'E': Keyboard.press(Y_Plus);
Keyboard.releaseAll();
delay(50);
break;
case 'F': Keyboard.press(Y_Minus);
Keyboard.releaseAll();
delay(50);
break;
case 'G': Keyboard.press(Z_Plus);
Keyboard.releaseAll();
delay(50);
break;
case 'H': Keyboard.press(Z_Minus);
Keyboard.releaseAll();
delay(50);
break;
case 'I': Keyboard.press(Free1);
Keyboard.releaseAll();
delay(50);
break;
case 'J': Keyboard.press(Free2);
Keyboard.releaseAll();
delay(50);
break;
case 'K': Keyboard.press('L'); // Feed reset
Keyboard.releaseAll();
stepCount++;
if (colorModeSet==0 || colorModeSet==1){
colorModeSet=2;
}
delay(50);
break;
case 'L': Keyboard.press('R'); // Jog reset
Keyboard.releaseAll();
stepCount--;
if( colorModeSet==0){
colorModeSet=1;
}else {
colorModeSet=0;
}
delay(50);
break;
}
Serial.println(key); //Izpišemo vrednost tipke v serijski monitor
}
}
// --------------------------------------------------------------
// Color setup function
// --------------------------------------------------------------
//Loop for addressing colors on LED - IDLE
void setColorsMode0(){
switch(colorModeSet){
case 0: if (colorUpdate == 0){ // have the neopixels been updated?
pixels.setPixelColor(0, pixels.Color( GREEN ));
pixels.setPixelColor(1, pixels.Color( ORANGE ));
pixels.setPixelColor(2, pixels.Color( RED ));
pixels.setPixelColor(3, pixels.Color( GREEN ));
pixels.setPixelColor(4, pixels.Color( ORANGE ));
pixels.setPixelColor(5, pixels.Color( GREEN ));
pixels.setPixelColor(6, pixels.Color( BLUE ));
pixels.setPixelColor(7, pixels.Color( BLUE ));
pixels.setPixelColor(8, pixels.Color( BLUE ));
pixels.setPixelColor(9, pixels.Color( WHITE ));
pixels.setPixelColor(10, pixels.Color( BLUE ));
pixels.setPixelColor(11, pixels.Color( YELLOW ));
pixels.setPixelColor(12, pixels.Color( BLUE ));
pixels.setPixelColor(13, pixels.Color( BLUE ));
pixels.setPixelColor(14, pixels.Color( WHITE ));
pixels.setPixelColor(15, pixels.Color( WHITE ));
pixels.setPixelColor(16, pixels.Color( BLUE ));
pixels.setPixelColor(17, pixels.Color( BLUE ));
pixels.setPixelColor(18, pixels.Color( BLUE ));
pixels.setPixelColor(19, pixels.Color( WHITE ));
//Function for jog feed resolution step choose
char stepLit=stepCount;
switch(stepLit){
case 0: pixels.setPixelColor(20, pixels.Color( GREEN ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 1: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( GREEN ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 2: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( GREEN ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 3: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( GREEN ));
break;
}
pixels.show();
colorUpdate=0;
}
break;
case 1: if (colorUpdate == 0){
test();
colorUpdate=0;
}
break;
case 2: if (colorUpdate==0){
test2();
colorUpdate=0;
}
break;
}
}
// --------------------------------------------------------------
// Encoders Functions
// --------------------------------------------------------------
//Loop for reading Encoder1
void encoder1_Jog(){
long newPos = encoder1.read()/4; //When the encoder lands on a valley, this is an increment of 4.
// your encoder might be different (divide by 2) i dunno.
if (newPos != positionEncoder1 && newPos > positionEncoder1) {
positionEncoder1 = newPos;//CCW - minus JOG
Keyboard.press('L');
Keyboard.releaseAll(); }
if (newPos != positionEncoder1 && newPos < positionEncoder1) {
positionEncoder1 = newPos;//CW - plus JOG
Keyboard.press('b');
Keyboard.releaseAll(); }
}
//Loop for reading Encoder2
void encoder2_Feed(){
long newPos = encoder2.read()/4; //When the encoder lands on a valley, this is an increment of 4.
// your encoder might be different (divide by 2) i dunno.
if (newPos != positionEncoder2 && newPos > positionEncoder2) {
positionEncoder2 = newPos;//CCW - minus FEED
Keyboard.press('M');
Keyboard.releaseAll();
}
if (newPos != positionEncoder2 && newPos < positionEncoder2) {
positionEncoder2 = newPos;//CW - plus FEED
Keyboard.press('P');
Keyboard.releaseAll(); }
}
// --------------------------------------------------------------
// Jog and Feed resolution step function
// --------------------------------------------------------------
//Loop for initial calibration
void StepChooseInitial(){
if (stepCount !=0){
for (int i = 0; i < 3; i++) {
Keyboard.press('2');
Keyboard.releaseAll();
}
}
stepCount=0;
}
//Loop for
void Step_Choose(){
if (stepCount<4){
Keyboard.press('1');
Keyboard.releaseAll();
stepCount++;
}
if (stepCount==4){
for (int i = 0; i < 3; i++) {
Keyboard.press('2');
Keyboard.releaseAll();
}
stepCount=0;
}
}
void test(){
currentTime = millis();
if (currentTime - startTime >= periodsH[index]) {
if (ledState == false) {
for (int i = 0; i < 20; i++) {
pixels.setPixelColor(i, pixels.Color( BLACK ));
}
ledState = true;
} else {
for (int i = 0; i < 20; i++) {
pixels.setPixelColor(i, pixels.Color( ORANGE ));
}
ledState = false;
}
index = ++index % 8;
startTime = currentTime;
}
char stepLit=stepCount;
switch(stepLit){
case 0: pixels.setPixelColor(20, pixels.Color( GREEN ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 1: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( GREEN ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 2: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( GREEN ));
pixels.setPixelColor(23, pixels.Color( LIGHT_BLUE ));
break;
case 3: pixels.setPixelColor(20, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(21, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(22, pixels.Color( LIGHT_BLUE ));
pixels.setPixelColor(23, pixels.Color( GREEN ));
break;
}
pixels.show();
}
void test2(){
currentTimeS = millis();
if (currentTimeS - startTimeS >= periodsS[indexS]) {
if (ledStateS == false) {
for (int i = 0; i < 20; i++) {
pixels.setPixelColor(i, pixels.Color( BLACK ));
}
ledStateS = true;
} else {
for (int i = 0; i < 20; i++) {
pixels.setPixelColor(i, pixels.Color( RED ));
}
ledStateS = false;
}
indexS = ++indexS % 8;
startTimeS = currentTimeS;
pixels.show();
}
if (++cycleCounter == 2) { // Preverjanje števca ciklov
colorModeSet=0;
setColorsMode0();
}
pixels.show();
}
And my second question, how can I intergrate function that is checking if for exyample Free1 button is pressed + another one, that you change function of a button?
example: If you press and Hold Free1 button, and rotationg Encoder1 you have brightness change.
I have also ideas to add other functions to keyboard but I need to do this first.