Saturday, March 23, 2013

Grass Led

For my grass I want leds that fad on and off. I wrote a program to do what I wanted:
Here is the code for that:


const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;

int fadeAmount1 = 1;
int fadeAmount2 = 1;
int fadeAmount3 = 1;
int fadeAmount4 = 1;
int fadeAmount5 = 1;
int brightness1 = 0;
int brightness2 = 51;
int brightness3 = 102;
int brightness4 = 153;
int brightness5 = 0;


void setup() {
 
  Serial.begin(9600);
 
}

void loop() {
 
  analogWrite(led1, brightness1);
  analogWrite(led2, brightness2);
  analogWrite(led3, brightness3);
  analogWrite(led4, brightness4);
  analogWrite(led5, brightness5);


  // change the brightness for next time through the loop:
  brightness1 = brightness1 + fadeAmount1;
  brightness2 = brightness2 + fadeAmount2;
  brightness3 = brightness3 + fadeAmount3;
  brightness4 = brightness4 + fadeAmount4;
  brightness5 = brightness5 + fadeAmount5;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness1 == 0 || brightness1 == 255) {
    fadeAmount1 = -fadeAmount1 ;
  }    
  if (brightness2 == 0 || brightness2 == 255) {
    fadeAmount2 = -fadeAmount2;
  }    
  if (brightness3 == 0 || brightness3 == 255) {
    fadeAmount3 = -fadeAmount3;
  }      
  if (brightness4 == 0 || brightness4 == 255) {
    fadeAmount4 = -fadeAmount4 ;
  }    
  if (brightness5 == 0 || brightness5 == 255) {
    fadeAmount5 = -fadeAmount5;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  //delay(30);                    
  Serial.println(brightness1);
}

The problem is that when I tried to add this to the code I previously had with the servos and the range sensor in it, it freaked out and did this:
Here is the code for the entire program. It works just fine with the range finder and the servos. The range finder is a little slow so I don't know if that might be causing problems.

#include <Servo.h>

Servo grass;
Servo flower;

const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;

int fadeAmount1 = 1;
int fadeAmount2 = 1;
int fadeAmount3 = 1;
int fadeAmount4 = 1;
int fadeAmount5 = 1;
int brightness1 = 0;
int brightness2 = 51;
int brightness3 = 102;
int brightness4 = 153;
int brightness5 = 0;


#include <Ultrasonic.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);


void setup() {
 
  Serial.begin(9600);
  grass.attach(14);//pins 9 and 10 don't work with servos
  flower.attach(2);

}

void loop() {
 
   grass.write(92);
   flower.write(179);
/*  
   float cmMsec, inMsec;
  long microsec = ultrasonic.timing();

  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  Serial.print("MS: ");
  Serial.print(microsec);
  Serial.print(", CM: ");
  Serial.print(cmMsec);
  Serial.print(", IN: ");
  Serial.println(inMsec);
  //delay(1000);
*/
  analogWrite(led1, brightness1);
  analogWrite(led2, brightness2);
  analogWrite(led3, brightness3);
  analogWrite(led4, brightness4);
  analogWrite(led5, brightness5);


  // change the brightness for next time through the loop:
  brightness1 = brightness1 + fadeAmount1;
  brightness2 = brightness2 + fadeAmount2;
  brightness3 = brightness3 + fadeAmount3;
  brightness4 = brightness4 + fadeAmount4;
  brightness5 = brightness5 + fadeAmount5;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness1 == 0 || brightness1 == 255) {
    fadeAmount1 = -fadeAmount1 ;
  }    
  if (brightness2 == 0 || brightness2 == 255) {
    fadeAmount2 = -fadeAmount2;
  }    
  if (brightness3 == 0 || brightness3 == 255) {
    fadeAmount3 = -fadeAmount3;
  }      
  if (brightness4 == 0 || brightness4 == 255) {
    fadeAmount4 = -fadeAmount4 ;
  }    
  if (brightness5 == 0 || brightness5 == 255) {
    fadeAmount5 = -fadeAmount5;
}
}

I had the range finder code commented out because I thought it might be causing issues, but that didn't seem to help at all.

No comments:

Post a Comment