my christmas light timer won't work correct

edited December 2017 in Questions about Code

Hi,I'm trying to make a christmas program because it's that time of year, but my lights on my tree keep returning to the basic instead of staying at the color i want them. I want my lights to change every 10 seconds to a different color & even made a timer to say when, but after they change the color automatically jumps back to the first one. I want all the lights to switch colors at the same time, so I'm just using a single timer. Since the code thing keeps messing up my code I'll place it in a link.

here the main tab; https://paste.ofcode.org/3aBVmezJj4KAGWQEdfFbV8m

here the timer tab; https://paste.ofcode.org/WBdYEJWLBXpeDZf63YjWed

here the light tab: https://paste.ofcode.org/36ecFTCGTgu4zht7xPXg9RG

here the control tab it how I place the light: https://paste.ofcode.org/v4WnYR3tW8KdEJHbyxdUbB

Answers

  • you called con.update() in draw() which overwrites all the lights every time.

    I put con.update() in setup() instead

    also, I think c in the class Light should be int and not float

    you forgot to post the Tree class.

    Chrisir

    //Tree tree;
    Control con;
    ArrayList<Light> light;
    Timer timer;
    
    
    void setup() {
      size(500, 500);
      timer = new Timer();
      con = new Control();
      light = new ArrayList<Light>();
      //tree = new Tree(15, height-50, width-15, height-50, width/2, 50);
      con.update();
    }
    
    void draw() {
      background(128, 128, 128);
      fill(0);
      text("timer: " + timer.Time, 15, 50);
      //tree.show();
      timer.countdown();
      for (int i = 0; i < light.size()-1; i++) {
        Light l = light.get(i);
        l.show();
      }
      //  con.update();
    }
    
    
    class Control {
      void update() {
        light.add(new Light(250, 80, 10));
        for (int i = 0; i < 2; i++) {
          light.add(new Light(235+(i * 30), 110, 10));
        }
        for (int i = 0; i < 4; i++) {
          light.add(new Light(220+(i * 30), 140, 10));
        }
      }
    }
    
    class Light {
      float x, y, r, ra, g, b;
      int c;
      Light(float x, float y, 
        float r) {
        this.x = x;
        this.y = y;
        this.r = r;
      }
    
      void show() {
        if (c == 0) {
          // RED
          fill(255, 0, 0);
        }
        if (c == 1) {
          // YELLOW
          fill(255, 255, 0);
        }
        if (c == 2) {
          // Blue
          fill(0, 0, 255);
        }
        if (c == 3) {
          // pink
          fill(255, 192, 203);
        }
        if (c == 4) {
          // purple
          fill(128, 0, 128);
        }
        if (c == 5) {
          // orange
          fill(255, 165, 0);
        }
        if (c == 6) {
          // black
          fill(0);
        }
        if (c == 7) {
          // white
          fill(255);
        }
        if (c == 8) {
          // aqua
          fill(0, 255, 255);
        }
        if (c == 9) {
          // grey
          fill(128, 128, 128);
        }
        if (c == 10) {
          // brown
          fill(165, 42, 42);
        }
        ellipse(x, y, r*2, r*2);
      }
    }
    
    
    class Timer {
      float Time=0;
      void countdown() {
        Time -= 1/frameRate;
        if (Time < 0) {
          for (int i = 0; i < light.size(); i++) {
            Light l = light.get(i);
            l.c += 1;
            Time = 10;
          }
        }
      }
    }
    
  • How to post in the processing forum.

    Best is to hit ctrl-t in processing first before copy paste into the forum (https://forum.processing.org).

    You can edit your post (click the small gear and the 'Edit').

    How to format Code:

    empty line before and after the code section
    
    select (highlight) entire code with the mouse
    
    hit ctrl-o OR click the small C in the command bar
    
  • the problem is the lights change but then switch back 1 second later.

  • edited December 2017 Answer ✓

    I Fixed that above

    Did you try my code? Run it

  • edited December 2017

    sry thought that was showing how to post I'll try it. TY it works. now I just need to add random lights ty for help.

  • you're welcome!

Sign In or Register to comment.