segunda-feira, 19 de maio de 2014

Arduino Circuits


Ultrasonic sensor


The following code outputs the distance:
  1. #define echoPin 11 // Echo Pin
  2. #define trigPin 12 // Trigger Pin
  3. long duration, distance; // Duration used to calculate distance
  4. void setup() {
  5.  Serial.begin (9600);
  6.  pinMode(trigPin, OUTPUT);
  7.  pinMode(echoPin, INPUT);
  8. }
  9. void loop() {
  10.  digitalWrite(trigPin, LOW);
  11.  delayMicroseconds(2);
  12.  digitalWrite(trigPin, HIGH);
  13.  delayMicroseconds(10);
  14.  digitalWrite(trigPin, LOW);
  15.  duration = pulseIn(echoPin, HIGH);
  16.  //Calculate the distance (in cm) based on the speed of sound.
  17.  distance = duration/58.2;
  18.  Serial.println(distance);
  19.  delay(50);
  20. }

Photoresistor controlling LED

With a photo resistor you can control something from your Arduino board depending on light intake. The following circuit shows how to connect your photo resistor to the Arduino board and read the values.

  1. int ledPin = 3;
  2. int photocellInput = 0;
  3. void setup()  {
  4.   pinMode(ledPin, OUTPUT);
  5. }
  6. void loop()  {
  7.   photocellInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  8.   analogWrite(ledPin, photocellInput);  
  9.   // The delay can be change to get the desired dimming effect
  10.   delay(20);                            
  11. }

Decision maker with LEDs

The following circuit and code shows an example of a decision maker made with LEDs. Pushing the button will make the LEDs flash in a random order for 1 second. Then displaying the random decision where one LED lights up for 3 seconds and then reseting.

  1. int timeShowRandom = 1000;
  2. int timeShowDecision = 3000;
  3. int timeBlink = 50;
  4. int buttonPin = 3;
  5. int buttonPress = false;
  6. int randomNumber;
  7. int previousNo = 0;
  8. int timePassed = 0;
  9. void setup() {    
  10.   // Set button pin
  11.   pinMode(buttonPin, INPUT);
  12.   // Set output pins  
  13.   pinMode(12, OUTPUT);
  14.   pinMode(11, OUTPUT);
  15.   pinMode(10, OUTPUT);
  16. }
  17. void getRandomNo() {
  18.   int rand = random(10,13);
  19.   if(rand == previousNo) {
  20.     getRandomNo();
  21.   } else {
  22.     randomNumber = rand;
  23.     previousNo = randomNumber;
  24.   }
  25. }
  26. void loop() {
  27.   // Check if button is pressed
  28.   if(digitalRead(buttonPin) == HIGH && buttonPress == false) {
  29.      buttonPress = true;
  30.   } if(buttonPress == true && timePassed <= timeShowRandom) {
  31.     getRandomNo(); // Get random pin number
  32.     digitalWrite(randomNumber, HIGH);
  33.     delay(timeBlink);  
  34.     digitalWrite(randomNumber, LOW);
  35.     delay(timeBlink);  
  36.     timePassed = timePassed + (timeBlink * 2);
  37.   } else if(buttonPress == true) {
  38.     digitalWrite(random(10,13), HIGH); // Set random pin on
  39.     delay(timeShowDecision); // For x seconds
  40.     buttonPress = false; // Set button to be enabled again
  41.     timePassed = 0;
  42.   } else {
  43.     // Reset all output pins
  44.     digitalWrite(10, LOW);
  45.     digitalWrite(11, LOW);
  46.     digitalWrite(12, LOW);
  47.   }    
  48. }

Arduino Ethernet Shield hooked up to LED message display

This hack allows you to display messages to a standard Amplus LED Message Display from Clas Ohlsson. The display is controlled by a remote control and via a RJ14 cable plugged into the screen. Serial data is transmitted from cable to the screen. The LED Message display expects the message you pass to it to be encrypted with a check sum. With the help of Rasmus blog post I could generate this checksum in Arduino. I turned his Perl code into Arduino code that you can see below.

  1. int stringToInt(String thisString) {
  2.   String letters = " !’#$%&’()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_`abcdefghijklmnopqrstuvwxyz{|}~";
  3.  
  4.   int newstr = 32 + letters.indexOf(thisString);
  5.  
  6.   return newstr;
  7. }
  8. String generateMessage(String inputString) {
  9.  
  10.   byte checksum = 0×74;
  11.   int i;
  12.  
  13.   for(i = 0;i < inputString.length();i++) {
  14.       String currentChar    = inputString.substring(i,i+1);
  15.       int asciiChar         = stringToInt(currentChar);
  16.       checksum = checksum ^ asciiChar;
  17.   }
  18.  
  19.    String hexStr =  String(checksum, HEX);
  20.    hexStr.toUpperCase();
  21.    
  22.    String message = "<ID00><L1><PA><FE><MA><WC><FE>" + inputString + hexStr + "<E>";
  23.    return message;
  24.    
  25. }
  26. String myString = "My led display message here";
  27.  String mess = generateMessage(myString);
  28.   Serial.print(mess);
Download my Arduino library on Github to try this out.

Control servo motor


The servo motor can be controlled by using the servo library(Servo.h) in Arduino IDE. Plug in the control wire of the servo motor into one of the PWM pins. Attach the servo object to that pin.
  1. Servo servoMotor;
  2. servoMotor.attach(9);
To rotate the servo motor pass in a number between 0 and 180. The servo motor can only move up to 180 degrees.
  1. myservo.write(90);
  2. delay(2000);
  3. myservo.write(180);
  4. delay(2000);

Using a potentiometer to control LED


You can easly control the current running through your LED by adding a potentiometer as part of your circuit.

To control the LED with Arduino programming you attach the potentiometer to your analog in and let your Arduino program decide how much to dim the LED depending on the input you get from the potentiometer.

The input from analogRead returns a value between 0 and 1023. The analogWrite takes values between 0 and 255. The code below show you have to convert your analog in value to make your LED shine as bright as possible when the potentiometer is fully on.
  1. int ledPin = 3;
  2. int potentiomenterInput = 0;
  3. void setup()  {
  4.   pinMode(ledPin, OUTPUT);
  5. }
  6. void loop()  {
  7.  
  8.   potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  9.   analogWrite(ledPin, potentiomenterInput);  
  10.   // The delay can be change to get the desired dimming effect
  11.   delay(20);                            

Sem comentários:

Enviar um comentário