1607Views19Replies

Author Options:

Need help with arduino code.? Answered

Alright I asked this question on the arduino forums but nobody is giving me a straight answer, and it's getting annoying. I am using the capsense library on an attiny85, I want to count the number of times an even happens, in this case the capacitive censor exceeding a certain amount. Then based on that number I want it to turn on one led, or two, and so forth, to keep count. Can someone tell me how the f*****g get this to work? I try to use serial to debug and just get "Serial" not defined in this scope. Heres the code: #include <SoftwareSerial.h>
#include <CapacitiveSensor.h>


CapacitiveSensor   cs_1_2 = CapacitiveSensor(1,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int ledpin = 0;
int countnumber = 0;

void setup()                   
{
  cs_1_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  pinMode (ledpin, OUTPUT);
}

void loop()                   
{
  long start = millis();
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 99) {
   countnumber == 1;
  }
  if (countnumber = 1) {
    digitalWrite(ledpin, HIGH);
  }
}

Discussions

The forums are retiring in 2021 and are now closed for new topics and comments.
0
madhavdtoshniwal
madhavdtoshniwal

4 months ago

Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
Sketch uses 4230 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 241 bytes (11%) of dynamic memory, leaving 1807 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0xf1
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xf1
Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#up... for suggestions.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


How do I solve it.

0
hasithasathsara13
hasithasathsara13

6 months ago

can you help me to change this Arduino code. The code is to display 1-5 in 7 segment display.so I want to change this code,when it goes to 5 in the 7 segment display I want to light up an led please help me to change this


const int a = 8; //For displaying segment "a"
const int b = 9; //For displaying segment "b"
const int c = 4; //For displaying segment "c"
const int d = 5; //For displaying segment "d"
const int e = 6; //For displaying segment "e"
const int f = 2; //For displaying segment "f"
const int g = 3; //For displaying segment "g"
bool bPress = false;
const int buttonPin = 10;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// put your setup code here, to run once:
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G
pinMode( buttonPin , INPUT_PULLUP );
Serial.begin(9600);
displayDigit(buttonPushCounter);
}
void loop() {
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter++;
if( buttonPushCounter > 5) buttonPushCounter =0 ;
Serial.println("on");
if( buttonPushCounter > 5) pinMode(LED_BUILTIN, 13);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if( bPress ){
turnOff();
displayDigit(buttonPushCounter);
}
}
void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);
//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);
//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);
//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);
//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);
//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);
//Conditions for displaying segment g
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);
}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

0
RitheshP
RitheshP

1 year ago

Input Input > 2sec = B light,
No IP = C
Sensor as an input
How to write program for this

0
l.alteneiji
l.alteneiji

1 year ago

#error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
^
exit status 1
Error compiling for board Generic ESP8266 Module.


I got this .. Can someone help me?!

0
RakshitT6
RakshitT6

2 years ago

I want to write a code for this prob. I have ir sensor to detect a line and ultrasonic sensor to detect the distance btw the obstacles and the robo .Please if anyone can get me the code it's urgent and the problem is it's getting wrong everytime I tried . In this prob . We have to detect the obstacle first and then take a left turn then using robo arm we have to pick the object and then take a right turn then we have to place it in the box. Dimensions are been written in the pic. Plz help me

IMG-20180407-WA0005.jpg
0
steveastrouk
steveastrouk

7 years ago

Several things:
One, the library is now called "CapSense.h"
Two, all the arguments are therefore called "CapSense"

0
XOIIO
XOIIO

Answer 7 years ago

I've gotten it to work just fine turning an LED on when it's touched, the problem lies that the LED does not turn on when I have the script set up as above, countnumber's value does not seem to change. Is there an easier way or more straightforward way to do this because I can't get the darn thing to work for the life of me.

0
steveastrouk
steveastrouk

Answer 7 years ago

So where did you get your copy of the sensor library - the Arduino.cc site version is called "CapSense", and works on Arduino 1+

0
XOIIO
XOIIO

Answer 7 years ago

I just googled arduino capsense and got this page

http://playground.arduino.cc//Main/CapacitiveSensor?from=Main.CapSense

They did have to change the name because cypress semiconductor has that trademarked.

0
bansuri.shankar
bansuri.shankar

Answer 7 years ago

Hi, in your code you mention cs_1_2.....would these be the legs 6 and 7 of the atTiny85 as per the pin diagram of the chip or would these mean Analog_input_1 and Analog_input_2 which would be the legs 7 and 4 of the chip...Can you please clarify..I'm having issues even in getting a basic LED to be enabled by touch....

0
steveastrouk
steveastrouk

Answer 7 years ago

The relevant declaration is her
CapacitiveSensor cs_1_2 = CapacitiveSensor(1,2);

The pin numbers are identified in the call. The second pin (pin 2 here) is the input pin from the capacitor

0
mpilchfamily
mpilchfamily

7 years ago

First of all countnumber ==1 isn't storing 1 to the variable. If you want to store a number to a variable like that you only use a single =. When you check to see if the variable equals a number you use ==. But if you intend to keep track of mutiple presses you will want to use countnumber ++ to add 1 each time it detects a touch. Then where you have the LED light be sure to set a delay for how long you want the LED to stay on then set your variable back to zero.

0
XOIIO
XOIIO

Answer 7 years ago

Ok I've gotten it to work using countnumber, however since I can only get it to work inside the loop it resets, I want the variable to keep incrementing.

void loop()
{
int countnumber = 0;
long start = millis();
long total1 = cs_1_2.capacitiveSensor(30);
if (total1 < 99) {
countnumber++;
Serial.print("touched");
}
if (countnumber == 1) {
digitalWrite(ledpin, HIGH);
} else {
digitalWrite(ledpin, LOW);
}
}

0
steveastrouk
steveastrouk

Answer 7 years ago

Then don't initialize the counter in the loop. Use a while statement around the code. Is it just supposed to loop 0..99, 0..99 ?

0
XOIIO
XOIIO

Answer 7 years ago

Its just supposed to increase each time it detects a touch. I've tried initializing it outside the loop but then it doesn't work at all.

heres the post in the arduino forums.
http://arduino.cc/forum/index.php/topic,157855.0.html

0
steveastrouk
steveastrouk

Answer 7 years ago

The guys in there are more patient than I am. Nobody's giving you a straight answer, because it looks like you aren't giving them decent information. Your code is terrible. You need to practice, you need to learn, by yourself and by listening to experts.

FIRST RULE OF CODING
Write down a description of what you want to do, so far you don't seem to know yourself:
Here's a very rough idea of what I mean.

Intialise cap sense.
while true
{
Read pin,
if pin cap < (some value) its touched, count it.
increment a counter.
if the counter is > 1, turn the led on,
while (pin touched) {wait and do nothing}
}

This is called pseudo code, its kind of a written description of what you are trying to do, kind of in your programming language.

Sort out making the LED go off on the NEXT touch.

0
XOIIO
XOIIO

Answer 7 years ago

Well I haven't touched arduino code before today but I do now what I want to do, the problem is that the counter is not incrementing and having a global variable does not work, when from what I can tell there is no reason that it shouldn't.

0
steveastrouk
steveastrouk

Answer 7 years ago

Have you actually read what I wrote ?

If you know what to do, write some pseudocode that explains it to yourself, post it here.

Have you implemented what I said to do, with a while loop around your code, after the counter is set to zero ?

At the moment there is EVERY reason why it won't work - YOU ARE INITIALISING THE COUNTER EVERY TIME THE CODE LOOPS THROUGH LOOP()

What RANGE does total1 vary over between touch and not touch ?

0
XOIIO
XOIIO

Answer 7 years ago

have you read what I wrote?

I stated in an answer above, if I initialize the counter outside the loop, then the LED stays lit solid, and does not change if you touch the sensor or not, it stays on. It doesn't matter if I do it in void setup() or above that, it stays on.

I don't know what range total1 varies over, but it turned the LED on just fine if I had digitalwrite there instead of changing the countnumber, so using it to trigger something else would work the same. Besides, changing what the total needs to be greater than does nothing because with the variable declared outside the loop the led stays on.

0
steveastrouk
steveastrouk

Answer 7 years ago

WHICH loop ? You haven't shown me any code WITH a loop in it, apart from "loop() "

FIND OUT how much total1 varies. You've got serial debugging USE IT

STILL no pseudocode.