Uint8_t trigger meaning

I'm new to coding and have encountered this function in a new code I wanted to try however I don't understand what it means or what it exactly does. Can someone please clarify my doubt .Any help is appreciated.

This is the full code:

#include <Encoder.h>

// Delays for certain events in ms
const int ROTARY_DELAY = 100;    // Time between checking for rotary turns
const int BUTTON_DELAY = 800;    // Button debouncing, and repeating
const int FLASH_DELAY = 600;     // How long to flash the entire row after button press
const int CURSOR_DELAY = 200;    // How quickly to flash the cursor led

// Pin Assignments
uint8_t rota = 2;
uint8_t rotb = 3;
uint8_t button = 4;
uint8_t dataIn = 5;
uint8_t load = 6;
uint8_t clock = 7;
uint8_t trigger = 12;

int maxInUse = 1;
uint8_t x = 0;

byte max7219_reg_noop        = 0x00;
byte max7219_reg_digit0      = 0x01;
byte max7219_reg_digit1      = 0x02;
byte max7219_reg_digit2      = 0x03;
byte max7219_reg_digit3      = 0x04;
byte max7219_reg_digit4      = 0x05;
byte max7219_reg_digit5      = 0x06;
byte max7219_reg_digit6      = 0x07;
byte max7219_reg_digit7      = 0x08;
byte max7219_reg_decodeMode  = 0x09;
byte max7219_reg_intensity   = 0x0a;
byte max7219_reg_scanLimit   = 0x0b;
byte max7219_reg_shutdown    = 0x0c;
byte max7219_reg_displayTest = 0x0f;


Encoder knobLeft(rota, rotb);


char id = 0;

void putByte(byte data) {
  byte i = 8;
  byte mask;
  while (i > 0) {
    mask = 0x01 << (i - 1);      // get bitmask
    digitalWrite( clock, LOW);   // tick
    if (data & mask) {           // choose bit
      digitalWrite(dataIn, HIGH);// send 1
    } else {
      digitalWrite(dataIn, LOW); // send 0
    }
    digitalWrite(clock, HIGH);   // tock
    --i;                         // move to lesser bit
  }
}

void maxSingle( byte reg, byte col) {
  //maxSingle is the "easy"  function to use for a     //single max7219

  digitalWrite(load, LOW);       // begin
  putByte(reg);                  // specify register
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  digitalWrite(load, LOW);       // and load da shit
  digitalWrite(load, HIGH);
}

void maxAll (byte reg, byte col) {    // initialize  all  MAX7219's in the system
  int c = 0;
  digitalWrite(load, LOW);  // begin
  for ( c = 1; c <= maxInUse; c++) {
    putByte(reg);  // specify register
    putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  }
  digitalWrite(load, LOW);
  digitalWrite(load, HIGH);
}

void maxOne(byte maxNr, byte reg, byte col) {
  //maxOne is for adressing different MAX7219's,
  //whilele having a couple of them cascaded

  int c = 0;
  digitalWrite(load, LOW);  // begin

  for ( c = maxInUse; c > maxNr; c--) {
    putByte(0);    // means no operation
    putByte(0);    // means no operation
  }

  putByte(reg);  // specify register
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data

  for ( c = maxNr - 1; c >= 1; c--) {
    putByte(0);    // means no operation
    putByte(0);    // means no operation
  }

  digitalWrite(load, LOW); // and load da shit
  digitalWrite(load, HIGH);
}


void setup () {
  pinMode(dataIn, OUTPUT);
  pinMode(clock,  OUTPUT);
  pinMode(load,   OUTPUT);
  pinMode(button, INPUT);
  pinMode(trigger, OUTPUT);
  digitalWrite(button, HIGH);
  digitalWrite(13, LOW);
  digitalWrite(trigger, LOW);

  //initiation of the max 7219
  maxAll(max7219_reg_scanLimit, 0x07);
  maxAll(max7219_reg_decodeMode, 0x00);  // using an led matrix (not digits)
  maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
  maxAll(max7219_reg_displayTest, 0x00); // no display test
  for (x = 1; x <= 8; x++) { // empty registers, turn all LEDs off
    maxAll(x, 0);
  }
  maxAll(max7219_reg_intensity, 0x01 & 0x0f);  // the first 0x0f is the value you can set range: 0x00 to 0x0f

}

// Value to display a single LED on a row
uint8_t values[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128};
// Array with all rows, indicating which 'value' to show
uint8_t line[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// What row we are current working with
uint8_t idx = 0;
// Our delay counters (in ms)
int delayButton = 0;
int delayRotary = ROTARY_DELAY;
int delayFlash = 0;
int delayCursor = CURSOR_DELAY;
int cursorState = 0;

// Rotary position, reset after each loop
long pos = 0;
// Do we update the matrix this run?
uint8_t update = 1;

void loop () {

  // Flash the row after a button press
  if (delayFlash && idx != 8) {
    maxSingle(idx + 1, 255);

    if (delayFlash == 1) {
      maxSingle(idx + 1, values[line[idx + 1]]);
    }

    delayFlash--;
    if (delayButton) delayButton--;

    return;
  }

  // Check for button press
  if (delayButton) delayButton--;
  if (!digitalRead(4) && delayButton == 0) {
    if (idx == 8)
      idx = 0;
    else
      idx++;
    delayButton = BUTTON_DELAY;
    delayFlash = FLASH_DELAY;
  }


  // Checks for rotary turning, and update
  //  the matrix buffer
  // My rotary encoder had '2 turns' per detent, making
  //  it awkward to move a single position. This is why
  //  the check is for > 1 and < -1 to make it a full detent.
  if (delayRotary) delayRotary--;
  if (delayRotary == 0 && idx != 8) {
    pos = knobLeft.read();
    if (pos > 1) {
      if (line[idx] != 0)
        line[idx]--;
      else
        line[idx] = 8;
      update = 1;
    } else if (pos < -1) {
      if (line[idx] != 8)
        line[idx]++;
      else
        line[idx] = 0;
      update = 1;
    }

    if (update) {
      knobLeft.write(0);
      delayRotary = ROTARY_DELAY;

      if (pos) {
        cursorState = 1;
        delayCursor = CURSOR_DELAY;
      }
    }
  }


  // Toggle the state of the cursor after the
  //  cursor delay (in ms) has been reached
  if (delayCursor) delayCursor--;
  if (delayCursor == 0 && idx != 8) {
    delayCursor = CURSOR_DELAY;
    cursorState = 1 - cursorState;
    update = 1;
  }

  // Update the matrix only if a change has been made
  if (update) {
    for (x = 0; x < 8; x++) {
      if (x == idx) {
        if (cursorState && line[x] != 0)
          maxSingle(x + 1, values[line[x]]);
        else
          maxSingle(x + 1, 0);
      } else {
        if (line[x] == 0) continue;
        digitalWrite(trigger, HIGH);
        maxSingle(x + 1, values[line[x]]);
        digitalWrite(trigger, LOW);t
      }
    }
    update = 0;
  }

  delay(1);

}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

1 Like
uint8_t trigger = 12;

That line of code declares an unsigned 8 bit integer variable, ie a byte, named trigger and gives it a value of 12

Later on in the code it has HIGH or LOW written to it (pin 12) using digitalWrite()

1 Like

Thank you very much, I followed the advice. :grinning_face_with_smiling_eyes:

Since your named Arduino pins generally won't change value, it is good to declare them 'const'. That way the compiler can warn you if you accidentally try to change the value.

I like pin names to end in 'Pin' and all global variable names to start with a capital letter.

'x' and 'id' are poor names for global variables, particularly since 'id' is apparently not used at all.

#include <Encoder.h>

// Delays for certain events in ms
const int ROTARY_DELAY = 100;    // Time between checking for RotAPinry turns
const int BUTTON_DELAY = 800;    // Button debouncing, and repeating
const int FLASH_DELAY = 600;     // How long to flash the entire row after ButtonPin press
const int CURSOR_DELAY = 200;    // How quickly to flash the cursor led

// Pin Assignments
const uint8_t RotAPin = 2;
const uint8_t RotBPin = 3;
const uint8_t ButtonPin = 4;
const uint8_t DataInPin = 5;
const uint8_t LoadPin = 6;
const uint8_t ClockPin = 7;
const uint8_t TriggerPin = 12;

int MaxInUse = 1;

const byte Max7219Reg_noop        = 0x00;
const byte Max7219Reg_digit0      = 0x01;
const byte Max7219Reg_digit1      = 0x02;
const byte Max7219Reg_digit2      = 0x03;
const byte Max7219Reg_digit3      = 0x04;
const byte Max7219Reg_digit4      = 0x05;
const byte Max7219Reg_digit5      = 0x06;
const byte Max7219Reg_digit6      = 0x07;
const byte Max7219Reg_digit7      = 0x08;
const byte Max7219Reg_decodeMode  = 0x09;
const byte Max7219Reg_intensity   = 0x0a;
const byte Max7219Reg_scanLimit   = 0x0b;
const byte Max7219Reg_shutdown    = 0x0c;
const byte Max7219Reg_displayTest = 0x0f;

Encoder knobLeft(RotAPin, RotBPin);

void putByte(byte data)
{
  byte i = 8;
  byte mask;
  while (i > 0)
  {
    mask = 0x01 << (i - 1);      // get bitmask
    digitalWrite( ClockPin, LOW);   // tick
    if (data & mask)             // choose bit
    {
      digitalWrite(DataInPin, HIGH);// send 1
    }
    else
    {
      digitalWrite(DataInPin, LOW); // send 0
    }
    digitalWrite(ClockPin, HIGH);   // tock
    --i;                         // move to lesser bit
  }
}

void maxSingle( byte reg, byte col)
{
  //maxSingle is the "easy"  function to use for a     //single max7219

  digitalWrite(LoadPin, LOW);       // begin
  putByte(reg);                  // specify register
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  digitalWrite(LoadPin, LOW);       // and LoadPin da shit
  digitalWrite(LoadPin, HIGH);
}

void maxAll (byte reg, byte col)      // initialize  all  MAX7219's in the system
{
  int c = 0;
  digitalWrite(LoadPin, LOW);  // begin
  for ( c = 1; c <= MaxInUse; c++)
  {
    putByte(reg);  // specify register
    putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  }
  digitalWrite(LoadPin, LOW);
  digitalWrite(LoadPin, HIGH);
}

void maxOne(byte maxNr, byte reg, byte col)
{
  //maxOne is for adressing different MAX7219's,
  //whilele having a couple of them cascaded

  int c = 0;
  digitalWrite(LoadPin, LOW);  // begin

  for ( c = MaxInUse; c > maxNr; c--)
  {
    putByte(0);    // means no operation
    putByte(0);    // means no operation
  }

  putByte(reg);  // specify register
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data

  for ( c = maxNr - 1; c >= 1; c--)
  {
    putByte(0);    // means no operation
    putByte(0);    // means no operation
  }

  digitalWrite(LoadPin, LOW); // and LoadPin da shit
  digitalWrite(LoadPin, HIGH);
}


void setup ()
{
  pinMode(DataInPin, OUTPUT);
  pinMode(ClockPin,  OUTPUT);
  pinMode(LoadPin,   OUTPUT);
  pinMode(ButtonPin, INPUT);
  pinMode(TriggerPin, OUTPUT);
  digitalWrite(ButtonPin, HIGH);
  digitalWrite(13, LOW);
  digitalWrite(TriggerPin, LOW);

  //initiation of the max 7219
  maxAll(Max7219Reg_scanLimit, 0x07);
  maxAll(Max7219Reg_decodeMode, 0x00);  // using an led matrix (not digits)
  maxAll(Max7219Reg_shutdown, 0x01);    // not in shutdown mode
  maxAll(Max7219Reg_displayTest, 0x00); // no display test
  for (int x = 1; x <= 8; x++)   // empty registers, turn all LEDs off
  {
    maxAll(x, 0);
  }
  maxAll(Max7219Reg_intensity, 0x01 & 0x0f);  // the first 0x0f is the value you can set range: 0x00 to 0x0f

}

// Value to display a single LED on a row
uint8_t values[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128};
// Array with all rows, indicating which 'value' to show
uint8_t line[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// What row we are current working with
uint8_t idx = 0;
// Our delay counters (in ms)
int delayButton = 0;
int delayRotary = ROTARY_DELAY;
int delayFlash = 0;
int delayCursor = CURSOR_DELAY;
int cursorState = 0;

// Rotary position, reset after each loop
long pos = 0;
// Do we update the matrix this run?
uint8_t update = 1;

void loop ()
{

  // Flash the row after a ButtonPin press
  if (delayFlash && idx != 8)
  {
    maxSingle(idx + 1, 255);

    if (delayFlash == 1)
    {
      maxSingle(idx + 1, values[line[idx + 1]]);
    }

    delayFlash--;
    if (delayButton) delayButton--;

    return;
  }

  // Check for ButtonPin press
  if (delayButton) delayButton--;
  if (!digitalRead(4) && delayButton == 0)
  {
    if (idx == 8)
      idx = 0;
    else
      idx++;
    delayButton = BUTTON_DELAY;
    delayFlash = FLASH_DELAY;
  }


  // Checks for rotary turning, and update
  //  the matrix buffer
  // My rotary  encoder had '2 turns' per detent, making
  //  it awkward to move a single position. This is why
  //  the check is for > 1 and < -1 to make it a full detent.
  if (delayRotary) delayRotary--;
  if (delayRotary == 0 && idx != 8)
  {
    pos = knobLeft.read();
    if (pos > 1)
    {
      if (line[idx] != 0)
        line[idx]--;
      else
        line[idx] = 8;
      update = 1;
    }
    else if (pos < -1)
    {
      if (line[idx] != 8)
        line[idx]++;
      else
        line[idx] = 0;
      update = 1;
    }

    if (update)
    {
      knobLeft.write(0);
      delayRotary = ROTARY_DELAY;

      if (pos)
      {
        cursorState = 1;
        delayCursor = CURSOR_DELAY;
      }
    }
  }

  // Toggle the state of the cursor after the
  //  cursor delay (in ms) has been reached
  if (delayCursor) delayCursor--;
  if (delayCursor == 0 && idx != 8)
  {
    delayCursor = CURSOR_DELAY;
    cursorState = 1 - cursorState;
    update = 1;
  }

  // Update the matrix only if a change has been made
  if (update)
  {
    for (int x = 0; x < 8; x++)
    {
      if (x == idx)
      {
        if (cursorState && line[x] != 0)
          maxSingle(x + 1, values[line[x]]);
        else
          maxSingle(x + 1, 0);
      }
      else
      {
        if (line[x] == 0) continue;
        digitalWrite(TriggerPin, HIGH);
        maxSingle(x + 1, values[line[x]]);
        digitalWrite(TriggerPin, LOW);
      }
    }
    update = 0;
  }

  delay(1);
}
1 Like

Thank for pointing that out. I'm new to coding , I am very happy to learn something new :innocent:

What exactly happens over here? what is a mask?
void putByte(byte data)
{
byte i = 8;
byte mask;
while (i > 0)
{
mask = 0x01 << (i - 1); // get bitmask
digitalWrite( ClockPin, LOW); // tick
if (data & mask) // choose bit
{
digitalWrite(DataInPin, HIGH);// send 1
}
else
{
digitalWrite(DataInPin, LOW); // send 0
}
digitalWrite(ClockPin, HIGH); // tock
--i; // move to lesser bit
}
}

A 'mask' or 'bitmask' is a pattern of bits where the bits you are interested in are '1' and the bits you are not interested in are '0'.

In this case, 'mask' is a single bit calculated by taking the low-order bit (0x01 == 0b00000001) and shifting it to the left (<<) by i-1 bits. The variable 'i' starts at 8 so the first mask is 1<<7 or 0b10000000(==0x80).

The 'mask' is used to select one bit from 'data'. When you use the bitwise AND operator (&) you keep only those bits that are 1 in both. This means that if the top bit of 'data' is 0, the result is 0. If the top bit of 'data' is 1, the result is 0b10000000.

The 'if' statement takes a boolean (true/false) value. When converting a number to boolean, zero == false and any other value == true. The statement 'if (number)' can be read as "if number is not zero".

The end result: If the selected ('masked') bit is 1, execute the 'if' and set the pin to HIGH. If the selected ('masked') bit is 0, execute the 'else' and set the pin to LOW.

1 Like

Thank you very much. Your explanation helped me understand the code. I appreciate it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.