Please help. Sensors and seven segments

Hey , very very knew to this arduino projects and not very technologically advanced. I’m trying to build a counter using touch sensors. These touch sensors will sense when a bottle is placed on it and sense when a bottle is removed. The results of which will be displayed on a seven segment display. Maximum of 4 sensors so maximum number of 4.

I have built the 7 segment and one touch sensor and have found code that when it is pressed the number goes up. I have tried to alter the code changing the states from high to low and asking to decrease counter as well in the “loop” etc but nothing seems to work.

Any help or direction would be much appreciated from more advanced arduino users.

I have included a picture of my seven segment display set up. And my sensors only have a gnd, vcc and I/o connection.

I have been stuck on this for days and can’t get my head around code.

Hi, welcome to the forum.
Can't see your images. Also please post your code and links to the sensors, display and your schematic. The forum guide will help you to post these things in the correct way, so please read that first.

This is the exact code and schematic I have used. This is my first ever project that I need to do.

My sensors that I am using are these Capacitive Touch Sensor Module for Arduino :: Arduino Development Boards & Accessories :: Cricklewood Electronics

The seven segment display I have
https://uk.rs-online.com/mobile/amp/p/led-displays/2358755/?cm_mmc=UK-PLA-DS3A--google--CSS_UK_EN_Displays_And_Optoelectronics_MOB--Displays_And_Industrial_Monitors%7CLed_Displays--PRODUCT_GROUP&matchtype=&pla-394445281901&gclid=EAIaIQobChMIg9KQ0oCG5wIVw7HtCh1IJgNREAQYFCABEgJ_3PD_BwE&gclsrc=aw.ds

I am literally pulling my hair out at this.
I look at the code from electro schematics and can see why it counts up when touched but can’t get my head around adding in/changing the code to include a decrement for removing the bottle off the sensor.

Ok so I have decided on a possible easier method. I plan now to try have code that would loop around the 4 sensors constantly checking their state either high or low. If all four are high it would display 4 then loop again checking for changes etc then if it only sees 3 high it would display 3 etc.

I’m hoping this code would be easier to use that trying to change sensor states on individual sensors in the code.

Any guidance on this would be grateful.

Sounds o.k. When you've made an attempt at this code post it here. Then if you have problems we will be able to help you get it right.

Steve

PaulRB:
Hi, welcome to the forum.
Can't see your images. Also please post your code and links to the sensors, display and your schematic. The forum guide will help you to post these things in the correct way, so please read that first.

I see no images. I see no code. I see no links. Just pasted addresses, nothing can be clicked. If you want help, please don't make it difficult for us to help you, make it easy for us to help you.

andymcgawn:
https://www.electroschematics.com/arduino-segment-display-counter/

Make links clickable:

arduino clickable links.png

arduino clickable links.png

Thanks Steve, I will be attempting the new code tomorrow :slight_smile:
Yeah I will try make all my links clickable in future and tbh the only code I had in the beginning was copied from the first link address. I literally am a beginner, with absolutely no knowledge and need to use the arduino platform for a project.

I will post what I complete tomorrow and will appreciate any help, advice and guidance where possible

https://www.electroschematics.com/arduino-segment-display-counter/

This is the site I was using to set up the 7 segment and one sensor to record an up count when high

I have now added to it at present to see the serial monitor and hooked up all 4 sensors. Initially on the serial monitor if you touch the sensor in pin 9 it changes from a 0 to 1 state which will obviouslt be what I want but touch any other sensor it just flicking between 0 and 1 really quickly. like I said before I am completely knew and don’t know where to go from here.

the code I have attempted to write is

byte numbers[10] = {
B11111100, B01100000, B11011010, B11110010, B01100110,
B10110110, B10111110, B11100000, B11111110, B11100110
};
void setup() {
for(int i = 2; i <= 8; i++) {
pinMode(i, OUTPUT);
}
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
Serial.begin(9600);
}

int counter = 0;
bool go_by_switch = true;
int last_input_value = LOW;

void loop() {
if(go_by_switch) {
int switch_input_value = digitalRead(9);
if(last_input_value == LOW && switch_input_value == HIGH) {
counter = (counter + 1) % 10;
}
last_input_value = switch_input_value;
} else {
delay(500);
counter = (counter + 1) % 10;
}
// writeNumber(counter);
bottle_counter ();
}

void writeNumber(int number) {
if(number < 0 || number > 9) {
return;
}
byte mask = numbers[number];
byte currentPinMask = B10000000;
for(int i = 2; i <= 8; i++) { if(mask & currentPinMask) digitalWrite(i,HIGH); else digitalWrite(i,LOW); currentPinMask = currentPinMask >> 1;
}
}

void bottle_counter () {
int count=0;
int bottle_onoff1;
int bottle_onoff2;
int bottle_onoff3;
int bottle_onoff4;
{

bottle_onoff1=digitalRead(9);
bottle_onoff2=digitalRead(10);
bottle_onoff3=digitalRead(11);
bottle_onoff4=digitalRead(12);
if (bottle_onoff1==HIGH)
{
count=count+1;
Serial.print(count);
Serial.println();
}
else {
Serial.print (count);
Serial.println();
}
if (bottle_onoff2==HIGH)
{
count=count+1;
Serial.print(count);
Serial.println();
}
else {
Serial.print (count);
Serial.println();
}
if (bottle_onoff3==HIGH)
{
count=count+1;
Serial.print(count);
Serial.println();
}
else {
Serial.print (count);
Serial.println();
}
if (bottle_onoff4==HIGH)
{
count=count+1;
Serial.print(count);
Serial.println();
}
else {
Serial.print (count);
Serial.println();
}
}
}

**** I have made the digital write a comment just now to stop it going to display. and don’t know where to go from here

Thanks for posting that link correctly, but it wasn't the only thing you are posting incorrectly...

PaulRB:
The forum guide will help you to post these things in the correct way, so please read that first.

andymcgawn:
https://www.electroschematics.com/arduino-segment-display-counter/

This is the site I was using to set up the 7 segment

Bad news. Just because someone published it on the net does not mean its correct. That display is wired incorrectly. Not in a damaging way but what you will notice (once its working) is that "1" will look much brighter than "8" because the more segments are lit, the dimmer they will be. The mistake is having a single resistor on the common pin. There should be a resistor on each segment pin.

what else posted needs corrected???

the display isn’t a priority in terms of brightness on the project. its more that I count the sensors up and display them somehow on the 7 segment.

my updated code now, on the serial monitor is recognising the state of the 4 different sensors, and it also counting up the states to a total. My struggle is getting that total sent onto the 7 segment.

my new updated code is

byte numbers[10] = {
B11111100, B01100000, B11011010, B11110010, B01100110,
B10110110, B10111110, B11100000, B11111110, B11100110
};
void setup() {
for(int i = 2; i <= 8; i++) {
pinMode(i, OUTPUT);
}
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
Serial.begin(1200);
}

int counter = 0;
bool go_by_switch = true;
int last_input_value = LOW;

void loop() {
int count;

/* if(go_by_switch) {
int switch_input_value = digitalRead(9);
if(last_input_value == LOW && switch_input_value == HIGH) {
counter = (counter + 1) % 10;
}
last_input_value = switch_input_value;
} else {
delay(500);
counter = (counter + 1) % 10;
} */

bottle_counter ();
delay(4000);

writeNumber(counter);
}

void writeNumber(int number) {
if(number < 0 || number > 9) {
return;
}
byte mask = numbers[number];
byte currentPinMask = B10000000;
for(int i = 2; i <= 8; i++) { if(mask & currentPinMask) digitalWrite(i,HIGH); else digitalWrite(i,LOW); currentPinMask = currentPinMask >> 1;

}
}

void bottle_counter () {
int count=0;
int bottle_onoff1;
int bottle_onoff2;
int bottle_onoff3;
int bottle_onoff4;
{

bottle_onoff1=digitalRead(9);
bottle_onoff2=digitalRead(10);
bottle_onoff3=digitalRead(11);
bottle_onoff4=digitalRead(12);

if (bottle_onoff1==HIGH)
{
count=count+1;
Serial.print("sensor 1 = ");
Serial.print(bottle_onoff1);
Serial.println();
}
else {
Serial.print("sensor 1 = ");
Serial.print (bottle_onoff1);
Serial.println();
}
if (bottle_onoff2==HIGH)
{
count=count+1;
Serial.print("sensor 2 = ");
Serial.print(bottle_onoff2);
Serial.println();
}
else {
Serial.print("sensor 2 = ");
Serial.print (bottle_onoff2);
Serial.println();
}
if (bottle_onoff3==HIGH)
{
count=count+1;
Serial.print("sensor 3 = ");
Serial.print(bottle_onoff3);
Serial.println();
}
else {
Serial.print("sensor 3 = ");
Serial.print (bottle_onoff3);
Serial.println();
}
if (bottle_onoff4==HIGH)
{
count=count+1;
Serial.print("sensor 4 = ");
Serial.print(bottle_onoff4);
Serial.println();
}
else {
Serial.print("sensor 4 = ");
Serial.print (bottle_onoff4);
Serial.println();
}
}
Serial.print (count);
}

my serial monitor I have attached is showing the states and the total at bottom. obviously it is with no sensors pressed

andymcgawn:
touch any other sensor it just flicking between 0 and 1 really quickly

That'e because each time it scans the 4 sensors it is printing the count after checking each sensor.

So if the first sensor is HIGH, you see 1 1 1 1 repeated

If the first sensor is LOW and the second sensor is HIGH, you see 0 1 1 1 repeated

If the first & second are LOW and the third is HIGH you see 0 0 1 1 repeated

If the first is HIGH and the third is HIGH you see 1 1 2 2 repeated and so on.

So I suggest you only print the value of count after all 4 sensors have been checked.

andymcgawn:
what else posted needs corrected???

I'm not going to tell you or help further until you read the forum guide. When you read the guide you will know what else to correct.

Hi Paul, many thanks for taking the time to reply. As per your responses, yes exactly what you said was happening in the first code I attempted. I altered this in the second code I posted and the serial monitor now displays the correct highs and lows and counts them.
I have read the forum guide, I hope I read the correct thing, regarding posting code, punctuation, links etc and I really appreciate any help I can get.

This is the first time I have even used a forum for guidance so I was unaware their was an ‘etiquette’ to follow.

Having the correct count on the serial monitor made me feel like I was getting somewhere with my project, but attempting to write that count onto the 7 segment display is becoming a real struggle. Using the code from electroschematics button increment counter, I have been trying to look at it and work out how it was sending the button count to the display. Obviously has something to do with digitalWrite. But Im not sure at all how I get my count to it.

Again, I do really appreciate the guidance

Andrew mcgawn
Scotland