Arduino and Trinket Pro 5V

Hello,

I have a project that requires some C++ code for my Trinket.

Would appreciate some feedback from knowledgeable Arduino programmers to answer a few questions.

  1. Can the Trinket process a Sketch that has several operations running in loops?

Examples of what I want

a: Monitor 3 momentary push buttons. When each are pressed, to toggle defined outputs either on or off.

b. Random loop to make 2 LEDS flash a a slow rate.

c:. Random loop to make 3 LEDs flash at a faster rate

d. Fade up/Fade down loop for an LED. I need to be able to set the percentage and rate.

I am not an experienced Arduino programmer. If all that I ask is possible, I will need assistance from members on this site.

All feedback is appreciated.

Thanks,

Hi @vcholman

Yes, all this is possible on a Trinket Pro 5V.

The only changes you may have to consider when adapting Arduino sketches are:

  • Pins #2 and #7 are not available (they are exclusively for USB)
  • The onboard 5V regulator can provide 150mA output, not 800mA out
  • You cannot plug shields directly into the Pro Trinket
  • SoftwareSerial will not work on the 3V Pro Trinket without changes. See this FAQ.
  • There is no Serial-to-USB chip onboard. This is to keep the Pro Trinket small and inexpensive, you can use any FTDI cable to connect to the FTDI port for a Serial connection. The USB connection is for uploading new code only.
  • The bootloader on the Pro Trinket use 4KB of FLASH so the maximum sketch size is 28,672 bytes. The bootloader does not affect RAM usage.
  • Trinket is supported on Linux, Mac OS or Windows!

Look page " Hints for Arduino-compatibility "

RV mineirin

I'm not familiar with the Trinket so rely on @ruilviana's knowledge

Some assistance:

Hi,

All good information.

Some of your concerns should not be a problem in my case.

I am using the Trinket Pro 5 volt

Output current limitation is not a show stopper. Outputs will be triggering transistors, which will provide the current for driving output devices.

Serial interface is not necessary. Once the code is uploaded via USB, the Trinket will run offline.

All I need is 6 outputs for the LEDs. If necessary, I can use two Trinkets. One would drive the random flashing LEDs and fade up/down led. The other Trinket can monitor the 3 push buttons, to toggle 3 separate outputs.

I have 3 separate sketches for my LEDs.
a. Random flash for 2 LEDs
b. Random flash for 3 LEDs
c. Fade up/down for 1 led.

My loops have delays, which I have learned no one likes. Can someone help me combine the 3 Sketches listed into a single Sketch that has no delays?

Thanks

The reason why 'no one likes' is that delays make it close to impossible to achieve what you want to achieve.

As you haven't shared your code, below will probably be more or less what your fade code looks like.

int fadeValue;
const int ledPin = 6;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  for (int cnt = 0; cnt <= 255; cnt++)
  {
    analogWrite(ledPin, fadeValue);
    fadeValue += 5;
    delay(100);
  }

  for (int cnt = 255; cnt >= 0; cnt--)
  {
    analogWrite(ledPin, fadeValue);
    fadeValue -= 5;
    delay(100);
  }
}

For demonstration, we will just use the first for-loop and leave the second for-loop out of the equation.

void loop()
{
  for (int cnt = 0; cnt <= 255; cnt++)
  {
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += 5;
    // wait a bit
    delay(100);
  }
}

The trick is to split those for loops in smaller pieces that will do one step at a time. In the below code we let loop() do the looping instead of the for-loop; the effect should be the same as above.

void loop()
{
  // update the led
  analogWrite(ledPin, fadeValue);
  // wait a bit
  delay(100);
  // update the brightness for the next time
  fadeValue += 5;
  // if over maximum, reset
  if (fadeValue > 255)
  {
    fadeValue = 0;
  }
}

If this is clear to you, we can add the decreasing of the brightnes back into this code. We use a variable fadeStep that will be positive to increase the brightnes and negative to decrease the brightness.

// the brightness level
int fadeValue;
// brightness steps
int fadeStep = 5;
// led pin
const int ledPin = 6;

And in loop() use that variable.

void loop()
{
  // update the led
  analogWrite(ledPin, fadeValue);
  // wait a bit
  delay(100);
  // update the brightness for the next time
  fadeValue += fadeStep;
  // if maximum or minimum reached, change the 'direction'
  if (fadeValue >= 255 || fadeValue <= 0)
  {
    fadeStep *= -1;
  }
}

You can now put this in a function that you call repeatedly from loop().

void loop()
{
  fade();
}

void fade()
{
  // update the led
  analogWrite(ledPin, fadeValue);
  // wait a bit
  delay(100);
  // update the brightness for the next time
  fadeValue += fadeStep;
  // if maximum or minimum reached, change the 'direction'
  if (fadeValue >= 255 || fadeValue <= 0)
  {
    fadeStep *= -1;
  }
}

We now have a function that we can further optimise to get rid of the delay() in favour of *millis()).

bool fade()
{
  // the brightness level of the led
  static int fadeValue;
  // time when the led's brightness needs to be updated
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the led
  if (currentTime >= nextTime)
  {
    Serial.println(fadeValue);
    // set the next time
    nextTime += fadeInterval;
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += fadeStep;
    // if over maximum or minimum reached, change the 'direction'
    if (fadeValue >= 255 || fadeValue <= 0)
    {
      fadeStep *= -1;
    }
  }

  return true;
}

First we make use of some local variables that are static; this means that they are only known inside the function but will be remembered (don't go out of scope). The new fadeValue replaces the global fadeValue near the top of the original code. The variable nextTime indicates when the led's brightness needs to be updated; this variable is updated each time that you update the led's brightness. The variable currentTime keeps trackk of the current time (in millis()).
A new global variable fadeInterval is used to get rid of the hard-coded 100 milliseconds.

The function returns a bool that can be used in loop(); it's currently just a place holder in case you have additional requirements where you want to know if e.g. a fade from 0 to 255 to 0 has been completed.

If anything is not clear, ask.

You can now also write functions to flash random leds etc. and call them from loop(); use the same timing principles as demonstrated in the last fade(). E.g.

void random2()
{
}

void random3()
{
}

That's for you to fill in :wink:

loop() will than look like

void loop()
{
  fade();
  random2();
  random3();
}

The complete code:

// brightness steps
int fadeStep = 5;
// led pin
const int ledPin = 6;
// the fade interval
const uint32_t fadeInterval = 100;

void setup()
{
  Serial.begin(57600);
  while (!Serial);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  fade();
  random2();
  random3();
}

/*
  fade led
  Returns:
    true
*/
bool fade()
{
  // the brightness level of the led
  static int fadeValue;
  // time when the led's brightness needs to be updated
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the led
  if (currentTime >= nextTime)
  {
    Serial.println(fadeValue);
    // set the next time
    nextTime += fadeInterval;
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += fadeStep;
    // if over maximum or minimum reached, change the 'direction'
    if (fadeValue >= 255 || fadeValue <= 0)
    {
      fadeStep *= -1;
    }
  }

  return true;
}

/*
  randomly blink two leds
*/
void random2()
{
}

/*
  randomly blink three leds
*/
void random3()
{
}

Sterretje,

Your fade up/dn sketch is very close to what I want. I don't want the led turning off. It goes to full brightness, steps down by 5 to zero. Then backup.

fadeStep if I'm correct changes how many step at a time the loop ramps from 0 to 255. And the fadeInterval is the step rate.

When I changed

if (fadeValue >= 255 || fadeValue <= 0)

to

if (fadeValue >= 255 || fadeValue <= 10)

The led had a rapid flash, no fade visable.

Thanks,

How would I modify the code so the led would go from 10 percent brightness up to 100 percent?

Sterretje,

Here is my not so efficient fade sketch, it has delays. But the fade up/dn brightness levels and rate are what I need.

int ledPin = 9; //LED connected to output digital pin 9

void setup() {
  // Nothing happens in setup

}

void loop() {
  // fade in from min to max in increments of 1 points:

  for (int fadeValue = 20; fadeValue <= 255; fadeValue += 1) {
    // Sets the value (range from 5 to 255);

    analogWrite(ledPin, fadeValue);

    // Wait for 20 milliseconds to see the dimming effect
    delay(20);
  }

  // Fade out from max to min in increments of 5 points;

  for (int fadeValue = 255; fadeValue >= 20; fadeValue -= 1) {
    // Sets the value (range from 5 to 255);

    analogWrite(ledPin, fadeValue);
    // Wait for 20 milliseconds to see the dimming effect

    delay(20);
  }

}

Sterretje,

Once I understand how to modify your fade up/dn code as listed under the "the complete code" so my led doesn't turn off. Remember I would like the fade to start at 10 percent and top at 100 percent. Then fade down from 100 percent to 10 percent, then repeat.

Once I have the fade up/dn code working, would you please provide the code to randomly blink 3 LED's so I can see how the two separate codes work together in a single sketch.

Once I see how to combine two codes, I should be able to add the other set of code to randomly blink 2 LEDs.

Thank you,

fadeStep are the increments; in the original code wit goes 0, 5, 10, 15, ..., 250, 255, 250, 245, ..., 5, 0, 5, 10 etc. You can observe the values in serial monitor.
fadeInterval indicates the amount of time before the led brightness is updated.

I've tested it with a Leonardo (and pin 13) and it works as expected. To improve it, you have to set the initial value of fadeValue in the fade() function.

static int fadeValue = 10;

It will be better to declare a variable minFadeValue above setup and use that in the fade() function.

// brightness steps
int fadeStep = 1;
// minimum brightness
const int minFadeValue = 10;
// led pin
const int ledPin = 6;
// the fade interval
const uint32_t fadeInterval = 20;
..
..
/*
  fade led
  Returns:
    true
*/
bool fade()
{
  // the brightness level of the led
  static int fadeValue = minFadeValue;
  // time when the led's brightness needs to be updated
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the led
  if (currentTime >= nextTime)
  {
    Serial.println(fadeValue);
    // set the next time
    nextTime += fadeInterval;
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += fadeStep;
    // if over maximum or minimum reached, change the 'direction'
    if (fadeValue >= 255 || fadeValue <= minFadeValue)
    {
      fadeStep *= -1;
    }
  }

  return true;
}

If you want to adjust the minimum brightness, simply change the value of minFadeValue; it's currently set to what I quoted (10), not to what your fade code had (20). I've also adjusted the timing and the step size to reflect your fade code.

Note: timing might be of due to serial prints; you can increase the baudrate if so or remove the serial prints from fade().

/*
  randomly blink three leds
*/
void random3()
{
  // time when an update of the random leds is required
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the random leds
  if (currentTime >= nextTime)
  {
    // set the next time
    nextTime += random3Interval;

    // do your stuff here

  }
}

You need to declare a variable random3Interval in the same way as fadeInterval was declared.

Sterretje,

There is something wrong with the code. Right after...

const uint32_t fadeInterval = 20;

Arduino reports...

exit status 1
expected unqualified-id before '.' token

I can't see your code :wink: Please post it.

Here you are...

// brightness steps
int fadeStep = 1;
// minimum brightness
const int minFadeValue = 10;
// led pin
const int ledPin = 6;
// the fade interval
const uint32_t fadeInterval = 20;
..
..
/*
  fade led
  Returns:
    true
*/
bool fade()
{
  // the brightness level of the led
  static int fadeValue = minFadeValue;
  // time when the led's brightness needs to be updated
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the led
  if (currentTime >= nextTime)
  {
    Serial.println(fadeValue);
    // set the next time
    nextTime += fadeInterval;
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += fadeStep;
    // if over maximum or minimum reached, change the 'direction'
    if (fadeValue >= 255 || fadeValue <= minFadeValue)
    {
      fadeStep *= -1;
    }
  }

  return true;
}

The .. reflects existing code; I'm too lazy to copy / paste it all :wink:

lol...got it.

This will be the full code till now

// brightness steps
int fadeStep = 1;
// minimum brightness
const int minFadeValue = 30;
// led pin
const int ledPin = 6;
// the fade interval
const uint32_t fadeInterval = 20;
// interval for 3 random leds
const uint32_t random3Interval = 100;

void setup()
{
  Serial.begin(57600);
  while (!Serial);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  fade();
  random2();
  random3();
}

/*
  fade led
  Returns:
    true
*/
bool fade()
{
  // the brightness level of the led
  static int fadeValue = minFadeValue;
  // time when the led's brightness needs to be updated
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the led
  if (currentTime >= nextTime)
  {
    Serial.println(fadeValue);
    // set the next time
    nextTime += fadeInterval;
    // update the led
    analogWrite(ledPin, fadeValue);
    // update the brightness for the next time
    fadeValue += fadeStep;
    // if over maximum or minimum reached, change the 'direction'
    if (fadeValue >= 255 || fadeValue <= minFadeValue)
    {
      fadeStep *= -1;
    }
  }

  return true;
}

/*
  randomly blink two leds
*/
void random2()
{
}

/*
  randomly blink three leds
*/
void random3()
{
  // time when an update of the random leds is required
  static uint32_t nextTime;
  // the current time
  uint32_t currentTime = millis();

  // if it's time to update the random leds
  if (currentTime >= nextTime)
  {
    // set the next time
    nextTime += random3Interval;

    // do your stuff here

  }
}

Sweet.

Performs exactly as I had hope for.

Great job.

Thank you,

I've changed post #17 as it used pin 13 instead of the earlier pin 6; just in case.

If your problem is solved, you can tick the solution checkbox under the most useful reply. That way others can see that it's solved if they encounter a similar problem.

i would like to wait until my two random led blinking codes are working.
ASAP