Multiple Analogue inputs? Tail lamp project

Hello, complete Noob alert.

I've been practicing with Arduino Nano LOT 33 and some LED matrices for a few days. I've learned a lot! I've been able to display different arrays and switch back and forth by changing analogue pin inputs. Having a lot of fun!

However I'm running into an unanticipated issue, which is that for my project I need to read more than one analogue input value at the same time, which may not be possible with nano?

This project is for tail lamps that contain a turn signal, brake lamp, reverse lamp, and "running lamp"

So I'd need to read all four values to determine what to display on the matrix.

Any advice on how to do this with Nano? It's looking to me like I've have to somehow translate the signals to voltages, that all add up to specific values, then run a "renderframe" for each possible value?

Thanks for any help!

Hello
did you check the Analog Input example from IDE too?

paulpaulson:
Hello
did you check the Analog Input example from IDE too?

Yes! Very helpful! And with a lot of other examples and research I got to the point where I can switch via a Analog input. I just can't seem to figure out how, if possible, to read multiple analog inputs at the same time :frowning:

However I'm running into an unanticipated issue, which is that for my project I need to read more than one analogue input value at the same time, which may not be possible with nano?

At exactly the same time will never be possible. So what is the time frame that you can allow? 1 second? 100 millseconds?

You can read the 4 analog inputs into 4 variabeles first and next process them. It's often advised to do two readings of each analog input and discard the first one. Below example for two inputs.

void loop()
{
  int valTurn;
  int valBreak;


  // read the analogInputs
  valTurn = analogRead(A0);
  valTurn = analogRead(A0);

  valBreak = analogRead(A1);
  valBreak = analogRead(A1);

  // process the data here

  Serial.print("valTurn = "); serial.println(valTurn);
  Serial.print("valBreak = "); serial.println(valBreak);

}

Code not compiled and tested.

Note: no experience with the Nano 33 IOT.

Hello,
you can read the analog inputs step by step.

If you just checking if bulbs are on , you can use digital inputs

What is connected to the inputs?

topdesign:
This project is for tail lamps that contain a turn signal, brake lamp, reverse lamp, and "running lamp"

I agree with @Hammy These are all digital signals. they are either ON or Off there is no need to use analog input.

hammy:
If you just checking if bulbs are on , you can use digital inputs

Okay! So I do a time delay between the "checks" on each PIN?
So the problem in more detail is I have to check 4 inputs at the same time to determine a state ie:
Brakes only
Brakes with Running lamp
Turn with brake
Turn with Reverse
Reverse with brake
ETC!
So I was assuming now I'd want to assign an integer to each Pin status, then add them up to determine a state? I'll use numbers so that the state is always a unique value for each possible pin status combination?

Sorry if this is confusing or wrong. New at this :slight_smile:

Hello
or you can use a current sensor for each bulb.

Pin name Pin number Pin int
run A0 1
turn A1 2
brake A2 5
reverse A3 9
Status x1 int total
run 1
turn 2
brake 5
reverse 9
Status x2
run + turn 3
run + brake 6
run + reverse 10
turn + brake 7
turn + reverse 11
brake + reverse 14
Status x3
run + turn + brake 8
run + turn + reverse 12
run + brake + reverse 15
Status x4
run + turn + brake + reverse 17

So I think with would cover all the possible states :o

paulpaulson:
Hello
or you can use a current sensor for each bulb.

I'm not running any bulbs :slight_smile:
This is to replace the complete tail lamp with a matrix display.
The signals are 12v on / off which I will bring down to 5v for the analog inputs.
I hope that makes sense

I can’t see this without thinking that the pin ints should could be 1, 2, 4 and 8.

This would also make the bits of the total correspond to the four signals you are monitoring, might come in handy.

Also you can use digital inputs as has been pointed out. Optoisolators would make it safer and easy. Google “optoisolator arduino” for some basic idea about that.

a7

alto777:
I can’t see this without thinking that the pin ints should could be 1, 2, 4 and 8.

This would also make the bits of the total correspond to the four signals you are monitoring, might come in handy.

Also you can use digital inputs as has been pointed out. Optoisolators would make it safer and easy. Google “optoisolator arduino” for some basic idea about that.

a7

Good point

Pin name Pin number Pin int
run A0 1
turn A1 2
brake A2 4
reverse A3 8
Status x1 int total
run 1
turn 2
brake 4
reverse 8
Status x2
run + turn 3
run + brake 5
run + reverse 9
turn + brake 6
turn + reverse 10
brake + reverse 12
Status x3
run + turn + brake 7
run + turn + reverse 11
run + brake + reverse 13
Status x4
run + turn + brake + reverse 15

:slight_smile:
I ordered a few 4 channel opto isolators just for this

So what the best way to write the value to a integer?

is that a float?

Warning, sloppiness ahead:

void loop()
{
 
if(sensorValue1 = digitalRead(sensorPin1)== HIGH) // read the value from the sensor1:


float state = + 1;
 
delay(10);


if(sensorValue2 = digitalRead(sensorPin2)== HIGH) // read the value from the sensor2:


float state = + 2;


delay(10);


if(sensorValue3 = digitalRead(sensorPin3)== HIGH) // read the value from the sensor3:


float state = + 4;


delay(10);


if(sensorValue4 = digitalRead(sensorPin3)== HIGH) // read the value from the sensor4:


float state = + 8;


delay(10);




if(float state = 1)


RenderFrame (ledarray2),
delay(100);

Close.

“state” can be an integer, better as integer really.

The logic looks OK, but you must initialise “state” to be zero.

Careful:

if(float state = 1)

has “=“ where you probably meant “==“.

You may find that with state bits set, you don’t need sensorValue1..4, the same information is in the bits of state, viz:

if (state & RUN_BIT)…

if you had, e.g.

define RUN_BIT 0x1

and similar defines for you other bits. Makes no difference, just pointing it out.

a7

alto777:
Close.

“state” can be an integer, better as integer really.

The logic looks OK, but you must initialize “state” to be zero.

Careful:

if(float state = 1)

has “=“ where you probably meant “==“.

You may find that with state bits set, you don’t need sensorValue1..4, the same information is in the bits of state, viz:

if (state & RUN_BIT)…

if you had, e.g.

define RUN_BIT 0x1

and similar defines for you other bits. Makes no difference, just pointing it out.

a7

So initialize state

Like:
int state = 0;

in setup or loop?

When I try:

if(int state == 3)
RenderFrame (ledarray1),
delay(100);

I get

expected primary-expression before 'int'

Sorry, still learning :frowning:

You could profit by examining in detail some of the examples you can get in the IDE.

You will never (ever) see

if(int state == 3)

in any example sketch.

it jumps off the page for anyone who has read enough C/C++ code.

That's just one. There would be other benefits.

a7

Thanks for all your help so far!

See my messy sketch below.

This works kind of. By applying 5v to pins 1-4 it will determine a state, then render a frame for a given state. Great!

I don't know how to get it now to ADD "state" values to get a value besides 1,2,4,8

Thanks again! I'm looking into examples a lot

//tail lamp dev v1


#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


int sensorValue1 = A0;  // variable to store the value coming from the sensor
int sensorValue2 = A1;  // variable to store the value coming from the sensor
int sensorValue3 = A2;  // variable to store the value coming from the sensor
int sensorValue4 = A3;  // variable to store the value coming from the sensor

[b](arrays deleted to save space here)[/b]
     
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 state = 0; // initialize state value to zero
  
if(sensorValue1 = digitalRead(sensorPin1)== HIGH) // read the value from the sensor1:


state = 1;
  
delay(10);


if(sensorValue2 = digitalRead(sensorPin2)== HIGH) // read the value from the sensor2:


state = 2;


delay(10);


if(sensorValue3 = digitalRead(sensorPin3)== HIGH) // read the value from the sensor3:


state = 4;


delay(10);


if(sensorValue4 = digitalRead(sensorPin4)== HIGH) // read the value from the sensor4:


state = 8;


delay(10);




if(state == 1)
RenderFrame (ledarray2),
delay(100);




if(state == 2)
RenderFrame (ledarray3),
delay(100);


if(state == 4)
RenderFrame (ledarray1),
delay(100);


if(state == 0)
RenderFrame (ledarray),
delay(100);


if(state == 3)
RenderFrame (ledarray3),
delay(100);


}


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

}