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 25: Python, Graphing, 3D Visualization and Inertial Measurement

Python Arduino Code int trigPin=13; //Sensor Trig pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pin...

Day 19: Project 2

Our project was a test stand for rocket motors Using an LCD screen, we could choose a display mode, and it would display the selected value. Unfortunately, we were unable to calibrate the pressure sensor, so all we ended up with was a load cell. We used the HX711 library for this. The LCD screen displayed the maximum force experienced during the test Code for MotorSensors.h #ifndef _MOTORSENSORS_ #define _MOTORSENSORS_ #include <LiquidCrystal.h> //Include Liquid Crystal Library #include <HX711.h> //Include HX711 Load Cell library #include "HX711.h" //Include Load Cell header file #include <math.h> //Include math library #define calibration_factor -8815 //set calibration factor #define DAT 3 //define DAT pin #define CLK 2 //define CLK pin #define pressurePin A1 #define tempPin A2 HX711 scale(DAT,CLK);  LiquidCrystal lcd(8,9,4,5,6,7); //Define LCD pins //Initialize variables float maxkg=0;  float currentval=0; float maxforce=0; char mo...