Help with Code for Ultrasonic sensor and neopixel strip

Hey y'all. Pretty new to Arduino and still don't know what I'm doing. I'm running into some issues with my code and don't know where I'm going wrong. For context, I'm trying to run a neopixelstrip through an ultrasonic sensor. I keep getting an issue with the function [all_pixels_on] not being declared in scope. Any help would be appreciated.

#include <NewPing.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>

#define trigPin 12
#define echoPin 13
#define MAX_DISTANCE 500
#define NEOPIXELPIN 7
#define NUMPIXELS 5

Adafruit_NeoPixel pixelStrip = Adafruit_NeoPixel(NUMPIXELS, NEOPIXELPIN, NEO_GRB + NEO_KHZ800);
NewPing sonar(trigPin, MAX_DISTANCE);

int pos = 20;
Servo myservo;

void all_pixels_on(int n, int r, int g, int b) {
  for (int i = 0; i < n; i++) {
    pixelStrip.setPixelColor(i, pixelStrip.Color(r, g, b));
  }
  pixelStrip.show();
}

void setup() {
  pixelStrip.begin();
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  myservo.attach(9);
}

void loop() {
  int duration, distance;

  int r = 0, g = 0, b = 50;

  all_pixels_on(NUMPIXELS, r, g, b);
  delay(1000);

  pixelStrip.clear();
  pixelStrip.show();
  delay(500);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print(distance);
  Serial.println("cm");

  if (distance <= 15) {
    all_pixels_on(5, 255, 0, 0);
    myservo.write(180);
    delay(450);
    myservo.write(90);
    delay(450);
  } else {
    all_pixels_on(5, 0, 255, 0);
    myservo.write(0);
    delay(450);
  }
}


Care to give a link to the documentation on that function?

The loop() is missing a closing brace }.

a7


Don’t power the servo from the Arduino

C:\Users\1Conn\OneDrive\Documents\Arduino\AlrightGamersGetThisBread\AlrightGamersGetThisBread.ino: In function 'void loop()':
C:\Users\1Conn\OneDrive\Documents\Arduino\AlrightGamersGetThisBread\AlrightGamersGetThisBread.ino:29:3: error: 'all_pixels_on' was not declared in this scope
   all_pixels_on(NUMPIXELS, r, g, b);
   ^~~~~~~~~~~~~
C:\Users\1Conn\OneDrive\Documents\Arduino\AlrightGamersGetThisBread\AlrightGamersGetThisBread.ino:59:50: error: a function-definition is not allowed here before '{' token
   void all_pixels_on(int n, int r, int g, int b) {
                                                  ^
C:\Users\1Conn\OneDrive\Documents\Arduino\AlrightGamersGetThisBread\AlrightGamersGetThisBread.ino:65:22: error: expected '}' at end of input
     pixelStrip.show();
                      ^

exit status 1

Compilation error: 'all_pixels_on' was not declared in this scope
#include <NewPing.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>

#define trigPin 12
#define echoPin 13
#define MAX_DISTANCE 500
#define NEOPIXELPIN 7
#define NUMPIXELS 5

Adafruit_NeoPixel pixelStrip = Adafruit_NeoPixel(NUMPIXELS, NEOPIXELPIN, NEO_GRB + NEO_KHZ800);
NewPing sonar(trigPin, MAX_DISTANCE);

int pos = 20;
Servo myservo;

void all_pixels_on(int n, int r, int g, int b) {
  for (int i = 0; i < n; i++) {
    pixelStrip.setPixelColor(i, pixelStrip.Color(r, g, b));
  }
  pixelStrip.show();
}

void setup() {
  pixelStrip.begin();
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  myservo.attach(9);
}

void loop() {
  int duration, distance;

  int r = 0, g = 0, b = 50;

  all_pixels_on(NUMPIXELS, r, g, b);
  delay(1000);

  pixelStrip.clear();
  pixelStrip.show();
  delay(500);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print(distance);
  Serial.println("cm");

  if (distance <= 15) {
    all_pixels_on(5, 255, 0, 0);
    myservo.write(180);
    delay(450);
    myservo.write(90);
    delay(450);
  } else {
    all_pixels_on(5, 0, 255, 0);
    myservo.write(0);
    delay(450);
  }
}

I updated the code and I think I fixed it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.