Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: February 01, 2013, 04:05:24 pm » |
Good day to all. Im a real newbie to all of this coding but Im learning each day  I guess this code would seem pretty basic to you guys so here's what Im busy with. I found this code on the net and Im controlling the 7 segment to count up and down when you turn the potentiometer knob. Iv changed it a bit to get what I want on the display, cut down the counts to only 7 diffrent numbers aswell. All works fine but want to add in a switch to turn off the write 3 and turn on write 4 and coil a relay. Iv still got 5 digital pins open. Arduino uno 7 segment display ( 1 digit) Potentiometer Relays and transistor Here is the code Thanks //Potentiometer controlling 7-segment LED
//declare variables const int pot = A0; // Potentiometer to Analog pin A0
void setup() { // To 7 segment display pinMode(2, OUTPUT); //3-A pinMode(3, OUTPUT); //9-B pinMode(4, OUTPUT); //6-C pinMode(5, OUTPUT); //5-D pinMode(6, OUTPUT); //4-E pinMode(7, OUTPUT); //2-G pinMode(8, OUTPUT); //1-F }
void loop() { delay(10); int sensorReading = analogRead(pot); delay(10);
// Segment shows P on start if(sensorReading == 0){ //write 0 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 0); delay(10); } // Segment shows R when potentiometer is turned if(sensorReading > 0 && sensorReading <= 113){ //write 1 digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } // Segment shows N if(sensorReading > 113 && sensorReading <= 226){ //write 2 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } // Segment shows D if(sensorReading > 226 && sensorReading <= 339){ //write 3 digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(8, 0); delay(10); } // Need it to turn off the write 3 when a switch is pushed at this point and show write 4 // Iv removed write 4 here so at this point it skips this number. // Also looking for it to strik a relay at the same time // Segment shows 2 if(sensorReading > 452 && sensorReading <= 565){ //write 5 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(8, 0); delay(10); } // Segment shows L if(sensorReading > 565 && sensorReading <= 678){ //write 6 digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 1); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } }
|
|
|
|
« Last Edit: February 01, 2013, 04:48:53 pm by oniudra_- »
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #1 on: February 01, 2013, 04:11:28 pm » |
turn off the write 3 and turn on write 4
What does that mean?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1589
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #2 on: February 01, 2013, 04:15:11 pm » |
Need some pointers on some code void setup() { //point //point //point } void loop() { //point //point } Sorry, couldn't resist. LOL  Ok joking aside, look into direct port manipulation, so you can use binary (0b00000000 , 0b00100101 ... 0b11111111) to change your output states.
|
|
|
|
« Last Edit: February 01, 2013, 04:25:12 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: February 01, 2013, 04:28:13 pm » |
Read this before posting a programming questionPlease edit your post, select the code, and put it between [code] ... [/code] tags. You can do that by hitting the # button above the posting area.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #4 on: February 01, 2013, 04:35:37 pm » |
Thanks...like I said Im a real newbie.  Iv got the numbers 1 to 7 showing as I turn the knob in one direction and when I turn it back it counts back down. At the 3rd display when turning the knob I need to then flip a switch and it shows up the 4 th digit without turning the knob. hang on,,, I am the knob here 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #5 on: February 01, 2013, 04:38:18 pm » |
Ok I will...sorry I found it like this on the net
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #6 on: February 01, 2013, 04:44:07 pm » |
Thanks...like I said Im a real newbie.  Iv got the numbers 1 to 7 showing as I turn the knob in one direction and when I turn it back it counts back down. At the 3rd display when turning the knob I need to then flip a switch and it shows up the 4 th digit without turning the knob. hang on,,, I am the knob here  You still need to define your requirements a little more. Do you mean the switch will act as an override?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #7 on: February 01, 2013, 04:55:31 pm » |
Thats it yes Im looking to have a condition in there of some sort so that it removes that 3 rd display with out moving the potentiometer.
Im busy making a gear shifter display and the gear shifter don't move only in a up and down motion.... got a right movement which Im trying to use a switch for that
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #8 on: February 01, 2013, 05:12:13 pm » |
Thats it yes Im looking to have a condition in there of some sort so that it removes that 3 rd display with out moving the potentiometer. What do you mean "3rd" display? I thought you only had one digit?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #9 on: February 01, 2013, 05:23:50 pm » |
Will state that better......Got a one digit 7 segment and mean it like this
No 0= Park ( P) No 1= Reverse ( R ) No 2= Nutral (N ) No 3 = Drive ( D )
No 4 = Three (3) need the three to work from a switch and blank out drive without moving the potentiometer. No 5 = Two (2) No 6 = Low (1)
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #10 on: February 01, 2013, 05:28:37 pm » |
Still not sure I understand, but here is a general answer: if switch is pressed do your override stuff else check potentiometer do potentiometer stuff If you need help reading the state of a switch, just take a look ant many of the examples posted.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6411
-
|
 |
« Reply #11 on: February 01, 2013, 05:32:08 pm » |
Something like: if((digitalRead(switchPin) == LOW) && (gear == 3)) { gear = 4; }
I'm assuming your switch input is active low.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #12 on: February 01, 2013, 05:48:55 pm » |
I see thanks....yes will be low state
will give it a bash
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #13 on: February 03, 2013, 04:14:21 am » |
Thanks guys I got my switch to work  Sorry for all these basic questions but I suffer with dyslexia so takes me a bit of time. always willing to ask and learn tho. Iv added in a fade on the 7 segment with the help of opensource hardware junkies.com...he's site is great. step by step.. Im trying to fade the whole array but it seem to have a problem adding in the 2 to 8 digital pins for some reason. Also seems to complain about sensorReading now which it had no problem with before I added this new function in. Thanks //Potentiometer controlling 7-segment LED
//declare variables const int pot = A0; // Potentiometer to Analog pin A0 const int SW = 9; // Switch in const int analogInPin = A1; const int analogOutPins = 2,3,4,5,6,7,8;
int fadeValue = 0; int outputValue = 0;
void setup() { // To 7 segment display pinMode(2, OUTPUT); //3-A pinMode(3, OUTPUT); //9-B pinMode(4, OUTPUT); //6-C pinMode(5, OUTPUT); //5-D pinMode(6, OUTPUT); //4-E pinMode(7, OUTPUT); //2-G pinMode(8, OUTPUT); //1-F pinMode(9, INPUT_PULLUP); // Switch in
Serial.begin(9600); }
void loop() { delay(10); int sensorReading = analogRead(pot); delay(10); int digitalValue = digitalRead(SW); delay (100); fadeValue = analogRead(analogInPin); //Map to range of analog out outputValue = map(fadeValue, 0,1023, 0, 255); // change the analog out value analogWrite(analogOutPins, outputValue); Serial.print("sensor = "); Serial.print(fadeValue); Serial.print("\t output = "); Serial.println(outputValue); delay(2); } // P
if(sensorReading == 0){ //write 0 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 0); delay(10); } // R if(sensorReading > 0 && sensorReading <= 113){ //write 1 digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } // N if(sensorReading > 150 && sensorReading <= 226){ //write 2 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(5, 1); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } // D if(sensorReading > 226 && sensorReading <= 290){ //write 3 digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(8, 0); delay(10); } // Segment shows 3
if(digitalRead(SW)==LOW){ digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(5, 0); digitalWrite(6, 1); digitalWrite(7, 1); digitalWrite(8, 0); delay(200); }
// Segment shows 2 if(sensorReading > 350 && sensorReading <= 565){ //write 5 digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(8, 0); delay(10); } // Segment shows L if(sensorReading > 565 && sensorReading <= 678){ //write 6 digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 1); digitalWrite(5, 0); digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(8, 1); delay(10); } }
|
|
|
|
|
Logged
|
|
|
|
|
Finland
Offline
Jr. Member
Karma: 1
Posts: 84
|
 |
« Reply #14 on: February 03, 2013, 05:08:07 am » |
Please work on indentation. I'm having trouble reading what the loop() does. It seems like either a function definition was missing after the loop and before the first, or there is one extra bracket in the middle.
Also, if you are reading a potentiometer, you can't really trust that it's steadily at 0 for a long period of time. It could keep switching between states P and R. Does it do anything if sensorReading was > 678? It would seem to keep the previous state, whatever it was.
You can shorten the code by wrapping the digitalWrites into a function with the 7 pins' states and the delay as parameters. That's 2 or 8 parameters, your choice.
|
|
|
|
|
Logged
|
|
|
|
|
|