system
June 27, 2015, 3:39pm
1
i'm totally new to this stuff so i'm sure this is a really simple question but i'm trying to make a light dimmer with a pot that displays a sentence once when the brightness is in a certain range. Right now it only works for when my analogRead(pot) is zero...
const int pot = 0;
int volt = 0;
const int Rled = 9;
const int Gled = 10;
const int Bled = 11;
int oldVal = 0;
void setup()
{
Serial.begin(9600);
pinMode(Rled, OUTPUT);
pinMode(Gled, OUTPUT);
pinMode(Bled, OUTPUT);
}
void loop()
{
volt = analogRead(pot);
if (volt != oldVal)
{
if (volt == 0)
{
Serial.println("Light is Off.");
}
else if (volt > 0 && volt <= 341)
{
Serial.println("Light is dim.");
}
else if (volt >= 682)
{
Serial.println("Light is Bright!");
}
else
{
Serial.println("Light is medium.");
}
oldVal=volt;
}
analogWrite(Rled, analogRead(pot)(255/1023.0));
analogWrite(Gled, analogRead(pot) (255/1023.0));
analogWrite(Bled, analogRead(pot)*(255/1023.0));
}
LarryD
June 27, 2015, 3:45pm
2
Put a "Serial.println(volt) ;" after "volt = analogRead(pot) ;"
what is printed?
system
June 27, 2015, 4:17pm
3
doint that gives me a line that continuously prints an update of the analog pin reading even it it doesnt change
i.e
0
0
0
0
23
23
but what i want is for my text line to print only when i move the potentiometer
LarryD
June 27, 2015, 4:21pm
4
how is your POT wired!
Change
const int pot = 0;
To
#define pot A0
system
June 27, 2015, 4:29pm
5
my pot has 5V coming in from the arduino and then going to ground with the middle being wired to the A0 pin
LarryD:
how is your POT wired!
Change
const int pot = 0;
To
#define pot A0
Do this, As Larry said, should fix your problem.
system
June 27, 2015, 5:11pm
7
I tried that and it did not work, am I resetting oldVal to volt in the wrong part of the loop or something?
but what i want is for my text line to print only when i move the potentiometer
The below is for controlling servos with pots, but it contains code to limit continuous printing when a pot value is not significantly changed.
//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor
#include <Servo.h>
Servo myservo1; //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
int potpin1 = 0; //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;
int newval1, oldval1; //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;
void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
Serial.println("testing multi pot servo");
}
void loop()
{
newval1 = analogRead(potpin1);
newval1 = map(newval1, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
myservo1.write(newval1); //position the servo
Serial.print(newval1); //print the new value for testing
Serial.print("a,");
oldval1=newval1; //set the current old value
}
newval2 = analogRead(potpin2);
newval2 = map(newval2, 0, 1023, 0, 179);
if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){
myservo2.write(newval2);
Serial.print(newval2);
Serial.print("b,");
oldval2=newval2;
}
newval3 = analogRead(potpin3);
newval3 = map(newval3, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){
myservo1.write(newval3);
Serial.print(newval3);
Serial.print("c,");
oldval3=newval3;
}
newval4 = analogRead(potpin4);
newval4 = map(newval4, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){
myservo1.write(newval4);
Serial.print(newval4);
Serial.print("d,");
oldval4=newval4;
}
newval5 = analogRead(potpin5);
newval5 = map(newval5, 0, 1023, 0, 179);
if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){
myservo1.write(newval5);
Serial.print(newval5);
Serial.print("e,");
oldval5=newval5;
}
delay(50); //to slow loop for testing, adjust as needed
}
system
June 27, 2015, 5:29pm
9
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is dim.
Light is Off.
This is what i get when the pot starts at full resistance, i turn the light on and then turn it off again. All the conditions fail to print one update line except for light is off.
void loop()
{
volt = analogRead(pot);
if (volt != oldVal)
{
if (volt == 0)
{
Serial.println("Light is Off.");
oldVal = volt;
}
else if (volt > 0 && volt <= 341)
{
Serial.println("Light is dim.");
oldVal = volt;
}
else if (volt >= 682)
{
Serial.println("Light is Bright!");
oldVal = volt;
}
else
{
Serial.println("Light is medium.");
oldVal = volt;
}
oldVal = volt;
}
this is my loop
LarryD
June 27, 2015, 6:23pm
10
if (volt == 0)
This will more than likely never be 0 as there is always some noise in the system.
Try
if (volt < 10) // or some other low value.
system
June 27, 2015, 6:25pm
11
Change
const int pot = 0;
To
#define pot A0
Why?
If anything, change it to const byte pot = A0;
but I cannot see any logic in your approach.
LarryD
June 27, 2015, 7:06pm
12
IMO it adds to the readability.
But
const byte pot = A0;
is better