Monday, October 20, 2014

//This is an updated code for the light that lights up when pressed
//And dims when not pressed. The last code turned it on and off
//completely, whereas this one can move between 0-250 whenever the
//button is pressed or released without having to to to a max.
//Includes "Serial.println's" for debugging.
int led=3;
int val;
int buttonstate;
int fadeValue;
int switchpin=2;
int Val2;

void setup()
{
  pinMode(switchpin,INPUT);
  Serial.begin(9600);
  buttonstate=digitalRead(switchpin);
}
void loop()
{
  val=digitalRead(switchpin);
  Serial.println("AAAAAAAAAAAAA");
  if(val!=buttonstate)
  {
    if(val==HIGH)
    {
      Serial.println("INCREASING");
      Val2=1;
      delay(70);
    }
    else
    {
      Serial.println("DECREASING");
      Val2=2;
      delay(70);
    }
  }
  if(Val2==1)
  {
    if(fadeValue<=250)
    {
    fadeValue=fadeValue+5;
    }
  }
  if(Val2==2)
  {
    if(fadeValue>=5)
    {
    fadeValue=fadeValue-5;
    }
  }
  analogWrite(led, fadeValue);
  Serial.println(fadeValue);
  buttonstate=val;
}
   

Thursday, October 16, 2014

//A simple code that brightens and dims an Led.
int brightness=0;
int fadeamount=5;

void setup(){
  pinMode(3, OUTPUT);
}

void loop(){
  analogWrite(3, brightness);
  brightness=brightness+fadeamount;
 
  if(brightness==0||brightness==255){
    fadeamount=-fadeamount;
  }

  delay(30);
}

//Unlike the other dimming code, you cannot control
//the individual speed of dimming and increasing of brightness
//you have 1 speed for both, which is the delay
//This code is a code for a true togglable Led. Where when you turn it on, it stays //on until you press it again.
int switchpin=2;
int val;
int buttonstate;
int led=7;
int ledstate=0;

void setup(){
  pinMode(switchpin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  buttonstate=digitalRead(switchpin);
}

void loop(){
  val=digitalRead(switchpin);
  if(val !=buttonstate){

    if (val==HIGH){

      if(ledstate==0){

        Serial.println("BUTTON PRESSED");
        digitalWrite(led, HIGH);
        ledstate=1;
      }

      else{
        Serial.println("BUTTON RELEASED");
        digitalWrite(led, LOW);
        ledstate=0;
      }
    }
  }
  buttonstate=val;
}
//This is a code for a turn signal button which uses a variable system.
int switchpin=2;
int val;
int buttonstate;
int led=9;
int ledstate=0;
int val2;
void setup(){
  pinMode(switchpin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  buttonstate=digitalRead(switchpin);
}

void loop(){
  Serial.println(ledstate);
  val=digitalRead(switchpin);
 
  if(val !=buttonstate){

    if (val==HIGH){
      if(ledstate<=2){
        ledstate++;
    }}
      else{
      }}
      buttonstate=val;
 
  if(ledstate==1){
    for(int led=9; led>=7; led=led-1){
      pinMode(led, OUTPUT);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
    }
  }
  if(ledstate==0){
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
  }
  if(ledstate==2){
    digitalWrite(led, HIGH);
    delay(80);
    digitalWrite(led, LOW);
    delay(80);
  }
  if(ledstate>2){
  ledstate=0;}
}
int led;
int val;
int buttonstate;
int switchpin=2;
int ledstate;
int time=500;
int LED=11;
int LED2=10;
int LED3=9;
int LED4=8;
int LED5=7;
int level=0;
void setup(){
  pinMode(switchpin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  buttonstate=digitalRead(switchpin);
  

}

void loop(){
code();

buttonstate=val;
   
    if(ledstate=0){
      digitalWrite(led, LOW);
    }
   if (led==4)
   {delay (1000);
   time=time-40;
   level++;
   Serial.println(level);}
  
   if(led==0){
   level=0;
   Serial.println("You Lost");
   digitalWrite(LED,HIGH);
   digitalWrite(LED2, HIGH);
   digitalWrite(LED3, HIGH);
   digitalWrite(LED4, HIGH);
   digitalWrite(LED5, HIGH);
   delay(300);
   digitalWrite(LED,LOW);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, LOW);
   digitalWrite(LED4, LOW);
   digitalWrite(LED5, LOW);
   delay(500);
   digitalWrite(LED,HIGH);
   digitalWrite(LED2, HIGH);
   digitalWrite(LED3, HIGH);
   digitalWrite(LED4, HIGH);
   digitalWrite(LED5, HIGH);
   delay(500);
   digitalWrite(LED,LOW);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, LOW);
   digitalWrite(LED4, LOW);
   digitalWrite(LED5, LOW);
   delay(1000);
   time=500;
  
   }
}



 void code()
 {
   for(led=11; led>6;led=led-1){
    pinMode(led, OUTPUT);
    digitalWrite(led, HIGH);
    delay(time);
    digitalWrite(led, LOW);
 //   Serial.println(buttonstate);
    val=digitalRead(switchpin);
    if(val!=buttonstate){
      if (val==HIGH){
        if (led==10){
       digitalWrite(led, HIGH);
       led=5;
   
  } else{
        led=1;}}
        }
    } 
  }
 
// This is my code for a game called "catch the light". A set of Led's light up in a sequence, one led being a different color from the rest. One tries to push the button when the different colored led is lit. If you "catch" it, it stays on for a second, then the lights start moving in sequence again, but speed up, untill you press the button at the wrong time, in which case, all the lights flash on and off, and the speed resets to the speed at the beginning. It also keeps score of how many times you pressed the button.

Tuesday, September 16, 2014

Here is the code for assignment 5.1, to make a code that finds a side of a right triangle that is not the hypotenuse. Hypotenuse length is integer a, the length of another side is integer b.

#include "math.h"
int a=10;
int b=6;
int h;
void setup()

{ Serial.begin(9600);
  Serial.println("Dis Iz Maf");
  h=sqrt(a*a-b*b);
  Serial.print("h=");
  Serial.println(h);

}
void loop()
{
}

Friday, September 12, 2014

int led=7;
int LED=3;

void setup() {               
  // initialize the digital pin as an output.

  for (int led=7; led>=3; led=led-1)
  {
    pinMode(led,OUTPUT);


  }

}
// the loop routine runs over and over again forever:
void loop()
{
  for(int led=7; led>3; led=led-1)
  {
    digitalWrite(led, HIGH);
    digitalWrite(LED, HIGH);
    delay (200);        
    digitalWrite(led, LOW);
    digitalWrite(LED, LOW);
    LED=LED+1;
      if(LED>=7)
    {
      LED=3;
    }
 
  }


This is the code that causes a system of 5 LEDs to turn on the outside 2 LEDs, then from both sides move to the middle LED and back to the outside LEDs.

Tuesday, September 9, 2014

This is my code for a system where five lights flash in a sequence from first to last, then back to first. Here is the code.

int led = 7;
int led2 = 1;
int led3 = 2;
int led4 = 3;
int led5 = 4;
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT); 
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);
  delay (20); // turn the LED on (HIGH is the voltage level)              // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 
  digitalWrite(led2, HIGH);
  delay (70);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  delay (70);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  delay (70);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  delay (70);
  digitalWrite(led5, LOW);
  digitalWrite(led4, HIGH);
  delay (70);
  digitalWrite(led4, LOW);
  digitalWrite(led3, HIGH);
  delay (70);
  digitalWrite(led3, LOW);
  digitalWrite(led2, HIGH);
  delay (70);
  digitalWrite(led2, LOW);


Thursday, September 4, 2014

Arduino Blink Program Sketch

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1500);               // wait for a second
}





This sketch produces an L.E.D that turns on for 25 milliseconds even 1.5 seconds. The delay on the digitalWright (led, HIGH) controls how long the led remains on (in milliseconds). For (led, LOW), the delay measures how long the led remains off (in milliseconds)

Wednesday, August 27, 2014

For Electronics 4th hour:

Easy: 24x6 L.E.D. Matrix
         Arduino Hitcounter

Medium: Wii Nunchuck adapter
               Secret Knock-detecting door-lock

Hard: Self balancing skateboard
          RC Lawnmower