Skip to main content

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight

const int red=11;
const int green=10;
const int blue=9;
const int button=4;
boolean lastbutton=LOW;
boolean currentbutton=LOW;
int ledMode=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(button,INPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);

}

boolean debounce(boolean last)
{
  boolean current=digitalRead(button);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(button);
  }
  return current;
}
void setMode(int mode)
{
  //RED
  if(mode==1)
  {
    digitalWrite(red,HIGH);
    digitalWrite(green,LOW);
    digitalWrite(blue,LOW);
  }
  if(mode==2)
  {
    //orange
    digitalWrite(red,HIGH);
    analogWrite(green,40);
    digitalWrite(blue,LOW);
  }
  if(mode==3)
  {
    //yellow
    digitalWrite(red,HIGH);
    analogWrite(green,100);
    digitalWrite(blue,LOW);
  }
  if(mode==4)
  {
    //green
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
    digitalWrite(blue,LOW);
  }
  if(mode==5)
  {
    //teal
    digitalWrite(red,LOW);
    analogWrite(green, 255);
    analogWrite(blue,50);
  }
  if(mode==6)
  {
    //blue
    digitalWrite(red,LOW);
    analogWrite(green,100);
    digitalWrite(blue,HIGH);
  }
  if(mode==7)
  {
    //purple
    analogWrite(red,180);
    digitalWrite(green,LOW);
    digitalWrite(blue,HIGH);
  }
  if(mode==8)
  {
    //white
    analogWrite(red,255);
    analogWrite(green,255);
    analogWrite(blue,255);
  }
  if(mode==9)
  {
    digitalWrite(red,LOW);
    digitalWrite(green,LOW);
    digitalWrite(blue,LOW);
  }
}
void loop() {
  // put your main code here, to run repeatedly:
  currentbutton=debounce(lastbutton);
  if(lastbutton==LOW && currentbutton==HIGH)
  {
    ledMode++;
  }
  lastbutton=currentbutton;
  if(ledMode==10) 
  {
    ledMode=0;
  }
  setMode(ledMode);
}


const int red=11;

const int green=10;
const int blue=9;
const int button1=4;
const int button2=3;
const int button3=2;
boolean lastbutton=LOW;
boolean currentbutton=LOW;
int mode=1;
void setup() {
  // put your setup code here, to run once:
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current=digitalRead(button1);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(button1);
  }
  return current;
}
void loop() {
  // put your main code here, to run repeatedly:
  currentbutton=debounce(lastbutton);
  if(lastbutton==LOW && currentbutton==HIGH)
  {
    mode++;
  }
  lastbutton=currentbutton;
  if(mode==10)
  {
    mode=1;
  }
  switch (mode)
  {
    case 1: //off
      digitalWrite(red,LOW);
      digitalWrite(green,LOW);
      digitalWrite(blue,LOW);
    break;
    
    case 2: //red
      digitalWrite(red,HIGH);
      digitalWrite(green,LOW);
      digitalWrite(blue,LOW);
    break;
    
    case 3: //orange
      digitalWrite(red,HIGH);
      analogWrite(green,40);
      digitalWrite(blue,LOW);
    break;
    
    case 4: //yellow
      digitalWrite(red,HIGH);
      analogWrite(green,100);
      digitalWrite(blue,LOW);
    break;
    
    case 5: //green
      digitalWrite(red,LOW);
      digitalWrite(green,HIGH);
      digitalWrite(blue,LOW);
    break;

    case 6: //teal
      digitalWrite(red,LOW);
      analogWrite(green, 255);
      analogWrite(blue,50);
    break;

    case 7: //blue
      digitalWrite(red,LOW);
      analogWrite(green,100);
      digitalWrite(blue,HIGH);
    break;

    case 8: //purple
      analogWrite(red,180);
      digitalWrite(green,LOW);
      digitalWrite(blue,HIGH);
    break;

    case 9: //white
      analogWrite(red,255);
      analogWrite(green,255);
      analogWrite(blue,255);
    break;
  }
}

const int red=11;
const int green=10;
const int blue=9;
const int button1=4;
const int button2=3;
const int button3=2;
boolean lastbutton1=LOW;
boolean currentbutton1=LOW;
boolean lastbutton2=LOW;
boolean currentbutton2=LOW;
boolean lastbutton3=LOW;
boolean currentbutton3=LOW;
int mode1=1;
int mode2=1;
int mode3=1;
void setup() {
  // put your setup code here, to run once:
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);
  digitalWrite(red,LOW);
  digitalWrite(green,LOW);
  digitalWrite(blue,LOW);
}
boolean debounce1(boolean last)
{
  boolean current=digitalRead(button1);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(button1);
  }
  return current;
}

boolean debounce2(boolean last)
{
  boolean current=digitalRead(button2);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(button2);
  }
  return current;
}

boolean debounce3(boolean last)
{
  boolean current=digitalRead(button3);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(button3);
  }
  return current;
}
void loop() {
  // put your main code here, to run repeatedly:
  
  currentbutton1=debounce1(lastbutton1);
  if(lastbutton1==LOW && currentbutton1==HIGH)
  {
    mode1++;
  }
  lastbutton1=currentbutton1;
  if(mode1==8)
  {
    mode1=1;
  }
  switch(mode1)
  {
    case 1: //off
      digitalWrite(red,LOW);
    break;

    case 2: //low
      analogWrite(red,40);
    break;

    case 3:
      analogWrite(red,85);
    break;

    case 4: 
      analogWrite(red,127);
    break;

    case 5:
      analogWrite(red,170);
    break;

    case 6:
      analogWrite(red,230);
    break;

    case 7:
      analogWrite(red,255);
    break;
  }
  
  currentbutton2=debounce2(lastbutton2);
  if(lastbutton2==LOW && currentbutton2==HIGH)
  {
    mode2++;
  }
  lastbutton2=currentbutton2;
  if(mode2==8)
  {
    mode2=1;
  }
  
  switch(mode2)
  {
    case 1: //off
      digitalWrite(green,LOW);
    break;

    case 2: //low
      analogWrite(green,40);
    break;

    case 3:
      analogWrite(green,85);
    break;

    case 4: 
      analogWrite(green,127);
    break;

    case 5:
      analogWrite(green,170);
    break;

    case 6:
      analogWrite(green,230);
    break;

    case 7:
      analogWrite(green,255);
    break;
  }
  
  currentbutton3=debounce3(lastbutton3);
  if(lastbutton3==LOW && currentbutton3==HIGH)
  {
    mode3++;
  }
  lastbutton3=currentbutton3;
  if(mode3==8)
  {
    mode3=1;
  }
  switch(mode3)
  {
    case 1: //off
      digitalWrite(blue,LOW);
    break;

    case 2: //low
      analogWrite(blue,40);
    break;

    case 3:
      analogWrite(blue,85);
    break;

    case 4: 
      analogWrite(blue,127);
    break;

    case 5:
      analogWrite(blue,170);
    break;

    case 6:
      analogWrite(blue,230);
    break;

    case 7:
      analogWrite(blue,255);
    break;
  }
}

Programming: Algorithm Development, Conditional Expressions, Selection Statements

Algorithm Development

Top-down design presents big picture description of problem solution in sequential steps

Decomposition Outline

Pseudocode: Uses English-like statements to describe steps in algorithm
Flowchart: Uses diagram to describe steps in algorithm

Structured Programming

Repetition structures: contain set of steps repeated as long as condition is true
Sequence: steps performed one after another
Selection: Selection structure contains condition that can be evaluated as either true or false. Execution of statements is dependent on whether condition is true or false. 

Homework

2. Give the value of a after the following set of statements is executed. int a = 750; ... if (a>0) if (a >= 1000) a = 0; else if (a < 500) a *= 2; else a *= 10; else a += 3; 

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 6: Analog Sensors | Functions and Modularity

Functions Functions are sets of statements that typically perform operation or compute value. They can help to make programs more accessible and usable for non-programmers e.g. create function that allows user to type "go forward" and move a robot forward. Modules -Functions can be split up into modules, "divide and conquer" -Each module has specific purpose, can be written and tested separately -Smaller than complete solution, therefore testing is easier -Can be used in new problem solutions without being retested -Reduces overall length of program -Allows for increased collaboration; modules can be worked on in parallel Debugging Longer Programs Use a compiler that gives meaningful information about errors. Adding comments around some sections of code can allow for better focus on other parts of the program. Test complicated functions by themselves. Programmer Defined Functions Execution of program always begins with main function. Additional ...

Day 3: Linear Interpolation, Mathematical Functions, and Arduino Buttons

Figure 1.1 Linear interpolation  assumes that a straight line joins two points f(a) and f(c), and that the value of f(b), where b lies between a and c, lies on this line. Figure 1.2 Cubic spline interpolation  is when the points f(a) and f(c) are joined by a cubic polynomial, and the value of f(b), a function of b, which lies between points a and c, lies on this curve. Formula Assumes a<b<c f(b)=f(a)+((b-a)/(c-a))[f(c)-f(a)] Example 1 The data was given, shown in Figure 1.1 at right. Figure 2.1 Example 2 In the example on the left, we used linear interpolation to find the freezing temperature of certain salinities of water. 1. Problem Statement Use linear interpolation to determine the freezing temperature of water with a certain salinity. 2. Input/Output Description Inputs: first salinity, second salinity, first freezing temperature, second freezing temperature, new salinity. Output: new freezing temperature 3. Hand Example See...