Wednesday, February 10, 2016

My first Arduino examples

1. Light sensor to control the brightness of an LED

int led = 4;           // the PWM pin the LED is attached to
int brightness = 0;

void setup() {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {

  // read the input on analog pin 0:
  int sensorValue = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to (0 - 255)
  brightness = 255 - sensorValue * 1.06;
  // print out the value you read:
  Serial.println(brightness);
  analogWrite(led, brightness);
}





2. Keyboard to control LEDs. Send 1 to turn on red LED and 2 to turn on green LED

const int redPin = 2;
const int greenPin = 3;
int x = 4;

void setup() {

  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {

  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in incoming serial stream

    x = Serial.parseInt();
    if (x == 1) {
      digitalWrite(redPin, HIGH);
      digitalWrite(greenPin, LOW);
    }
    else if (x == 2) {
      digitalWrite(greenPin, HIGH);
      digitalWrite(redPin, LOW);
    }

    Serial.print(x);

  }
}





3. Keyboard to control the brightness of an LED. Send brightness value (0-255) from keyboard to control the LED.


// pin for the LED, make sure you use a PWM~ pin
const int ledPin = 6;
int x = 444;

void setup() {

  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(ledPin, OUTPUT);
}

void loop() {

  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    Serial.println("Enter the brightness of LED between 0 and 255");
    // look for the next valid integer in the incoming serial stream:
    x = Serial.parseInt();
    Serial.println(x);
    analogWrite(ledPin, constrain(x, 0, 255));  
  }
}


Loneliness

You can check your Loneliness level using the Loneliness Scale UCLA (psytests.org) If you are   dealing with feelings of loneliness   that j...