HW 4
Value of given statement set:
#include <stdio.h>
#include <math.h>
int main(void)
{
int a = 750;
if (a>0)
{
if (a >= 1000)
{
a = 0;
}
else
if (a < 500)
{
a *= 2;
}
else
a *= 10;
}
else
a += 3;
printf("%i", a);
return 0;
}
a = 7500
RBG LED
KNOWN WORKING:
const int BLED = 9; //Blue LED on Pin 9
const int GLED = 10; //Green LED on Pin 10
const int RLED = 11; //Red LED on Pin 11
const int BUTTON = 2; //The Button is connected to pin 2
const int RCONT = 3;
const int GCONT = 4; //Inputs for controlling analogWrite colors
const int BCONT = 5;
int count = 0;
int R = 0;
int G = 0; //Control analogWrite using int of colors so can increment easily
int B = 0;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
int D = 500;
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
Debouncing Function Pass it the previous button state,
and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
/*
LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
switch (mode)
{
//RED
case 1:
R = 254;
G = 0;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//GREEN
case 2:
R = 0;
G = 254;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//BLUE
case 3:
R = 0;
G = 0;
B = 254;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 5:
R = 254;
G = 254;
B = 254;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 6:
R = 0;
G = 100;
B = 127;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 7:
R = 254;
G = 100;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//PURPLE (RED+BLUE)
case 4:
R = 127;
G = 0;
B = 127;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//OFF (mode = 0)
default:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
for (count = 0; count < 100; count ++)
{
if (count > 90)
{
lastButton = currentButton;
count = 0;
do
{
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
break;
}
else
{
delay(5);
setMode(0);
delay(90);
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
delay(85);
}
} while (1 == 1);
goto stop;
}
else
{
delay(4);
if (digitalRead(BUTTON) != HIGH) break;
}
}
ledMode++; //increment the LED value
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
stop:
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (digitalRead(RCONT) == HIGH)
{
R++;
delay(8);
analogWrite(RLED, R);
}
if (digitalRead(GCONT) == HIGH)
{
G++;
delay(8);
analogWrite(GLED, G);
}
if (digitalRead(BCONT) == HIGH)
{
B++;
delay(8);
analogWrite(BLED, B);
}
}
button control:
input
if statement (switch statement?)
check input == HIGH
increment +1
set 255 = 0
blink:
input
if statement (switch statement?)
check button state
if yes:
endless for loop
check button state per loop
cease mode increment
else:
the usual code
setup:
3 buttons for rgb,
1 button, either change color or set blink mode?
time delay, hold for some time to turn blink mode
else increment color mode
#include <stdio.h>
#include <math.h>
int main(void)
{
int a = 750;
if (a>0)
{
if (a >= 1000)
{
a = 0;
}
else
if (a < 500)
{
a *= 2;
}
else
a *= 10;
}
else
a += 3;
printf("%i", a);
return 0;
}
a = 7500
RBG LED
KNOWN WORKING:
const int BLED = 9; //Blue LED on Pin 9
const int GLED = 10; //Green LED on Pin 10
const int RLED = 11; //Red LED on Pin 11
const int BUTTON = 2; //The Button is connected to pin 2
const int RCONT = 3;
const int GCONT = 4; //Inputs for controlling analogWrite colors
const int BCONT = 5;
int count = 0;
int R = 0;
int G = 0; //Control analogWrite using int of colors so can increment easily
int B = 0;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
int D = 500;
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
Debouncing Function Pass it the previous button state,
and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
/*
LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
switch (mode)
{
//RED
case 1:
R = 254;
G = 0;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//GREEN
case 2:
R = 0;
G = 254;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//BLUE
case 3:
R = 0;
G = 0;
B = 254;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 5:
R = 254;
G = 254;
B = 254;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 6:
R = 0;
G = 100;
B = 127;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
case 7:
R = 254;
G = 100;
B = 0;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//PURPLE (RED+BLUE)
case 4:
R = 127;
G = 0;
B = 127;
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
break;
//OFF (mode = 0)
default:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
for (count = 0; count < 100; count ++)
{
if (count > 90)
{
lastButton = currentButton;
count = 0;
do
{
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
break;
}
else
{
delay(5);
setMode(0);
delay(90);
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
delay(85);
}
} while (1 == 1);
goto stop;
}
else
{
delay(4);
if (digitalRead(BUTTON) != HIGH) break;
}
}
ledMode++; //increment the LED value
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
stop:
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (digitalRead(RCONT) == HIGH)
{
R++;
delay(8);
analogWrite(RLED, R);
}
if (digitalRead(GCONT) == HIGH)
{
G++;
delay(8);
analogWrite(GLED, G);
}
if (digitalRead(BCONT) == HIGH)
{
B++;
delay(8);
analogWrite(BLED, B);
}
}
button control:
input
if statement (switch statement?)
check input == HIGH
increment +1
set 255 = 0
blink:
input
if statement (switch statement?)
check button state
if yes:
endless for loop
check button state per loop
cease mode increment
else:
the usual code
setup:
3 buttons for rgb,
1 button, either change color or set blink mode?
time delay, hold for some time to turn blink mode
else increment color mode
Comments
Post a Comment