Multiple Analogue inputs? Tail lamp project

Why do you keep on referring to Analog pins?

Why did I see the word "float" in your code? Do you know what it means?

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post any; it seems you understand how to edit posts) to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks. Inappropriate blanks make your code very confusing to read. :astonished:

const int sensorPin1 = A0;    // select a input pin for control 1 RUN LAMP
...
int sensorValue1 = A0;  // variable to store the value coming from the sensor
...
  pinMode(A0, OUTPUT);
...
if(sensorValue1 = digitalRead(sensorPin1)== HIGH) // read the value from the sensor1:

Explanation, please

Please remember to use code tags when posting code

TheMemberFormerlyKnownAsAWOL:

const int sensorPin1 = A0;    // select a input pin for control 1 RUN LAMP

...
int sensorValue1 = A0;  // variable to store the value coming from the sensor
...
 pinMode(A0, OUTPUT);
...
if(sensorValue1 = digitalRead(sensorPin1)== HIGH) // read the value from the sensor1:



Explanation, please

Please remember to use code tags when posting code

Thank you! Still learning.
Explanation ... I don't have a good one. I've only started trying to learn this about a week ago. I've basically experimented with different examples until I got the desired result. I'm sure I'm making a lot of mistakes! Please help :slight_smile:

Paul__B:
Why do you keep on referring to Analog pins?

Why did I see the word "float" in your code? Do you know what it means?

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post any; it seems you understand how to edit posts) to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks. Inappropriate blanks make your code very confusing to read. :astonished:

Thank you. I'm not 100% sure what I'm doing (that extends to most aspects of my life)
I'll try to follow those rules and be more disciplined with my code and my posting here.

Good work on the code tags. :sunglasses: Still confusing blank space but better than nothing. :grinning:

I'll let you figure out the other details regarding what a "float" is and what "analog" pins do.

Paul__B:
Good work on the code tags. :sunglasses: Still confusing blank space but better than nothing. :grinning:

I'll let you figure out the other details regarding what a "float" is and what "analog" pins do.

I'm working on that code yes :slight_smile:
So a float vs int, only difference is a whole number vs a floating point, meaning it's a number with a decimal.
So a float should only be used when more precision is needed... Certainly a waste of space to use in my case :grin:

That makes sense.

Working on the other stuff..... long way to go obviously.

Is there an example code similar to what I'm trying to do?

Thanks again :slight_smile:

You set state to a value 1, 2, 4 or 8. (=)

Your original idea of adding to state as you check each sensor is sound. ( += 2, e.g.)

So do that. But many would use the ‘or’ operator instead of addition:

state |= RUN_BIT;

which just sets the bit for ‘run’. (# define RUN_BIT 1 // or whatever)

You can clear one bit with the ‘and’ operator:

state &= ~RUN_BIT;

these are common idiomatic expressions in C. If you learn about bits and how to deal with them in C/C++ you will come across this right away and then see it and use it for… the rest of your life.

HTH

a7

Hi,
Can I suggest you read ALL your inputs at the top of the loop and assign variables their various values.
This way you take a snapshot of your inputs.

Then add them to get your unique total and then use switch.. case to assign your output functions to the values.

switch.. case...

Tom... :slight_smile:

void loop()
{

int state1 = 0; // initialize state1 value to zero
int state2 = 0; // initialize state2 value to zero
int state3 = 0; // initialize state3 value to zero
int state4 = 0; // initialize state4 value to zero

  
if(sensorValue1 = digitalRead(sensorPin1)== HIGH) // read the value from the sensor1:
state1 = 1;
delay(10);

if(sensorValue2 = digitalRead(sensorPin2)== HIGH) // read the value from the sensor2:
state2 = 2;
delay(10);

if(sensorValue3 = digitalRead(sensorPin3)== HIGH) // read the value from the sensor3:
state3 = 4;
delay(10);

if(sensorValue4 = digitalRead(sensorPin4)== HIGH) // read the value from the sensor4:
state4 = 8;
delay(10);


//totals of state for which frame to render

if(state1 + state2 + state3 + state4 == 0)
RenderFrame (ledarrayoff),
delay(100);

if(state1 + state2 + state3 + state4 == 1)
RenderFrame (ledarrayrun),
delay(100);

if(state1 + state2 + state3 + state4 == 2)
RenderFrame (ledarrayturnon),
delay(400),
RenderFrame (ledarrayoff),
delay(400);

if(state1 + state2 + state3 + state4 == 3)
RenderFrame (ledarrayrunturn),
delay(400),
RenderFrame (ledarrayrun),
delay(400);

if(state1 + state2 + state3 + state4 == 4)
RenderFrame (ledarraybrake),
delay(50);

if(state1 + state2 + state3 + state4 == 5)
RenderFrame (ledarrayrunbrake),
delay(50);

if(state1 + state2 + state3 + state4 == 6)
RenderFrame (ledarrayturnbrakeoff),
delay(400),
RenderFrame (ledarrayturnbrakeon),
delay(400);

if(state1 + state2 + state3 + state4 == 8)
RenderFrame (ledarrayreverse),
delay(50);

if(state1 + state2 + state3 + state4 == 9)
RenderFrame (ledarrayrunreverse),
delay(50);

if(state1 + state2 + state3 + state4 == 10)
RenderFrame (ledarrayturnreverseon),
delay(400),
RenderFrame (ledarrayreverse),
delay(400);

if(state1 + state2 + state3 + state4 == 11)
RenderFrame (ledarrayrunturnreverseon),
delay(400),
RenderFrame (ledarrayrunreverse),
delay(400);

if(state1 + state2 + state3 + state4 == 12)
RenderFrame (ledarraybrakereverse),
delay(40);

if(state1 + state2 + state3 + state4 == 13)
RenderFrame (ledarraybrakereverse),
delay(40);

if(state1 + state2 + state3 + state4 == 15)
RenderFrame (ledarrayrunturnreverseon),
delay(400),
RenderFrame (ledarrayrunreverse),
delay(400);

}

void RenderFrame(const uint32_t *arr)
{
  for (uint16_t t = 0; t < 256; t++)
  {
   strip.setPixelColor(t, arr[t]);
  }
  strip.show();


}

Here's a section of my code that made this work as desired!

The total of the 4 sensors is added up and individual frames are shown.

I'm open to any suggestions to clean that up. I was thinking the expression "if(state1 + state2 + state3 + state4 == x)"

Could probably be "if(stateT == x)" and before that put int stateT = state1 +state2 + state3 + state4)

or something like that to make the lines have less text.

Thanks :slight_smile:

Through a combination of over-thinking and under-thinking, and some willful ignorance of suggestions made here, you seem to be regressing.

Using only part of what @alto777 was getting at in #26, here's a chopped up but complete and compiles version of your code. I had to comment out some things you left out of your last posted code. Always try to post a complete compiles and fuctions code, we like to take it and work with it in the IDE, so.

I also auto-formatted it, that's good for catching errors as C/C++ does not treat whitespace like you might want it to, the {s and }s rule, not indentation!

Below uou wll see one variable, 'theState' keeps all four sensor readings as bits 1, 2, 4 and 8. Their sum can be checked for combinations of bits. If you are worried that you might need state3, the corresponding bit can be tested

if (theState & 4) { /* test one bit in theState)
/* code to run if sensor3 is HIGH */
}

You can give nice names to the combinations like

define COMBO 9

which would be equal to theState if the 8 and 1 bits were set.

Then

if (theState == COMBO) // &c.

HTH

a7

//tail lamp dev v1

// alternate method

#include <Adafruit_NeoPixel.h>

#define PIN 3

const int sensorPin1 = A0;    // select a input pin for control 1 RUN LAMP
const int sensorPin2 = A1;    // select a input pin for control 2 TURN SIGNAL
const int sensorPin3 = A2;    // select a input pin for control 3 BRAKE LIGHT
const int sensorPin4 = A3;    // select a input pin for control 4 REVERSE

const int NUM_PIXELS = 256;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB);

void setup()
{
  strip.begin();
  strip.setBrightness(10);
  strip.show();   // Initialize all pixels to 'off'
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
}

void loop()
{

  int theState = 0; // all sensors LOW until proven HIGH!


  if (digitalRead(sensorPin1) == HIGH) // read the value from the sensor1:
    theState += 1;

  delay(10);


  if (digitalRead(sensorPin2) == HIGH) // read the value from the sensor2:
    theState += 2;

  delay(10);

  if (digitalRead(sensorPin3) == HIGH) // read the value from the sensor3:
    theState += 4;
 
  delay(10);

  if (digitalRead(sensorPin4) == HIGH) // read the value from the sensor4:
    theState += 8;

  delay(10);

  if (theState == 13) // or whatever sum you wanna check 
    //RenderFrame (ledarrayoff),
    delay(100);

}

void RenderFrame(const uint32_t *arr)
{
  for (uint16_t t = 0; t < 256; t++)
  {
    strip.setPixelColor(t, arr[t]);
  }
  strip.show();
}

Sorry, I had done that last night and had not seen your other suggestions yet! I'll work on cleaning that up.

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