Need some pointers on some code

Good day to all.

Im a real newbie to all of this coding but Im learning each day :cold_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

//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);
  }
}

oniudra_-:
turn off the write 3 and turn on write 4

What does that mean?

Need some pointers on some code

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.

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.

Thanks...like I said Im a real newbie. :zipper_mouth_face:

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:

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

oniudra_-:
Thanks...like I said Im a real newbie. :zipper_mouth_face:

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:

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

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

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.

What do you mean "3rd" display? I thought you only had one digit?

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)

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.

Something like:

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

I'm assuming your switch input is active low.

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

will give it a bash

Thanks

Thanks guys I got my switch to work :slight_smile:

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);
  }
}

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.

Thanks a mill

This littel setup I have here seems to work pretty well.. it never seem to miss a step when I move the potentiometer and the only
thing it has a problem( apart from the fade function) is the bounce when I push the switch....Im still busy looking at how to programe the debounce in so iv lifted the delay for now.

Iv seen some arrays where they look somthing like this ( 0,1,0,0,1,1,1) tryed to change it here but no luck. will go thru the array tutorial a bit more.

Also seems to complain about sensorReading now which it had no problem with before I added this new function in.

The compiler either complains, or it doesn't. There is nothing ambiguous about it.

Your collection of if statements should be if/else if statements. There should be no overlap between one block's condition and another block's condition, so only one block should ever be executed, so if/else if/else if is more appropriate than if/if/if.

I agree wholeheartedly with the comment that you need to use Tools + Auto Format. Putting each { on a new line might help you see the program structure better, too. It certainly does for me.

If "seems to" should be "does", post the whole code and the exact error message.