0

trouble interpreting analogRead
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

trouble interpreting analogRead

by mdikov on Fri Jun 14, 2019 11:44 am

I have been having some issues interpreting the value analogRead in my program to a trinket 3v. When I use the USB plug as power all works fine, but when I use 5v power through the BAT pin the behavior changes and the program goes to the top if branch.

Code: Select all | TOGGLE FULL SIZE
 if(switchOne > ON && switchTwo > ON) {
    rainbowHold(10);
    return;
  }


What am I missing? I am thinking the numbers I get from analogRead are different with each power source, but they should be stable, no?

Find attached the pin connections. On #1 I have the neopixel jewels; #3 and #4 are the buttons which are directly connecting to ground (no extra resistors); BAT for USB power, Ground.


Code: Select all | TOGGLE FULL SIZE
...
#define SWITCH_PIN_ONE 2
#define SWITCH_PIN_TWO 3
int ON = 1000;
...
void loop() {
 
  int switchOne = analogRead(SWITCH_PIN_ONE);
  int switchTwo = analogRead(SWITCH_PIN_TWO);

  if(switchOne > ON && switchTwo > ON) {
    rainbowHold(10);
    return;
  }

  if(switchOne > ON) {
    solidWhite(0);
    return;
  }

  if (switchTwo > ON) {
    fruit(50);
    return;
  }

  solidBlack(10);

}
...
Attachments
IMG_4416.jpg
IMG_4416.jpg (578.82 KiB) Viewed 163 times

mdikov
 
Posts: 12
Joined: Thu Oct 06, 2016 3:25 pm

Re: trouble interpreting analogRead

by franklin97355 on Fri Jun 14, 2019 12:24 pm

What battery are you running the Trinket on? If you are running on a LiPo (3.7v) you don't have 5-volts available.

franklin97355
 
Posts: 20986
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: trouble interpreting analogRead

by adafruit_support_bill on Fri Jun 14, 2019 12:28 pm

Please post a photo or diagram showing how your switches are connected.
And please post the complete code so we can see if and/or how you have configured your pins.

A more fundamental question is: why are you using analogRead to read switch states? digitalRead would be more appropriate for that.

adafruit_support_bill
 
Posts: 74058
Joined: Sat Feb 07, 2009 10:11 am

Re: trouble interpreting analogRead

by mdikov on Fri Jun 14, 2019 3:07 pm

franklin97355 wrote:What battery are you running the Trinket on? If you are running on a LiPo (3.7v) you don't have 5-volts available.


No battery, USB cable to a USB wall charger. The lamp is stationary.

mdikov
 
Posts: 12
Joined: Thu Oct 06, 2016 3:25 pm

Re: trouble interpreting analogRead

by mdikov on Fri Jun 14, 2019 3:12 pm

adafruit_support_bill wrote:Please post a photo or diagram showing how your switches are connected.
And please post the complete code so we can see if and/or how you have configured your pins.

A more fundamental question is: why are you using analogRead to read switch states? digitalRead would be more appropriate for that.


Well about digital vs analog... we don't know the things we don’t know :) Now that you said it totally makes sense. Here is the code.

Don’t have the diagram on hand but the wiring picture is above. Basically I have a 3 way lamp switch with 4 states hooked up to 3,4 and the common end to 5v/BAT.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 1
#define NUMBER_OF_PIXELS 32
#define SWITCH_PIN_ONE 2
#define SWITCH_PIN_TWO 3

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMBER_OF_PIXELS, PIXEL_PIN);
uint32_t orange = pixels.Color(255, 128, 0),
         lyme = pixels.Color(0, 255, 0),
         lemon = pixels.Color(255, 255, 0),
         grapefruit = pixels.Color(255, 0, 128);
int ON = 1020;

void setup() {
  pixels.begin(); 
  solidWhite(20);
  solidBlack(20);
}

void loop() {
 
  int switchOne = analogRead(SWITCH_PIN_ONE);
  int switchTwo = analogRead(SWITCH_PIN_TWO);

  // runs #3
  if(switchOne > ON && switchTwo > ON) {
    rainbowHold(10);
    return;
  }

  // runs #1
  if(switchOne > ON) {
    solidWhite(0);
    return;
  }

  // runs #2
  if (switchTwo > ON) {
    fruit(50);
    return;
  }

  solidBlack(10);

}

void solidWhite(uint8_t wait) {
  uint8_t  i;
 
  pixels.setBrightness(120);
 
  for(i=0; i<pixels.numPixels(); i++) {
      pixels.setPixelColor(i, pixels.Color(250, 250, 250));
  }
  pixels.show();
  delay(wait);
}

void solidBlack(uint8_t wait) {
  uint8_t  i;
 
  pixels.setBrightness(0);
 
  for(i=0; i<pixels.numPixels(); i++) {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  }
 
  pixels.show();
  delay(wait);
}

void fruit(uint8_t wait) {
  uint8_t  i;
  int      firstJewel = 6,
           secondJewel = 13,
           thirdJewel = 20;

  pixels.setBrightness(120);
 
  for(i=0; i < pixels.numPixels(); i++) {
    if(i <= firstJewel) {
      pixels.setPixelColor(i, orange);
    } else if(i > firstJewel && i <= secondJewel) {
      pixels.setPixelColor(i, lyme);
    } else if(i > secondJewel && i <= thirdJewel) {
      pixels.setPixelColor(i, lemon);
    } else {
      pixels.setPixelColor(i, grapefruit);
    }
  }
 
  pixels.show();
  delay(wait);
}

void rainbowHold(uint8_t wait) {
  uint16_t r, j;

  pixels.setBrightness(120);

  for(j=0; j<256*1; j++) { // 3 cycles of all colors on wheel
    for(r=0; r< pixels.numPixels(); r++) {
      pixels.setPixelColor(r, Wheel(((r * 256 / pixels.numPixels()) + j) & 255));

      int switchOne = analogRead(SWITCH_PIN_ONE);
      int switchTwo = analogRead(SWITCH_PIN_TWO);
     
      if(switchTwo < ON) {
        return;
      }
    }
    pixels.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

mdikov
 
Posts: 12
Joined: Thu Oct 06, 2016 3:25 pm

Re: trouble interpreting analogRead

by XRAD on Fri Jun 14, 2019 5:35 pm

Instead of 'ON,' consider adding switch 'states' before setup. Look at this example:

https://www.arduino.cc/en/tutorial/button

Then you can add all the T /F's (0,1 or boolean) you want to the if's( ) to make something happen.....

and you can do something like:
if ((a ==True) && (B == True)) { do something} // etc.....

XRAD
 
Posts: 475
Joined: Sat Nov 19, 2016 3:28 pm

Re: trouble interpreting analogRead

by adafruit_support_bill on Fri Jun 14, 2019 6:54 pm

Don’t have the diagram on hand but the wiring picture is above. Basically I have a 3 way lamp switch with 4 states hooked up to 3,4 and the common end to 5v/BAT.

So when the switch is open, the pin is 'floating' and the value is 'undefined'.

See lesson 6 for a tutorial on how to wire and program switch inputs: https://learn.adafruit.com/adafruit-ard ... s/overview

adafruit_support_bill
 
Posts: 74058
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.