Jump to content

arduino code

Guest

i need an arduino code that will light up a neopixel red normally and green when a connection is made between a pin and ground or 5v what could i do for this? i have tried if/else and while

Link to comment
Share on other sites

Link to post
Share on other sites

real quick and dirty solution would be (not gonna put it in actual code, arduino has been too long for me to properly remember syntax)

 

while true do

 

if pin input: - one color

else - other color

Link to comment
Share on other sites

Link to post
Share on other sites

real quick and dirty solution would be (not gonna put it in actual code, arduino has been too long for me to properly remember syntax)

 

while true do

 

if pin input: - one color

else - other color

 

believe me, i've tried... i think it's the code for the led that f#ck's it all up, it's something like   for (int i = 0; i < NUMPIXELS; i++) {

 
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();}
Link to comment
Share on other sites

Link to post
Share on other sites

Could you post what part of the program you already have written?

And give a link to the reference of the LED library?

When in doubt: C4

Link to comment
Share on other sites

Link to post
Share on other sites

Could you post what part of the program you already have written?

And give a link to the reference of the LED library?

 

i written

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN            13
 
#define NUMPIXELS      1
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int i;
int read10;
int read7;
int read5;
int read4;
int j;
int k;
 
 
void setup() {
  Serial.begin(9600);
  pixels.begin();
  j = 0;
  k = 1;
 
}
void loop() {
  
  read10 = digitalRead(10);
  read7 = digitalRead(7);
  read5 = digitalRead(5);
  read4 = digitalRead(4);
 
  
    if (read10 == LOW)
  {
   for ( i = 0; i >100;) {
    while(j < 10){
    j = 1000;
     Serial.println("klaar om voor serienummers te scannen.");}}}
       
       
       
       
       
  if (read4 == LOW)
  {
   for (int i = 0; i < NUMPIXELS; i++) {
 
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();
    while(k < 10){
        k = 1000;
    while(j < 10){
    j = 1000;
     Serial.println("serienummer: 1692'2");}
  }
 
  }
    
   
  }
  else if (read7 == LOW)
  {
   for (int i = 0; i < NUMPIXELS; i++) {
 
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();
    while(k < 10){
        k = 1000;
    while(j < 10){
    j = 1000;
     Serial.println("serienummer: 5432'2");}
  }
 
  }
    
   
  }
   else if (read5 == LOW)
  {
   for (int i = 0; i < NUMPIXELS; i++) {
 
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));
    pixels.show();
    while(j < 10){
    j = 1000;
     Serial.println("serienummer: 9547'2");}
  }
    
   
  }
  else {
    for (int i = 0; i < NUMPIXELS; i++) {
 
    pixels.setPixelColor(i, pixels.Color(150,0, 0));
    pixels.show();
  }
   
    delay(1000);
  }
    
}
 

but it's broken

 

 

and the library is https://github.com/adafruit/Adafruit_NeoPixel

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not familiar with neopixels but the one thing that stands out to me is if pin 10 is low you enter an infinite loop preventing anything from happening even if pin 10 is changed to high.

The

while (k < 10){   k = 1000;   ...}
lines are also strange, IDK if they are there for some intended future code though.

Have you tried something simpler to start with like:

if (digitalRead(4) == LOW){   pixels.setPixelColor(0, pixels.Color(0, 150, 0));   pixels.show();}else{   pixels.setPixelColor(0, pixels.Color(150, 0, 0));   pixels.show();}
Also you are initialising the strip with a length of 1, IDK if you have a strip with a length of 1 (seems a bit redundant) but if not that might throw out the code.
Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

It doesn't seem like you're using the pixel code wrong, but try organizing your if and while statements with proper indents (tab) so it's easier to read your code.

And then check for infinite loops you can get stuck in. (like while( x > 5) {something something x++;} this will loop forever and get stuck)

When in doubt: C4

Link to comment
Share on other sites

Link to post
Share on other sites

It doesn't seem like you're using the pixel code wrong, but try organizing your if and while statements with proper indents (tab) so it's easier to read your code.

And then check for infinite loops you can get stuck in. (like while( x > 5) {something something x++;} this will loop forever and get stuck)

 

 

I'm not familiar with neopixels but the one thing that stands out to me is if pin 10 is low you enter an infinite loop preventing anything from happening even if pin 10 is changed to high.

The

lines are also strange, IDK if they are there for some intended future code though.

Have you tried something simpler to start with like:

Also you are initialising the strip with a length of 1, IDK if you have a strip with a length of 1 (seems a bit redundant) but if not that might throw out the code.

 

 

can someone just give me the full code you would use? that would make it a lot clearer and i have to use more pins to detect high...

Link to comment
Share on other sites

Link to post
Share on other sites

can someone just give me the full code you would use? that would make it a lot clearer and i have to use more pins to detect high...

Try:

#include <Adafruit_NeoPixel.h>#define LOOP_DELAY	100	// Delay between each loop in ms (higher values will reduce responsiveness to inputs)#define NUMPIXELS	1	// Number of pixels on NeoPixel strip/matrix#define NEOPIXELS_PIN	13	// NeoPixel control line#define INPUT_PIN	4	// Input to control colour (high/low)Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIXELS_PIN, NEO_GRB + NEO_KHZ800);void setup() {	pixels.begin();	pixels.show();}void loop() {	// If input low set pixel colour to red (about half brightness)	if (digitalRead(INPUT_PIN) == LOW)	{		for (int c = 0; c < NUMPIXELS; c++)		{			pixels.setPixelColor(c, pixels.Color(0, 150, 0));		}	}	// If input high set pixel colour to green (about half brightness)	else	{		for (int c = 0; c < NUMPIXELS; c++)		{			pixels.setPixelColor(c, pixels.Color(150, 0, 0));		}	}	// Update pixels	pixels.show();	delay(LOOP_DELAY);}

Where NUMPIXELS needs to be set to the number of pixels/LEDs on your strip/matrix.

Again I'm not familiar with NeoPixels so I cant test it and I'm assuming the "NEO_GRB + NEO_KHZ800" part of the initialiser is correct for your device.

The

pixels.setPixelColor(c, pixels.Color(0, 150, 0))
parts could also be written as

pixels.setPixelColor(c, 0, 150, 0)
its the same thing, the pixels.Color() function just allows you to save the colour values as a single 32bit integer instead of three 8bit integers for convenience.
Link to comment
Share on other sites

Link to post
Share on other sites

Try:

Where NUMPIXELS needs to be set to the number of pixels/LEDs on your strip/matrix.

Again I'm not familiar with NeoPixels so I cant test it and I'm assuming the "NEO_GRB + NEO_KHZ800" part of the initialiser is correct for your device.

The

parts could also be written as
its the same thing, the pixels.Color() function just allows you to save the colour values as a single 32bit integer instead of three 8bit integers for convenience.

 

i get the neopixel part, but i need the code to do something when pin 10 or 4 are HIGH or LOW, everything i did resulted in the code just skipping my if(digitalRead(4) == LOW) and activating the code and the led...

Link to comment
Share on other sites

Link to post
Share on other sites

The most straight forward way would probably be something like:

if (digitalRead(4) == LOW && digitalRead(10) == LOW){	....}else if (digitalRead(4) == LOW && digitalRead(10) == HIGH){	....}else if (digitalRead(4) == HIGH && digitalRead(10) == LOW){	....}else if (digitalRead(4) == HIGH && digitalRead(10) == HIGH){	....}
but a slightly more elegant way might be something like:

unsigned int input_pins;input_pins = 0;input_pins = digitalRead(4) << 1 | digitalRead(10);switch (input_pins){	case 0:			// P4 LOW + P10 LOW		....		break;	case 1:			// P4 LOW + P10 HIGH		....		break;	case 2:			// P4 HIGH + P10 LOW		....		break;	case 3:			// P4 HIGH + P10 HIGH		....		break;	default:		....		break;}
where input_pins can be loaded with your inputs using the shift operator (<<) followed by the number of spaces to shift the input bit by (LOW = 0, HIGH = 1) giving you a binary representation of the input pins states which you can then use as an integer value to determine the appropriate action. This way becomes tidier as you start using more and more inputs to determine more and more actions.

Or if you wanted pin 10 to be a sort of on/off for the inputs then you could either nest the other inputs 'if' statements within the pin 10 'if' statement or use

while(!digitalRead(10));
at the start of the loop to prevent the loop from running unless pin 10 was HIGH.

If this doesn't help please describe what you want your program to do/output at each combination of your inputs to make it clear what you are trying to do :)

Link to comment
Share on other sites

Link to post
Share on other sites

Neopixels are using Pulse width modulation. You should be programming for that. It has three wires. 1 for power in, 1 for ground, and a signal wire. 

https://www.arduino.cc/en/Tutorial/PWM

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Neopixels are using Pulse width modulation. You should be programming for that. It has three wires. 1 for power in, 1 for ground, and a signal wire. 

https://www.arduino.cc/en/Tutorial/PWM

When there's a library to take care of it you would just be wasting your time ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×