Pages: [1] 2   Go Down
Author Topic: Need some pointers on some code  (Read 1095 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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 Offline
Faraday Member
**
Karma: 89
Posts: 3410
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

turn off the write 3 and turn on write 4

What does that mean?
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3748
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Need some pointers on some code
Code:
void setup() {
//point
//point
//point
}
void loop() {
//point
//point
}

Sorry, couldn't resist.  LOL smiley-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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19020
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3410
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-grin


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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 89
Posts: 3410
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 89
Posts: 3410
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like:

Code:

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

I'm assuming your switch input is active low.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

will give it a bash

Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys I got my switch to work  smiley

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:
//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 Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: