Heartbeat effect

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Heartbeat effect

Post by chargerdude70 »

Sorry, I am new to this programming. I am trying to create a heartbeat effect using the loop function. I just can’t get it to work. And believe me, it’s not for lack of trying. I have spent hours trying different things. I know what I want and have an idea how, but my skills just are not there. I have also tried doing a count up format with no luck. I just can’t get two beats then lights off rhythm.

First cycle: I want all 16 lights to fade from off to brightest and back to off.
Second cycle: I want all lights bet set to off.
I want first cycle to run a definable amount (twice) with a definable pause in between.
I want second cycle to run with a definable pause in between the first cycle for a definable amount of time.
I want it all to run in a loop.

Would someone please help me out with this? Please feel free to cur teak the code. I'm sure it can be done more robust from someone with skills

Code: Select all

#include <Adafruit_NeoPixel.h>
	#define PIN 0 // Trinket Output pin #
        Adafruit_NeoPixel pixels = Adafruit_NeoPixel(16, PIN);
	uint8_t i,j,n;
        uint8_t p = 25; //color decrease increments 
        uint8_t wait = 3500;
        uint32_t BLACK  = 0x000000; // black
        uint32_t GREEN  = 0x00FF00; // green
        uint32_t RED = 0xFF0000; // red
        
	void setup() {
	  pixels.begin();
          pixels.show(); 
          }

        void heartBeat(){
          for(j = 0; j < 250; j=j+p) {
          for(i=0; i<16; i++) {
          pixels.setPixelColor(i, GREEN);
          pixels.show();
          //delay(0);
          }
          }
          for(j = 250; j > 0; j=j-p) {
          for(i=0; i<16; i++) {
          pixels.setPixelColor(i,GREEN);
          pixels.show();
          //delay(0);
          }
          }
          delay(50);{// time between heartbeats
          }
           
          for(j = 0; j < 250; j=j+p) {
          for(i=0; i<16; i++) {
          pixels.setPixelColor(i, GREEN);
          pixels.show();
           
          }
          }
                   
          for(j = 250; j > 0; j=j-p) {
          for(i=0; i<16; i++) {
          pixels.setPixelColor(i,GREEN);
          pixels.show();
          //delay(0); 
          }
          } 
          }
           
          
        void darkNess() {
          for(i=0; i<16; i++) {
          pixels.setPixelColor(i,RED);
          pixels.show();
          //delay(0);
          }
          }
          
           
        void loop() {
          heartBeat();   //  run colorGlow function every loop
          darkNess();    //  run darkNess function every loop
          delay(wait);   
          }

User avatar
jcgoodman
 
Posts: 107
Joined: Thu Jan 23, 2014 6:03 pm

Re: Heartbeat effect

Post by jcgoodman »

So close! Looks like your "j" variable is intended to control the brightness as you fade in / out, but you're not using it to do that. I'd suggest you use the other version of the setPixelColor() function, which lets you set separate red, green, blue amounts. So, replace

Code: Select all

          pixels.setPixelColor(i,GREEN);
with

Code: Select all

          pixels.setPixelColor(i,0,j,0);
everywhere, and it should work!

One other bug I noticed:

Code: Select all

        uint8_t wait = 3500;
a uint8_t is is 8 bits, so it can't have a value greater than 256. Use

Code: Select all

        int wait = 3500;
instead.

NaughtyGnosiophile
 
Posts: 3
Joined: Tue Feb 11, 2014 12:51 am

Re: Heartbeat effect

Post by NaughtyGnosiophile »

From the first bug can tell you are honest about not knowing how to program (it's really obvious if you understand what the code is doing). And as a programmer I'm amazed at how far you got! Bravo!

As for the second bug, I can picture myself doing that. =)

I probably feel like you when I'm trying to read assembly. ;-P

User avatar
chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: Heartbeat effect

Post by chargerdude70 »

@ jcgoodman>>>>Thank You....Thank You....honestly I don't think I would have gotten that. Here I thought all this time it was my format and kept changing and changing.

@NaughtyGnosiophile>>>>thank you for the kind words. It's mainly bits a pieces I picked up from here and I just compiled them together. This segment is the last in a series of three for and small project I am working on for my car.

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

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”