Stopping a program under certain conditions - c++

I am greeting you today with a program question for my project that uses light as an input for a servo on a wall. Light will enter the room and the shade will go down, and in the absence of light, the shade will recede. I would like to make two conditions in a void loop in the arduino code only applicable one time unless the condition changes. By saying this I mean that, I want this void loop to run continually,in which i have two conditions. And if the same condition is met twice in a row,(ie. sensor reading between 800 and 10000, like 5000 and and then at 6032), nothing will run. If one condition is met and then the other is met afterwards, that's ok. Here's my code and any help as to what reference commands I should use or my next course of action would be greatly appreciated.
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
#include <Servo.h>
Servo myservo1;
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 30; //higher = slower but more stable and accurate
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
if(lightLevel>800 && lightLevel<1000)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1300);delay(1000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
else if(lightLevel<800)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000 / (length / samples)) * 10;
return freq;
}

First add this to your setup ...
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
myservo1.attach(5);
}
Then make a new variable and add it to your if statements
Boolean once; // declare this with your other int variables
if(lightLevel > 800 && lightLevel < 1000 && once==True)
{
myservo1.writeMicroseconds(1300);delay(1000);
myservo1.writeMicroseconds(1500);delay(1000);
once = False;
}
else if(lightLevel<800 && once == False)
{
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(1000);
once = True;
}

Related

Multiple if statements and

Thanks in advance for your help.
So I'm working on a pressure sensor. The idea is that when the pressure sensor gets to a certain value, say 10 AND then decreases sharply after it has gotten to 10, activate the buzzer. (So it's like, I pump my car tire to 10 and I want to know if there is a leak)
I just for the life of me can not make this happen. I know I'm just missing something very small but I will greatly appreciate any help.
Below is my code:
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// These constants won't change: const int analogPin = A0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 5; // an arbitrary threshold level that's in the range of the analog input const int buzzer = 9; // pin that the buzzer is connected to.
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initilizes the pin as an output. pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
// initialize serial communications: Serial.begin(9600); lcd.begin(16, 2); lcd.print ("Pressure in kpa"); }
void loop() { // read the value of the pressure sensor: float sensorValue = analogRead(analogPin); // int kpa = (((sensorValue*5)/1024)*(120/5)-116); // 116 was subtracted to equilibrate the sensor int kpa = ((sensorValue * 5 / 1024) * (15 / 5) * (6.894 / 1) - 10); // // if the analog value is greater than whatever threshold we use , turn on the LED:
if (kpa > threshold )
{
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
tone(buzzer, 1000); // this controls the picth of the sound. (Hz)
delay(1000); } else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
noTone(buzzer); // Stop sound...
delay(1000); }
// print the analog value: Serial.println(kpa); delay(1); // delay in between reads for stability lcd.setCursor (0, 1); lcd.print (kpa);
}
Thanks.
I have edited the code to reflect what you are trying to do.
Essentially you need to check if your pressure is increasing or decreasing.
So for this you can take two readings and compare them; one reading is taken before the other. If the latest reading is higher than the previous then its going up, no cause for alarm.
If the latest reading is less that the previous then the pressure is going down. So, cause for alarm but not just yet...
TWO conditions need to be met before the alarm sounds, the pressure needs to be on a downward path AND the pressure needs to be below the threshold value.
kpa must be going down AND be below threshold:
(val < previous && val < threshold)
See if this works, sorry I rushed it a bit and haven't tested it at all:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 5; // an arbitrary threshold level that's in the range of the analog input
const int buzzer = 9; // pin that the buzzer is connected to.
int val = 0; // store value
int previous; // previous reading
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
Serial.begin(9600); // initialize serial communications:
lcd.begin(16, 2);
lcd.print ("Pressure in kpa");
}
void loop() {
float sensorValue = analogRead(analogPin); // read the value of the pressure sensor:
int kpa = ((sensorValue * 5 / 1024) * (15 / 5) * (6.894 / 1) - 10);
// we want to check if this is going up or down.
previous = val;
val = kpa;
if (val > previous) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, HIGH); // make LED flashy-flashy if its going up
delay(1000);
}
else if (val < previous && val < threshold) {
digitalWrite(ledPin, LOW); // ... and we switch off the LED
tone(buzzer, 1000); // WEE WAH WEE WAH WEE WAH
delay(1000);
}
}
I added a flashing LED to the code because flashing LEDs are just better.
I also used an else...if statement.
Hopefully this will work.
Cheers,
Ingwe.

Storing Last Reading in Arduino

Hello I am trying to write a sketch that reads the distance from an ultrasonic distance sensor and only sends the data if the previous and current readings are to different (20cm). Below is my sketch;
const int TRIG_PIN = 12;
const int ECHO_PIN = 11;
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
void setup() {
// The Trigger pin will tell the sensor to range find
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
// We'll use the serial monitor to view the sensor output
Serial.begin(9600);
BTserial.begin(9600);
}
void loop() {
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
float inches;
float lastcm;
float diff;
// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Wait for pulse on echo pin
while ( digitalRead(ECHO_PIN) == 0 );
// Measure how long the echo pin was held high (pulse width)
// Note: the () microscounter will overflow after ~70 min
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
// Calculate distance in centimeters and inches. The constants
// are found in the datasheet, and calculated from the assumed speed
//of sound in air at sea level (~340 m/s).
cm = pulse_width / 58.0;
diff = cm - lastcm;
lastcm = cm;
// Print out results
if ( pulse_width > MAX_DIST ) {
BTserial.write("Out of range");
lastcm = cm;
} else {
Serial.println(diff);
Serial.println("Or act value");
Serial.println(cm);
lastcm = cm;
if (abs(diff)>20) {
BTserial.println(cm);
}
}
// Wait at least 60ms before next measurement
delay(150);
}
However this is not correctly calculating the difference between the two values - as the serial monitor just returns
29.24
Or act value
29.24
29.31
Or act value
29.31
28.83
Or act value
28.83
Any thoughts?
Your variable 'lastcm' is declared inside the loop function. You have two options. Either declare it external to the loop or else declare it where you do but make it static (in either case you also have to deal with the initial invalid reading that it contains)
You could simplify this sketch and make it less prone to error by using the NewPing library:
http://playground.arduino.cc/Code/NewPing

Arduino Micro: SP02 Custom Sensor, IR & Red LEDS. Serial Output Inconsistant

thank you in advance for the community's help.
As of right now, we are looking to get consistent data to output to the serial monitor.
We are seeing output to the monitor several seconds after turning on the device, and sometimes the output takes more than 15 seconds. Furthermore, we are getting a oxogyn concentration of a value well above 100%.
There are times when we get a HR value, and a O2 value which seem reasonable, however, this is not always the case and rather inconsistent.
Is there something wrong with the timing of the interrupts or even the frequency in which we write to the serial monitor?
The code is as follows:
#include <LiquidCrystal.h> //Including this so we can test the code as is
//We can always remove the LCD code later
#include "floatToString.h" //SEE COMMENTS BELOW
/*
* You need to include the float to string header file. I am unsure if this goes in the
* main direcotry withe the code or just the library. For completness I have incouded it on my
* computer in both locations. Please do the same untill we can test further.
*/
#include "Arduino.h" //Formally floatToString.h This changed years ago, using Arduino.h
#include <SoftwareSerial.h> //bluetooth
#include <TimerOne.h> //interrupts SEE COMMENT BELOW
//**********************************************************
//Read Below..........................................
/*
* Need a library for the above timeerOne file
* without the library, you will get an error.
* See Github at https://github.com/PaulStoffregen/TimerOne
* Download the timerOne-master.zip file and place the entire
* TimerOne-master folder in my Documents > Arduino > libraries
*/
//**********************************************************
#define analogPin A0
#define Theta 0.6
#define RxD 1 //bluetooth read
#define TxD 0 //bluetooth transmit
#define DEBUG_ENABLED //bluetooth
#define redLED 5 //High is Red led on, Low is Infrared on.
//#define iredLED 6
volatile int maxTemp,minTemp; //shared variables between interrupts and loop
volatile int lastcount,count;
int Rmax, Rmin, IRmax, IRmin;
float R, Spo2,HeartR_frq;
int Sp02_int;
int HeartRate, HeartR;
float LastHeartRate = 70; //default
int average = 140; //average is average crossing
//int average2 = 220;
int interrupts_counter =0; //count the times of interrupts
float value=0.5;
float Spo2_float;
char buffer[25];
SoftwareSerial blueToothSerial(RxD,TxD); //define bluetooth
void setup() {
pinMode(redLED, OUTPUT); //define LED
pinMode(RxD, INPUT); //bluetooth input
pinMode(TxD, OUTPUT); //bluetooth output
pinMode(analogPin, INPUT); //analog signal input
//initially switch on Red LED, after each interrupt will turn on the other
digitalWrite(redLED, HIGH);
Serial.begin(115200);
//bluetooth
setupBlueToothConnection();
//We might not need this, this might be for the LCD display. Leaving it in for now
init_interrupts();
Timer1.initialize(40000); //terrupt every 0.04 seconds
Timer1.attachInterrupt(max_min_num); //interrupt call max_min_num function
Rmax = 0;
IRmax = 0;
Rmin = 0;
IRmin = 0;
LastHeartRate = 70;
}
void loop() {
//initialize LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // initialize LCD, the pins are all depends
lcd.begin(8, 2); //set up the LCD's number of columns and rows:
while(1){ //the whole while is used to avoid LCD reinitialize
digitalWrite(redLED,HIGH);
delay(2000); //let red led signal to be stable
//interrupts();
while(!((lastcount>average )&& (count<average)) ){ }
digitalWrite(redLED,HIGH);
init_interrupts();
while(!((lastcount>average )&& (count<average)) ){ }
noInterrupts(); // temporarily disabel interrupts, to be sure it will not change while we are reading
Rmax = maxTemp;
Rmin = minTemp;
delay(100);
HeartR_frq = 1/(0.04*interrupts_counter); //d is the times of ISR in 1 second,
interrupts(); //enable interrupts
HeartRate = HeartR_frq * 60;
if(HeartRate> 60 && HeartRate< 120){
HeartR = Theta*HeartRate + (1 - Theta)*LastHeartRate; //Use theta to smooth
LastHeartRate = HeartR;
}
digitalWrite(redLED, LOW);
//initialize lcd
lcd.setCursor(0,0);
lcd.print("HR:");
lcd.setCursor(5,0);
lcd.print(HeartR);
R = (Rmax - Rmin);
Spo2 = (R-180)*0.01 +97.838;
int Spo2_int = (int)Spo2; //float Spo2 to int Spo2_int
String Spo2_float = floatToString(buffer,Spo2,2);
lcd.setCursor(0,1);
lcd.print("SPO2:");
lcd.setCursor(5,1);
lcd.print(Spo2_int);
//transmit data to phone app through bluetooth
String H = String("H: ")+HeartR +String("S: ") +Spo2_float;
Serial.println(H);
delay(1000);
init_interrupts();
}
}
void setupBlueToothConnection() //initialize bluetooth
{
blueToothSerial.begin(115200); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //t the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=HC-06\r\n"); //t the bluetooth name as "HC-05"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
//blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
blueToothSerial.print("bluetooth connected!\n");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
void init_interrupts()
{
maxTemp = 0;
minTemp = 1023;
count = 0;
lastcount =0;
interrupts_counter = 0;
}
void max_min_num()
{
lastcount = count;
count = analogRead(analogPin); //read signa
if(count> maxTemp){
maxTemp = count;
}
else if(count<minTemp){
minTemp = count;
}
interrupts_counter++; //interrupt counter
}
Thank you in advance for your time!

Arduinosumo bot with rotatory motor

I am making a sumo bot for a project by using an Arduino. I have no experience with Arduino code, but I do have java experience. That said this bot has an edge detector to prevent it from falling off, a L298 driver, and so forth.
With my lack of experience, I don't know exactly how Arduino's code works with methods and etc. That said, my main question is how is it executing my methods with the delays? Is it getting stuck in the methods with no return? Not entering? Executing be ending later? Currently it seems to just run the motors forward without the switch on or off. I have also checked the wiring. ( I apologize for poor questioning- stackedoverflow virgin).
int motor_forward = 10;
int motor_reverse = 9;
int motor2_forward = 13;
int motor2_reverse = 12;
int edgeDec1 = 7;
//int edgeDec2 = 6;
//the setup routine runs once when you press reset;
void setup(){
//initialize the digital pin as an output.
pinMode(motor_forward, OUTPUT);
pinMode(motor_reverse,OUTPUT);
pinMode(motor2_forward,OUTPUT);
pinMode(motor2_reverse,OUTPUT);
pinMode(edgeDec1,INPUT);
}
//the loop routine runs over and over again forever
void loop(){
int indicator = random(2);
delay(5000);//5 second delay
//loop to prevent another 5 second delay
while(true){
if (indicator = 0){
for(int x = 0; x < random(200); x++){
Forward();
EdgeDec();
delay(10);
}
}
else if ( indicator = 1){
for(int x = 0; x < random(200); x++){
TurnLeft();
EdgeDec();
delay(10);
}
}
else if ( indicator = 2){
for(int x = 0; x < random(200); x++){
TurnRight();
EdgeDec();
delay(10);
}
}
}
}
void Forward(){
//right motor
digitalWrite(motor_forward,1);//terminal d1 will be high
digitalWrite(motor_reverse,0);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,1);//terminal d1 will be high
digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
//going in reverse
void Reverse(){
//right motor
digitalWrite(motor_forward,0);//terminal d1 will be low
digitalWrite(motor_reverse,1);//terminal d2 will be high
//left motor
digitalWrite(motor2_forward,0);//terminal d1 will be low
digitalWrite(motor2_reverse,1);//terminal d2 will be high
}
//rotating left
void TurnLeft(){
//right motor
digitalWrite(motor_forward,0);//terminal d1 will be high
digitalWrite(motor_reverse,1);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,1);//terminal d1 will be high
digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
void TurnRight(){
//right motor
digitalWrite(motor_forward,1);//terminal d1 will be high
digitalWrite(motor_reverse,0);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,0);//terminal d1 will be high
digitalWrite(motor2_reverse,1);//terminal d2 will be low
}
void EdgeDec(){
if(edgeDec1 == 1){
Reverse();
delay(700);
TurnLeft();
delay(1000);
Forward();
}
}
You have a couple of errors/misunderstandings in your code, so I've added a bit of simple debugging that works by opening Tools>Serial Monitor after you've downloaded your code. In your EdgeDec() function (function = Java method) you had if(EdgeDec = 1), but EdgeDec is just an int with the value of 7. What you wanted was to read the value of a pin numbered 7 - if(digitalRead(edgeDec1) == LOW).
also, I reversed your logic from HIGH (1) to LOW (0) because you hadn't tied down the digitalRead pins either LOW (using external resistors) or HIGH (using the Arduino's internal resistors) - read my comments in the code. Not sure what you were doing with the while(true) loop - maybe some further debugging? Anyway, hope it helps...
int motor_forward = 10;
int motor_reverse = 9;
int motor2_forward = 13;
int motor2_reverse = 12;
int edgeDec1 = 7;
//int edgeDec2 = 6;
int indicator; // pulled this out of your loop - askchipbug
String repeatstring; //debugging variable, stops a debug string from repeating in Serial monitor when watching a loop - askchipbug
//the setup routine runs once when you press reset;
void setup(){
Serial.begin(9600); // set up Serial library at 9600 bps, using SerialMonitor to debug - askchipbug
//initialize the digital pin as an output.
pinMode(motor_forward, OUTPUT);
pinMode(motor_reverse,OUTPUT);
pinMode(motor2_forward,OUTPUT);
pinMode(motor2_reverse,OUTPUT);
pinMode(edgeDec1,INPUT_PULLUP); // set this high so it doesn't float about - askchipbug
// you can use the internal 20k pullups with INPUT_PULLUP which means 1 = off, 0 = on
// or you have to use external pulldown resistors to have 1 = on, 0 = off
pr("setup completed"); // askchipbug
}
//the loop routine runs over and over again forever
void loop(){
randomSeed(analogRead(0)); // seeds the random() function with a different number each time the loop runs - askchipbug
indicator = random(2);
pr("indicator: " + String(indicator)); // askchipbug
pr("5 second delay");
delay(5000);//5 second delay
//loop to prevent another 5 second delay
while(true){
if (indicator == 0){ //you had this set as indicator = 0 - askchipbug
pr("indicator: " + String(indicator)); // askchipbug
for(int x = 0; x < random(200); x++){
Forward();
EdgeDec();
delay(10);
}
}
else if ( indicator = 1){
pr("indicator: " + String(indicator)); //askchipbug
for(int x = 0; x < random(200); x++){
TurnLeft();
EdgeDec();
delay(10);
}
}
else if ( indicator = 2){
pr("indicator: " + String(indicator));// askchipbug
for(int x = 0; x < random(200); x++){
TurnRight();
EdgeDec();
delay(10);
}
}
}
}
void Forward(){
//right motor
pr("forward"); //askchipbug
digitalWrite(motor_forward,1);//terminal d1 will be high
digitalWrite(motor_reverse,0);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,1);//terminal d1 will be high
digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
//going in reverse
void Reverse(){
pr("reverse"); //askchipbug
//right motor
digitalWrite(motor_forward,0);//terminal d1 will be low
digitalWrite(motor_reverse,1);//terminal d2 will be high
//left motor
digitalWrite(motor2_forward,0);//terminal d1 will be low
digitalWrite(motor2_reverse,1);//terminal d2 will be high
}
//rotating left
void TurnLeft(){
pr("turn left"); //askchipbug
//right motor
digitalWrite(motor_forward,0);//terminal d1 will be high
digitalWrite(motor_reverse,1);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,1);//terminal d1 will be high
digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
void TurnRight(){
pr("turn right"); //askchipbug
//right motor
digitalWrite(motor_forward,1);//terminal d1 will be high
digitalWrite(motor_reverse,0);//terminal d2 will be low
//left motor
digitalWrite(motor2_forward,0);//terminal d1 will be high
digitalWrite(motor2_reverse,1);//terminal d2 will be low
}
void EdgeDec(){
pr("edge detector: " + String(digitalRead(edgeDec1))); // read pin 7 - askchipbug
if(digitalRead(edgeDec1) == LOW){ // remember we're using the internal pullups so LOW = on - askchipbug
pr("edge detected!"); // askchipbug
Reverse();
delay(700);
TurnLeft();
delay(1000);
Forward();
}
}
//simple debug technique - use pr("something"); in your code - askchipbug
void pr(String txt){
if(repeatstring != txt){
//if the debug text is different, print it
Serial.println(txt); //prints the text and adds a newline
Serial.flush(); //waits for all the data to be printed
delay(1000); //just pauses the scrolling text for 1 second, make bigger if you want a longer pause
repeatstring = txt;
}
}
The delay method in Arduino generally speaking is implemented as just executing a number of nops on the CPU for enough cycles to make up the required delay time. It's essentially a blocking call in that it just completely uses up the CPU for enough time as specified by the argument passed to delay.
For example lets say that your processor is running at 100hz then essentially to delay for 1 second would involve generating 100 nop instructions that the processor would execute. The presumes of course that such an instruction takes exactly 1 clock cycle. Given that the Arduino Uno runs off an ATMEGA328p microprocessor you would want to look at that datasheet to find out more.

Strange issue trying fade on arduino on multiple LEDs

I am trying to test out Fade on multiple LEDs on an Arduino Uno. Here's the code i have written
int led[2] = {9,10}; // the pin that the LED is attached to
int brightness[2] = {0,0}; // how bright the LED is
int fadeAmount[2] = {5,15}; // how many points to fade the LED by
long previousMillis[2] = {0,0}; // will store last time LED was updated
long interval[2] = {30, 50};
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pins:
for (int counter = 0; counter < 2; counter++) {
unsigned long currentMillis = millis();
analogWrite(led[counter], brightness[counter]);
Serial.print("LED ");
Serial.print(led[counter]);
Serial.print(": Brightness ");
Serial.println(brightness[counter]);
if (currentMillis - previousMillis[counter] > interval[counter]) {
// change the brightness for next time through the loop:
brightness[counter] = brightness[counter] + fadeAmount[counter];
// reverse the direction of the fading at the ends of the fade:
if (brightness[counter] == 0 || brightness[counter] == 255) {
fadeAmount[counter] = -fadeAmount[counter] ;
}
}
}
}
Here's the weird thing. If i comment out the Serial stuff (print and begin), the fades don't work. They just "flicker" a little.
Any idea what is wrong?
I figured it out. It looks like I forgot a line of code.
previousMillis[counter] = currentMillis;
I should have put that in on the first if statement in the code. I guess the act of observing by using the serial print slowed things down enough that the code seemed to work.
Boy, do i feel foolish.