Go Down

Topic: Need some pointers on some code (Read 1 time) previous topic - next topic

oniudra_-

Feb 01, 2013, 10:05 pm Last Edit: Feb 01, 2013, 10:48 pm by oniudra_- Reason: 1
Good day to all.

Im a real newbie to all of this coding but Im learning each day   :smiley-roll-sweat:

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

Code: [Select]
//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);
  }
}
 

Arrch


HazardsMind

#2
Feb 01, 2013, 10:15 pm Last Edit: Feb 01, 2013, 10:25 pm by HazardsMind Reason: 1
Quote
Need some pointers on some code

Code: [Select]

void setup() {
//point
//point
//point
}
void loop() {
//point
//point
}


Sorry, couldn't resist.  LOL XD

Ok joking aside, look into direct port manipulation, so you can use binary (0b00000000 , 0b00100101 ... 0b11111111) to change your output states.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Nick Gammon

Read this before posting a programming question


Please 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

oniudra_-

Thanks...like I said Im a real newbie. :smiley-zipper:

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  :D

oniudra_-

Ok I will...sorry I found it like this on the net

Arrch


Thanks...like I said Im a real newbie. :smiley-zipper:

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  :D



You still need to define your requirements a little more. Do you mean the switch will act as an override?

oniudra_-

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

Arrch


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?

oniudra_-

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)

Arrch

Still not sure I understand, but here is a general answer:

Code: [Select]
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.

PeterH

Something like:

Code: [Select]


if((digitalRead(switchPin) == LOW) && (gear == 3))
{
    gear = 4;
}


I'm assuming your switch input is active low.
I only provide help via the forum - please do not contact me for private consultancy.

oniudra_-

I see thanks....yes will be low state

will give it a bash

Thanks

oniudra_-

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


Code: [Select]
//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);
  }
}
 

Chaul

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy