help! modifiying existing code

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dr_chops
 
Posts: 1
Joined: Fri Jan 14, 2022 10:44 pm

help! modifiying existing code

Post by dr_chops »

hi all

Ive built a little project from the thingverse page and i would like to know how to swap the segemented read out for a bigger dot matrix unit. Ive tried just swapping them but it doesn't work, which must mean there is something in the code i need to add or change but i don't even know where to begin. I'll add as much info as i can

cheers
BANNED

dot matrix info
https://www.jaycar.co.nz/medias/sys_mas ... alMain.pdf

this is the code i need to end up with

Code: Select all

// Include EEPROM memory library
#include <EEPROM.h>

// Include Alphanumeric LED Backpack Library
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

int indicatorPin = 13;
int btnPin = A3;
int potPin = A6;
int btnState; // Used to tell the system what state the button is currently in (after debounce work)
int btnLastState = LOW; // The last confirmed state the button was in
int currentPotRead; // To store pot position

// base alpha chars
int b1 = 45;
int b2 = 45;
int b3 = 45;
int b4 = 45;

int lastb1 = 45;
int lastb2 = 45;
int lastb3 = 45;
int lastb4 = 45;

// character sets for gears
int gearCharSetOne[7];
int gearCharSetTwo[7];

// get shift positions from memory
int memShiftPos[7]; // Gear array
int memAddress = 0; // Base memory address for reading and writing values
int memMaxAddress = 24; // Maximum memory address

// variables for gear loop
int gearLoopIndex = 0;
int gearLoopMaxIndex = 6;
int gearLoopCurrentSmallestIndex = 0;
int gearLastSmallestIndex = -1;
int gearLoopCurrentDifferenceCheck;

// programming controls
int allowProg = HIGH; // Allow programming
int curHold = 0; // Current count of the cycles while being held
int oppMode = 0; // Program state - changes when differnt parts of the programing sequence are entered
int progModeRefresh = LOW; // Used to determine if the particular program state has JUST been entered (kind of used for menu "debounce")

unsigned long btnLastDebounce = 0; // time in millis - millis count of last debounce measure
unsigned long btnDebounceDelay = 50; // time in millis - amount of delay for checking for debounce

unsigned long progDetect = 0; // time in millis - millis count
unsigned long progDelay = 2000; // time in millis - amount of delay

unsigned long dashBlinkDelay = 300; // time in millis - millis count
unsigned long dashBlinkLast = 0; // time in millis - amount of delay
int dashBlink = 0; // Used to detect dashes blinking on display for program mode

//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3.
void EEPROMWritelong(int address, long value){
      //Decomposition from a long to 4 bytes by using bitshift.
      //One = Most significant -> Four = Least significant byte
      byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

      //Write the 4 bytes into the eeprom memory.
      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
      }
      
long EEPROMReadlong(long address){
      //Read the 4 bytes from the eeprom memory.
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

      //Return the recomposed long by using bitshift.
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
      }

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  Serial.begin(9600);
  pinMode(indicatorPin, OUTPUT);
  pinMode(btnPin, INPUT);
  Serial.write("INIT");
  Serial.println();
  
  // ## PARK
  gearCharSetOne[0] = 80;
  gearCharSetTwo[0] = 75;
  
  // ## REV
  gearCharSetOne[1] = 82;
  gearCharSetTwo[1] = 86;
  
  // ## NEU
  gearCharSetOne[2] = 78;
  gearCharSetTwo[2] = 76;
  
  // ## OD
  gearCharSetOne[3] = 79;
  gearCharSetTwo[3] = 68;
  
  // ## DR
  gearCharSetOne[4] = 68;
  gearCharSetTwo[4] = 82;
  
  // ## L2
  gearCharSetOne[5] = 50;
  gearCharSetTwo[5] = 110;
  
  // ## L1
  gearCharSetOne[6] = 49;
  gearCharSetTwo[6] = 115;
  
  // Read values from memory
  memShiftPos[0] = EEPROMReadlong(0);
  memShiftPos[1] = EEPROMReadlong(4);
  memShiftPos[2] = EEPROMReadlong(8);
  memShiftPos[3] = EEPROMReadlong(12);
  memShiftPos[4] = EEPROMReadlong(16);
  memShiftPos[5] = EEPROMReadlong(20);
  memShiftPos[6] = EEPROMReadlong(24);

  alpha4.begin(0x70);  // pass in the address
  
  alpha4.writeDigitAscii(0, 45);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 45);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 92);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 47);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 124);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 124);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 47);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 92);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 45);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 45);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 92);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 47);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 124);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 124);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 47);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 92);
  alpha4.writeDisplay();
  delay(150);
  
  alpha4.writeDigitAscii(0, 45);
  alpha4.writeDigitAscii(1, 71);
  alpha4.writeDigitAscii(2, 77);
  alpha4.writeDigitAscii(3, 45);
  alpha4.writeDisplay();
  delay(150);

  alpha4.writeDigitAscii(0, b1);
  alpha4.writeDigitAscii(1, b2);
  alpha4.writeDigitAscii(2, b3);
  alpha4.writeDigitAscii(3, b4);
  alpha4.writeDisplay();

}

//char displaybuffer[4] = {' ', ' ', ' ', ' '};

void loop() {
  
  if(millis() > 9300 && oppMode == 0){
    allowProg = LOW;
  }
  else {
    allowProg = HIGH;
  }
  
  if(oppMode == 0){ // Only do shift display when in the base opp mode (i.e. - no programming is taking place)
    dashBlink = 0;
    b1 = 45;
    b4 = 45;
    // SHIFT DETECT ########-------------------------------------------------------------------------------#-#-#-
    digitalWrite(indicatorPin, LOW);
    // Display gear selection #-#-#-#-#-#-#-#-#-#-#-#-
    gearLoopIndex = 0;
    while(gearLoopIndex <= gearLoopMaxIndex){
      // Get current position of pot
      currentPotRead = analogRead(potPin);
      // Compare that position to this index
      gearLoopCurrentDifferenceCheck = abs(memShiftPos[gearLoopIndex] - currentPotRead);
      
      // See if the difference between this index and the current position
      // is smaller than any past checked indexes
      // If it is smaller, store this index as the smallest
      if(gearLoopCurrentDifferenceCheck < (abs(memShiftPos[gearLoopCurrentSmallestIndex] - currentPotRead))){
        gearLoopCurrentSmallestIndex = gearLoopIndex;
      }
      // Incriment counter
      gearLoopIndex = gearLoopIndex + 1;
    }
    if(gearLoopCurrentSmallestIndex != gearLastSmallestIndex){
      // Update character variables
      b2 = gearCharSetOne[gearLoopCurrentSmallestIndex];
      b3 = gearCharSetTwo[gearLoopCurrentSmallestIndex];     
      gearLastSmallestIndex = gearLoopCurrentSmallestIndex;
    }
    delay(100);
  } // END shift detect
  
  // PROGRAMMING ########-------------------------------------------------------------------------------#-#-#-
  if(allowProg == HIGH) { // We are withing the allowable programming time. Allow programming.
    // Clean up in case we were in a prog
    
    // Read button with debounce ---------------------------------------------------
    int btnRead = digitalRead(btnPin);
    
    if (btnRead != btnLastState) {
      btnLastDebounce = millis();
      progDetect = millis();
      Serial.write("Button State Change: ");
      Serial.write(btnRead);
      Serial.println();
    }
    
    if ((millis() - btnLastDebounce) > btnDebounceDelay) {
      // debounce time has elapsed. Current read is valid.
      // if the button state has changed:
      if (btnRead != btnState) {
        btnState = btnRead;
        Serial.write("Button State Saved");
        Serial.println();
        // Determine Program State and update if needed
        if(oppMode != 0){
          oppMode = 2;
          progModeRefresh = HIGH;
        }
      }
    }
    btnLastState = btnRead; // Save ucrrent read as last state for next loop
    // END Read button with debounce ---------------------------------------------------
  
    if(oppMode == 0){
      // Listen for entry into programing mode #-#-#-#-#-#-#-#-#-#-#-#-
      if(btnState == HIGH){
        digitalWrite(indicatorPin, HIGH);
        if ((millis() - progDetect) > progDelay) {
          // Program mode entered
          oppMode = 1;
          Serial.write("Entering Program Mode: ");
          Serial.print("progDetect adj: ");
          Serial.print(millis() - progDetect);
          Serial.print(" | ");
          Serial.print("progDelay: ");
          Serial.print(progDelay);
          Serial.println();
        }
      }
    } // END program entry detect
    if(oppMode != 0) { // Program Mode
      dashBlink = 1; // tell dashes to blink
      if(oppMode == 1) {
        // Program mode intro
        Serial.write("Program Mode 1 - Enter Programing");
        Serial.println();
        Serial.write("Current values: ");
        Serial.println();
  
        Serial.print("Pos1 ");
        Serial.print(memShiftPos[0]);
        Serial.println();
        
        Serial.print("Pos2 ");
        Serial.print(memShiftPos[1]);
        Serial.println();
        
        Serial.print("Pos3 ");
        Serial.print(memShiftPos[2]);
        Serial.println();
        
        Serial.print("Pos4 ");
        Serial.print(memShiftPos[3]);
        Serial.println();
        
        Serial.print("Pos5 ");
        Serial.print(memShiftPos[4]);
        Serial.println();
        
        Serial.print("Pos6 ");
        Serial.print(memShiftPos[5]);
        Serial.println();
        
        Serial.print("Pos7 ");
        Serial.print(memShiftPos[6]);
        Serial.println();
        
        oppMode = 2;
        digitalWrite(indicatorPin, LOW);
        delay(100);
        digitalWrite(indicatorPin, HIGH);
        delay(100);
        digitalWrite(indicatorPin, LOW);
        delay(100);
        digitalWrite(indicatorPin, HIGH);
      } // END program opp mode 1
      else if(oppMode == 2){
        // Display gear we are trying to store
        b2 = gearCharSetOne[(memAddress/4)];
        b3 = gearCharSetTwo[(memAddress/4)];
        if(btnState == HIGH && progModeRefresh == HIGH && memAddress <= memMaxAddress){
          progModeRefresh = LOW;
          currentPotRead = analogRead(potPin);
          // Save current position
          Serial.write("Saving Current to memory block ");
          Serial.print(memAddress);
          Serial.print(" | ");
          Serial.print(currentPotRead);
          Serial.println();
          EEPROMWritelong(memAddress, currentPotRead);
          memAddress = memAddress + 4;
          digitalWrite(indicatorPin, LOW);
          delay(100);
          digitalWrite(indicatorPin, HIGH);
          delay(100);
          digitalWrite(indicatorPin, LOW);
          delay(100);
          digitalWrite(indicatorPin, HIGH);
        } // END program store data
        else if (memAddress > memMaxAddress) {
          digitalWrite(indicatorPin, LOW);
          delay(100);
          digitalWrite(indicatorPin, HIGH);
          delay(100);
          digitalWrite(indicatorPin, LOW);
          delay(100);
          memAddress = 0;
          // Reread values from memory so any new values are loaded
          memShiftPos[0] = EEPROMReadlong(0);
          memShiftPos[1] = EEPROMReadlong(4);
          memShiftPos[2] = EEPROMReadlong(8);
          memShiftPos[3] = EEPROMReadlong(12);
          memShiftPos[4] = EEPROMReadlong(16);
          memShiftPos[5] = EEPROMReadlong(20);
          memShiftPos[6] = EEPROMReadlong(24);
          
          b1 = 45;
          b2 = 45;
          b3 = 45;
          b4 = 45;
          
          // reset program detection variables
          oppMode = 0;
          progModeRefresh = LOW;
          progDetect = millis();
        } // END program exit
      } // END program opp mode 2
    }  // END program opp modes
  } // END programming possible
  
  // Main execution
  
  if(dashBlink == 1){
    if ((millis() - dashBlinkLast) > dashBlinkDelay) {
      // Delay has elapsed. Swap Dashes.
      if(b1 == 45){
        b1 = 32;
        b4 = 32;
      } else {
        b1 = 45;
        b4 = 45;
      }
      dashBlinkLast = millis();
    }
  }
  
  if(lastb1 != b1 || lastb2 != b2 || lastb3 != b3 || lastb4 != b4){
    // Update display
    alpha4.begin(0x70);  // pass in the address
    alpha4.writeDigitAscii(0, b1);
    alpha4.writeDigitAscii(1, b2);
    alpha4.writeDigitAscii(2, b3);
    alpha4.writeDigitAscii(3, b4);
    alpha4.writeDisplay();
  }
  
  lastb1 = b1;
  lastb2 = b2;
  lastb3 = b3;
  lastb4 = b4;
  
} // END main loop
and this is my dot matrix info i need to insert to the above code. i only need to add the co ordinates for the dot matrix in the unsigned char table to the relitive places to display it in the top code

Code: Select all

//data display from right to left, from bottom to top, HIGH level display.
#define IIC_SCL A5
#define IIC_SDA A4

unsigned char data_line = 0;
unsigned char delay_count = 0;
unsigned char data_display1 = 0;
unsigned char data_display2 = 0;
unsigned char data_display3 = 0;
unsigned char data_display4 = 0;
unsigned char data_display5 = 0;
unsigned char data_display6 = 0;
unsigned char data_display7 = 0;
unsigned char data_display8 = 0;
unsigned char data_display9 = 0;
unsigned char data_display10 = 0;
unsigned char data_display11 = 0;
unsigned char data_display12 = 0;
unsigned char data_display13 = 0;
unsigned char data_display14 = 0;
unsigned char data_display15 = 0;
unsigned char data_display16 = 0;
void IIC_start();
void IIC_send(unsigned char send_data);
void IIC_end();
//unsigned char table[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char table[6][16] = {
  { 0x00, 0x7e, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7e, 0x7e, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00 },  // park
  { 0x00, 0x7e, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7e, 0x7c, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x00 },  // reverse
  { 0x00, 0x42, 0x42, 0x62, 0x62, 0x72, 0x7a, 0x7e, 0x7e, 0x5e, 0x4e, 0x46, 0x46, 0x42, 0x42, 0x00 },  // neutral
  { 0x00, 0x78, 0x7c, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7c, 0x78, 0x00 },  //drive
  { 0x00, 0x78, 0x08, 0x38, 0x09, 0x7b, 0x06, 0x0c, 0x18, 0x30, 0x6e, 0xc2, 0x8e, 0x08, 0x0e, 0x00 },  //32
  { 0x00, 0x1c, 0x3c, 0x7c, 0x7c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x7e, 0x7e, 0x00 }   // 1st
};
void setup() {
  pinMode(IIC_SCL, OUTPUT);
  pinMode(IIC_SDA, OUTPUT);
  digitalWrite(IIC_SCL, LOW);
  digitalWrite(IIC_SDA, LOW);
}
/*----------------------------------------------------------------*/
void loop() {
  /**************set the address plus 1***************/
  IIC_start();
  IIC_send(0x40);  // set the address plus 1 automatically
  IIC_end();
  /************end the process of address plus 1 *****************/
  /************set the data display*****************/
  IIC_start();
  IIC_send(0xc0);  // set the initial address as 0
  for (char i = 0; i < 16; i++) {
    IIC_send(table[data_line][i]);  // send the display data
  }
  if (++delay_count >= 10) {
    delay_count = 0;
    data_line++;
    if (data_line >= 6) {
      data_line = 0;
    }
  }

  IIC_end();
  /************end the data display*****************/
  /*************set the brightness display***************/
  IIC_start();
  IIC_send(0x8A);  // set the brightness display
  IIC_end();
  /*************end the brightness display***************/
  delay(100);
}
/*----------------------------------------------------------------*/
void IIC_start() {
  digitalWrite(IIC_SCL, LOW);
  delayMicroseconds(3);
  digitalWrite(IIC_SDA, HIGH);
  delayMicroseconds(3);
  digitalWrite(IIC_SCL, HIGH);
  delayMicroseconds(3);
  digitalWrite(IIC_SDA, LOW);
  delayMicroseconds(3);
}
void IIC_send(unsigned char send_data) {
  for (char i = 0; i < 8; i++) {
    digitalWrite(IIC_SCL, LOW);
    delayMicroseconds(3);
    if (send_data & 0x01) {
      digitalWrite(IIC_SDA, HIGH);
    } else {
      digitalWrite(IIC_SDA, LOW);
    }
    delayMicroseconds(3);
    digitalWrite(IIC_SCL, HIGH);
    delayMicroseconds(3);
    send_data = send_data >> 1;
  }
}
void IIC_end() {
  digitalWrite(IIC_SCL, LOW);
  delayMicroseconds(3);
  digitalWrite(IIC_SDA, LOW);
  delayMicroseconds(3);
  digitalWrite(IIC_SCL, HIGH);
  delayMicroseconds(3);
  digitalWrite(IIC_SDA, HIGH);
  delayMicroseconds(3);
}
Attachments
Screenshot 2023-01-17 at 19-48-16 GM 700r4 Arduino based Programmable Shift Indicator by pggibson.png
Screenshot 2023-01-17 at 19-48-16 GM 700r4 Arduino based Programmable Shift Indicator by pggibson.png (157.11 KiB) Viewed 78 times

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

Return to “Arduino”