Connect with us

Arduino Nano + RGB led + adxl335

Discussion in 'Microcontrollers, Programming and IoT' started by mollekake, Jan 24, 2015.

Scroll to continue with content
  1. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    Hello!
    So i've been fidling around with my arduino, a rgb led and an accelerometer.
    I've tried out some tap detection, and got it to work, and decided i wanted to do something with it.
    So i hooked up an RGB led, and am trying to make it change color for each tap.
    The tap detection works, but the led colors are all wrong :S

    Here is my code:
    Code:
    // these constants describe the pins. They won't change:
    const int xpin = A2;                  // x-axis of the accelerometer
    const int ypin = A3;                  // y-axis
    const int zpin = A4;                  // z-axis (only on 3-axis models)
    
    const int red = 9;
    const int green = 10;
    const int blue = 11;
    
    //int sampleDelay = 50;   //number of milliseconds between readings
    
    
    void setup()
    {
      // initialize the serial communications:
      Serial.begin(9600);
    
      //Make sure the analog-to-digital converter takes its reference voltage from
      // the AREF pin
      analogReference(EXTERNAL);
    
      pinMode(xpin, INPUT);
      pinMode(ypin, INPUT);
      pinMode(zpin, INPUT);
      pinMode(red, OUTPUT);
      pinMode(green, OUTPUT);
      pinMode(blue, OUTPUT);
      digitalWrite(red, HIGH);
      digitalWrite(green, HIGH);
      digitalWrite(blue, HIGH);
    }
    
    int x = 0;
    int y = 0;
    int z = 0;
    int prex = 0;
    int prey = 0;
    int prez = 0;
    
    int redVal = 0;
    int greenVal = 0;
    int blueVal = 0;
    boolean redb = false;
    boolean greenb = false;
    boolean blueb = false;
    
    void loop()
    {
      //Defining previous readings
      prex = x;
      prey = y;
      prez = z;
     
      Serial.print( analogRead(xpin));
      Serial.print("\t");
      //add a small delay between pin readings.  I read that you should
      //do this but haven't tested the importance
      delay(1);
      Serial.print( analogRead(ypin));
      Serial.print("\t");
      //add a small delay between pin readings.  I read that you should
      //do this but haven't tested the importance
      delay(1);
      Serial.print( analogRead(zpin));
      Serial.print("\n");  // delay before next reading:
      //delay(sampleDelay);
      delay(1);
     
      //Reading current x,y,z
      x = analogRead(xpin);
      delay(1);
      y = analogRead(ypin);
      delay(1);
      z = analogRead(zpin);
     
      //Detect if the difference between last reading and current
      //reading is too far apart, tap occured
      //Tap detection is quite sensive, too sensitive?
      if(
      (x > (prex + 10) || x < (prex - 10)) ||
      (y > (prey + 10) || y < (prey - 10)) ||
      (z > (prez + 10) || z < (prez - 10))
      ){
        Serial.print("TAP!!");
        Serial.println();
       
      //Switch color
      if(redb){
       redVal = 255;
       greenVal = 0;
       blueVal = 0;
       redb = false;
       greenb = true;
      }else if(greenb){
       redVal = 0;
       greenVal = 255;
       blueVal = 0;
       greenb = false;
       blueb = true;
      }else if(blueb){
       redVal = 0;
       greenVal = 0;
       blueVal = 255;
       blueb = false;
       redb = true;
      }else{
       redVal = 255;
       greenVal = 0;
       blueVal = 0;
       greenb = true;
      }
      delay(1);
      analogWrite(red, redVal);
      analogWrite(green, greenVal);
      analogWrite(blue, blueVal);
      //END of color switch
       
       
        delay(150);//Delay to prevent false tap detection
        //Reading current x,y,z
          x = analogRead(xpin);
          delay(1);
          y = analogRead(ypin);
          delay(1);
          z = analogRead(zpin);
      }
      //End of tap detection
     
      delay(10);
    }

    What happens is that when i write the program, the Led starts out lighting up BOTH green and blue. When i TAP it changes to RED. When i tap again, it is still RED. When i tap again, it goes to BOTH green and blue.
    I can't see why :S
    Halp?
     
  2. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    Figured it out after a lot of frustration. I did it the opposite of correct.
    In order to light up blue, red and green must be at 255 and blue at 0. Green is red and blue 255, and green 0, and so on.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-