I know that it is not what you want to do. I asked the question to make you think about what you are doing
If the state of the current LED is ON
- What is it that you want to do ?
- How do you do it ?
I know that you know the answers to both questions
I know that it is not what you want to do. I asked the question to make you think about what you are doing
If the state of the current LED is ON
I know that you know the answers to both questions
Dear Arduino community,
Sooo, I have good news. the LEDs are working now and dispalying the intensity values from the array (yay!). Buuut, as soon as I upload the programm all the leds are turned on and do not react to any keypad input.
#include "SoftPWM.h"
#define OFF 0 //set these HGH or LOW to suit your LED wiring
#define ON 255
struct ledData //data needed for each LED
{
byte pinNum;
byte currentState;
char key;
int intensity; // added integer values for the light intensities
};
ledData leds[] = { //initialiase the LED data + added the actual led values to the array
{ 2, OFF, 'A', 8 },
{ 3, OFF, 'B', 8 },
{ 4, OFF, 'C', 16 },
{ 5, OFF, 'D', 16 },
{ 6, OFF, 'E', 32 },
{ 7, OFF, 'F', 32 },
{ 8, OFF, 'G', 64 },
{ 9, OFF, 'H', 64 },
{ 10, OFF, 'I', 128 },
{ 11, OFF, 'J', 128 },
{ 12, OFF, 'K', 255 },
{ 13, OFF, 'L', 255 }
};
const byte LED_COUNT = sizeof(leds) / sizeof(leds[0]); //calculate the number of LEDs
void setup() {
Serial.begin(115200);
for (int c = 0; c < LED_COUNT; c++) {
SoftPWMBegin();
SoftPWMSet(leds[c].pinNum, 0); // changed instead of pinMode
}
showLeds(); //show LEDs in there initial state
}
void loop() {
if (Serial.available()) //wait for Serial input
{
char c = Serial.read(); //get a character
for (int x = 0; x < LED_COUNT; x++) //search for the character in the LED data
{
if (leds[x].key == c) //if there is a match
{
leds[x].currentState = !leds[x].currentState; //change the state of matching LED
showLeds(); //display the LEDs in their current state
}
}
}
}
void showLeds() {
for (int c = 0; c < LED_COUNT; c++) //for each LED
{
if (leds[c].currentState == OFF)
{
SoftPWMSet(leds[c].pinNum, 0);
//Serial.println("LED is OFF");
}
else (leds[c].currentState == ON);{
SoftPWMSet(leds[c].pinNum, leds[c].intensity);
//Serial.println("LED is ON");
}
}
}
Thank you for bearing with me
No problem with your slow progress but I hope that you are learning something
I am short of time at the moment but will look at your latest sketch later
As an aside, can you tell me in what context the LED display will be used and will the user ever want/need to change the state of an LED that is not adjacent to the one previously changed ?
Yes Im defiently learning something All I want to do is written in "Responsiveness to light: Phototaxis". Im actually a Biology student and I work with honeybees. My projects consists in building an arena like written in the PDF.
JAR52404-9-10.pdf (634,9 KB)
I just have to turn off and on the individual leds one at a time, starting from the lowest intensity, to study the phototaxis of honeybees while being exposed to certain insecticides.
So it will be like this.
All leds are off
Led1 (lowest intensity) ON
Bee moves toward Led1
when bee reaches the Led1 --> LED1 OFF
LED2 (with the same intensity as Led1) goes ON
bee moves toward led2
process is cycles for all the other 12 leds.
It would be super cool if its all automatic by using a sensor that notices when the bee is at the LED, but it will take way too much programming/time.
I am back on the case
Look at the else clause in your showLeds() function
else (leds[c].currentState == ON);{
SoftPWMSet(leds[c].pinNum, leds[c].intensity);
//Serial.println("LED is ON");
}
It looks like you are trying to test whether the current LED state is ON but instead you are turning it on
There is no need to test its state, even if you do it correctly, because you already know that it is not OFF so it must be ON
Try this version of the showLeds() function and spot how it differs from your original
void showLeds()
{
for (int c = 0; c < LED_COUNT; c++) //for each LED
{
if (leds[c].currentState == OFF)
{
SoftPWMSet(leds[c].pinNum, 0);
//Serial.println("LED is OFF");
}
else //the LED is not OFF so it must be ON !
{
SoftPWMSet(leds[c].pinNum, leds[c].intensity);
//Serial.println("LED is ON");
}
}
}
Yes that makes toatally sense and I also tried out the code that you wrote but now it does nothing... thats why I figured I had to test it.
So the difference is that in the else
statement I am not trying to test the if the LED is On.
if I try to change the comparison operators ==
with just an equal sing, the LEDs are all ON again.
void showLeds()
{
for (int c = 0; c < LED_COUNT; c++) //for each LED
{
if (leds[c].currentState = OFF)
{
SoftPWMSet(leds[c].pinNum, 0);
//Serial.println("LED is OFF");
}
So maybe I have to work out something with the comparison operators?But It actually makes sense because I the LEDs are set to OFF by default:
void setup() {
Serial.begin(115200);
for (int c = 0; c < LED_COUNT; c++) {
SoftPWMBegin();
SoftPWMSet(leds[c].pinNum, 0); // changed instead of pinMode
}
showLeds(); //show LEDs in there initial state
}
There is a fundamental difference between = and ==
= is used to set the value of a variable
== is used to compare 2 values for equality
I don't understand why you think you need to use =
Use my version of the showLeds() function from post # 45. I know that it works
I have just noticed that here is at least one other problem in your version of the sketch and that is
#define OFF 0 //set these HGH or LOW to suit your LED wiring
#define ON 255
The logic of the sketch depends on the LEDs being ON or OFF and it flips their state using
leds[x].currentState = !leds[x].currentState; //change the state of matching LED
That will not work if you use values of 0 and 255. Change them to LOW and HIGH
Could you maybe share the sketch were the code works so that I can compare any other errors? I tried to do the changes you said.
#include "SoftPWM.h"
#define OFF LOW //set these HGH or LOW to suit your LED wiring
#define ON HIGH
This is the version that I have working
I have tested it with 8 LEDs because that was convenient. Replace the definition of the leds array with yours and try it
//UKHB version
#include <SoftPWM.h>
#define OFF LOW //set these HGH or LOW to suit your LED wiring
#define ON HIGH
struct ledData //data needed for each LED
{
byte pinNum;
byte currentState;
char key;
int intensity;
};
ledData leds[] = {
//initialiase the LED data
{ 2, OFF, 'A', 8 },
{ 4, OFF, 'B', 8 },
{ 7, OFF, 'C', 16 },
{ 8, OFF, 'D', 16 },
{ 10, OFF, 'E', 32 },
{ 11, OFF, 'F', 32 },
{ 12, OFF, 'G', 64 },
{ A4, OFF, 'H', 64 },
};
const byte LED_COUNT = sizeof(leds) / sizeof(leds[0]); //calculate the number of LEDs
void setup()
{
Serial.begin(115200);
SoftPWMBegin();
showLeds(); //show LEDs in there initial state
}
void loop()
{
if (Serial.available()) //wait for Serial input
{
char c = toupper(Serial.read()); //get a character
for (int x = 0; x < LED_COUNT; x++) //search for the character in the LED data
{
if (leds[x].key == c) //if there is a match
{
leds[x].currentState = !leds[x].currentState; //change the state of matching LED
}
}
showLeds(); //display the LEDs in their current state
}
}
void showLeds()
{
for (int c = 0; c < LED_COUNT; c++) //for each LED
{
if (leds[c].currentState == ON)
{
SoftPWMSet(leds[c].pinNum, leds[c].intensity);
}
else
{
SoftPWMSet(leds[c].pinNum, 0);
}
}
}
Hello!
Yes it works on my PC too. Thank you very much for your help! I really did not thought that this community is so helpful and friendly.
I found out that I misplaced SoftPWMSet(leds[c].pinNum, 0);
in the setup code
Cheers and have a good easter!
I am glad that it finally works. To be honest it worked for me a long while ago but I wanted you to learn something by doing things yourself
Now that you have it working can I suggest an improvement ? You said earlier
You do not need 12 different character inputs to do that, just a single one will do. Each time the character is entered the current LED would be turned off and the next one turned on.
Does that sound interesting ?
No thank you I just need to turn them on and off multiple times.
Cheers!
I have just read the PDF that you linked to so can see why you decided to use 12 separate characters as it is a very understandable solution
The single character solution could still work by alternating between the 2 LEDs of the same intensity the required number of times before moving to the next pair of LEDs at the next intensity. In fact you could control the whole thing with a single button input but as long as what you have now meets your needs then there is, of course, no problem
Good luck with your experiment going forward
Thank you for the input! I also have to connect a camera and it would be ideal to start recording when I press a button but I still have to figure the details out...
Come back if/when you need any more help
Appreciate you!
It's the bees that I feel sorry for !
No sooner do they get where they want to than you turn off the LED and attract them somewhere else
Hahaha yeah I kinda make a fool of themselves
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.