Trying to add a 12v input to run code

New to this but i found a sketch I want to use with a 12v input to run it.. I know i have to use a resistor in the wiring. I just can't figure out how to mod this sketch.. What i want is my uno to stay on.. And when pin 9 recieves the votlage input, it runs the code until the power to pin 9 is disconnected.

#include <Adafruit_NeoPixel.h>

// SETUP YOUR OUTPUT PIN AND NUMBER OF PIXELS
#define PIN 7
#define NUM_PIXELS  34

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  clearStrip(); // Initialize all pixels to 'off'
  delay(1000);
}

void loop() {
knightRider(2, 15, 10, 0xFF0000); // Cycles, Speed, Width, RGB Color (red) 
 knightRider(3, 1, 1, 0xFF0000); // Cycles, Speed, Width, RGB Color (red)
  
  clearStrip();
  delay(1);

}

// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs.  The original display used
//         light bulbs, so they have a persistance when turning off.  This creates a trail.
//         Effective range is 2 - 8, 4 is default for 16 pixels.  Play with this.
// Color - 32-bit packed RGB color value.  All pixels will be this color.
// knightRider(cycles, speed, width, color);
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
  // Larson time baby!
  for(int i = 0; i < cycles; i++){
    for (int count = 1; count<NUM_PIXELS; count++) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for(int x = count; x>0; x--) {
        old_val[x-1] = dimColor(old_val[x-1], width);
        strip.setPixelColor(x-1, old_val[x-1]); 
      }
      strip.show();
      delay(speed);
    }
    for (int count = NUM_PIXELS-1; count>=0; count--) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for(int x = count; x<=NUM_PIXELS ;x++) {
        old_val[x-1] = dimColor(old_val[x-1], width);
        strip.setPixelColor(x+1, old_val[x+1]);
      }
      strip.show();
      delay(speed);
    }
  }
}

void clearStrip() {
  for( int i = 0; i<NUM_PIXELS; i++){
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

uint32_t dimColor(uint32_t color, uint8_t width) {
   return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}

// Using a counter and for() loop, input a value 0 to 251 to get a color value.
// The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
// Entering 255 will give you white, if you need it.
uint32_t colorWheel(byte WheelPos) {
  byte state = WheelPos / 21;
  switch(state) {
    case 0: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127)); break;
    case 1: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0); break;
    case 2: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0); break;
    case 3: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0); break;
    case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255, 0); break;
    case 5: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6); break;
    case 6: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127); break;
    case 7: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255); break;
    case 8: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255); break;
    case 9: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255); break;
    case 10: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255); break;
    case 11: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6)); break;
    default: return strip.Color(0, 0, 0); break;
  }
}

i guess you're thinking of using a pair of resistors to create a voltage divider to lower the 12V to an appropriate level to the input.

i'd suggest using an npn transistor with its emitter connected to ground, it's collector connector tied to the Arduino input pin and the base connected to the 12V input thru a resistor (~1k). no need for a resistor on the input pin when configured as INPUT_PULLUP

of course best isolation is with a opto-coupler that would be wired similar to the transistor

Not clear to me if you mean you need to use a 12V button connected to the Arduino to input into the program or that the 12V is to run the neo-pixels.

I don't see pin 9 in your code, perhaps I missed it.
Do you mean that you want to use a button that is pressed to run the code and the code keeps running until you let go of the button? That is very easy to do and basically it would be a button reading function.
But if you mean there is an external voltage source, then please post a wiring diagram/hand drawn sketch so we can see exactly what you mean.

Yes I want to run neopixels and the code i posted is the original code. I haven't added pin 9 yet. I tried several ways to do it but the code would run regardless of the button state..Basically i'm connecting the neopixels to pin 7 wih a resistor inline, then a 12v power source to pin 9. The uno will always be powered on. I want the code for the neopixels to run only when the 12v source is on and to stop when it's off

You didn't answer the previous question directly. It's not asking what the code is doing, it's asking whether the on/off state of 12V is set somewhere else and should be detected by the Arduino, or the opposite where the Arduino controls the 12V power to the LEDs by some kind of switch for example.

So it's really a question about your existing or desired hardware configuration.

Sorry about the sloppy design.. lol I want the code to run with i turn the 12v on and to stop when i turn it off

Well, I've seen Fritzing that was worse. It looks like you want to turn on the mysterious rectangle with dots (please tell us what that is again). The resistor network is correct.

If you literally only want to turn on the mystery box, whenever 12V is detected, you might not even need an Arduino, just a relay. Or, if the box has some built in power amplification, and has a digital input, you could connect the output of the resistor divider directly to the box. No Arduino, no relay, no extra hardware. :slight_smile:

The code would consist of, as well as the usual sketch outline, a simple pass through:

digitalWrite(outPin, digitalRead(inputPin) );

One thing that strikes me about using the resistor divider approach, is to ask how confident you are that the 12V is never over 12V. If you spec'ed the divider for 12V to 5V and found that the input actually went over 12V, you would be putting >5V into the Arduino pin.

Hi,
Have you got the gnd of the potential divider and hence the controller, connected to the gnd of the machine that you get the 12V from?
Do you have a DMM?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The funny looking rectangle is a neopixel strip.. i have a buck convertor powering the Uno..

Still not clear why you are needing the 12V input to be read by the MCU.

  • Is the 12V powering the Neopixels?
  • Is it powering something else, and you want the Neopixels turned off when that other thing is turned off?

Letting us know this will really allow us to help you better with perhaps some simpler/safer answers to your question.

i thought it is simply a signal, not power source, to the Arduino indicating when the code should run.

That's what I though too. But then he could use input-pullup on a pin and a plain old button.

but that's not what he has

I'll take a wild guess that it's tapped from some other device that happens to be 12V.

But @lostmagic if it's 12V automotive, beware it's a very noisy environment.

I agree, the OP has not provided enough information, I thought the diagram was a start, but it's obvious there are more questions. A buck converter was mentioned, it's not in the diagram. So I think it's fair to demand some real documentation or at least complete photos of the entire project.

Otherwise, it is a huge waste of our time.

I want the 12v to be an signal to tell th uno to run the sketch and to stop it.. I want to use it in my truck on a switch.. I flip the switch and the uno runs the sketch. I turn the switch off and the uno stops the code.. The uno has its own power source that is always on.. \

Best i can do with drawing on linux

Not super sure on your problem, but for drawing schematics and visuals of your circuit, have you considered downloading EasyEDA or Fritzing or something similar? EasyEDA is free. Fritzing I'm not sure if it is free or costs money.

I sort of had an inkling that this is what you wanted to do. I presume the switch and the Uno are in the cab, near each other?

If so, just wire the switch directly to the Uno pin 9. In your code, set pin 9 to INPUT_PULLUP. When the button is pressed or the switch is flipped, it will read LOW, otherwise HIGH.

BTW, however you do it, you will need to de-bounce the switch. There is lots of info on simple ways to do it in software.

And hopefully you are not aiming to power the neoPixels from the Uno and instead will power them directly from the regulator.

image