Results 1 to 3 of 3

Thread: Arduino Sketches

  1. #1
    If you can't hack it, you don't own it! Oneslowz28's Avatar
    Join Date
    Mar 2007
    Location
    Aiken, Sc
    Posts
    5,084

    Default Arduino Sketches

    This will be a small repository of Arduino Sketches that we have created and shared. To have your sketch listed here it must work. If you are wanting to ask questions about your sketch you should create a new thread. This thread is only for sketches to be posted in. For comments you should post your sketch in a new thread.

  2. #2
    If you can't hack it, you don't own it! Oneslowz28's Avatar
    Join Date
    Mar 2007
    Location
    Aiken, Sc
    Posts
    5,084

    Default Re: Arduino Sketches

    I have a pretty cool Random color RGB LED fader code you should have fun messing around with. Just change the delays for faster or slower fade time. It works with common anode RGB leds.

    It uses pins 9, 10 and 11 and remember to use resistors on each anode. Cathode to Ground.


    Code:
    float RGB1[3];
    float RGB2[3];
    float INC[3];
    
    int red, green, blue;
    
    int RedPin = 10;
    int GreenPin = 11;
    int BluePin = 9;
    
    
    void setup()
    {
        Serial.begin(9600);
      randomSeed(analogRead(0));
      
    for (int x=0; x<3; x++) {
      RGB1[x] = random(256);
      RGB2[x] = random(256); }
    
    }
    
    void loop()
    {
      randomSeed(analogRead(0));
      
      for (int x=0; x<3; x++) {
        INC[x] = (RGB1[x] - RGB2[x]) / 256; }
      
      for (int x=0; x<256; x++) {
        
        red = int(RGB1[0]);
        green = int(RGB1[1]);
        blue = int(RGB1[2]);
    
        analogWrite (RedPin, red);  
        analogWrite (GreenPin, green);  
        analogWrite (BluePin, blue);    
        delay(25);  
    
        for (int x=0; x<3; x++) {
    	   RGB1[x] -= INC[x];}
     
      }
      
      for (int x=0; x<3; x++) {
      RGB2[x] = random(956)-700;
      RGB2[x] = constrain(RGB2[x], 0, 255);
      
      delay(10);
     }
    }

  3. #3
    Resident 100HP water-cannon operator SXRguyinMA's Avatar
    Join Date
    Jun 2008
    Location
    MA
    Posts
    5,862

    Default Re: Arduino Sketches

    This code with move a servo based on a temperature reading. The temp sensor used is a 2-wire type from an NZXT Sentry 2 fan controller.

    it will run the loop only when 5v is sensed on the digital #7 input. this can be easily removed if you don't need this feature.

    You'll have to adjust the "val = map" and "myservo.write(#)" values according to your specific usage.

    Project here: http://fritzing.org/projects/arduino...servo-control/

    schematic: (made using Fritzing)

    Code:
    // Controlling a servo position using a temperature sensor 
    // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 
    // edited 5-12-2010 by Will Lyon to include base setting for servo if voltage not detected on pin 7
    //Huge thanks to crenn @ TBCS for helping me sort this!
    
    #include <Servo.h> 
    #define CONTROL 7
    
    Servo myservo;  // create servo object to control a servo 
     
    int temps = 0;  // analog pin used to connect the temp sensor
    int val;        // variable to read the value from the analog pin
     
    void setup() 
    { 
      pinMode (CONTROL, INPUT);       // sets the control pin to input
      myservo.attach(9);              // attaches the servo on pin 9 to the servo object
      digitalWrite(CONTROL, LOW);     // ensure internal pullup resistor is disabled.
    
    } 
     
    void loop()   
    {
      val = digitalRead(CONTROL);         //read input of pin 7 and store it
      if (val == HIGH){                   // reads whether or not 5v is present on pin 7, if 5v present, continue:
      val = analogRead(temps);            // reads the value of the temp sensor (value between 0 and 1023) 
      val = map(val, 350, 700, 75, 105);  // scale it to use it with the servo (value between 0 and 180) 
      myservo.write(val);                 // sets the servo position according to the scaled value 
      delay(25);                          // waits for the servo to get there 
    } else {
      if (val == LOW);                    // if no voltage present on pin 7, continue
      myservo.write(50);                  //sets servo position to 50 if no voltage is detected on pin 7
    }
    delay(25);        // waits for the servo to get there
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •