Trying to read the current position of a flex sensor and store it as a variable? NANO ATmega328p

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to write a code to read the current position of a flex sensor when a button is pushed and use that data to pulse a piezo. I set it up to read the flex sensor and to alarm when it gets 100 or below, that works! So what I want to do is integrate a button where you push it to set the current state of the flex sensor and then when its moved and is less than the number logged when the button was pressed then it alarms. I used to write in Basic and quick basic and am now (many years later in life) trying to learn C and am struggling. (I also attached diagram - 9v batt. And the red. Is 10k) I'm using a NANO ATmega328p - here is my code so far:

 

<

int flexSensorPin = A0;//analog pin 7= tiny2=A1
const int Piezo = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status
void setup() {
  pinMode (Piezo, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}
void loop() {
  int val = 0;
  val = digitalRead(inPin)
        ;  int flexSensorReading = analogRead(flexSensorPin);
  Serial.println(flexSensorReading);
  if (flexSensorReading <               100) {
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
    delay(100);
    digitalWrite(Piezo, LOW);

  }
  delay(250);
}

>

Attachment(s): 

Dave E.

Last Edited: Mon. May 28, 2018 - 05:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
const int flexSensorPin = A0;//analog pin 7= tiny2=A1 >>I do not understand<<
const int Piezo = 3; //digital pin 3 = tiny0
const int inPin = 2; // Push Button to set current Bend Status
int oldFlex = -999;
void setup() {
  pinMode (Piezo, OUTPUT);
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}
void loop() {

  int val = 0;
  val =digitalRead(inPin);
  int flexSensorReading = analogRead(flexSensorPin);
  if (val != 0) {
    oldFlex = flexSensorReading;

    delay(100);
    SerialPrint ("old Flex :");
    Serial.print( odlFlex);
  }
  SerialPrint (" Flex :");
  Serial.println(flexSensorReading);
  delay (500);

  if (flexSensorReading <    oldFlex) { // at init, oldFlex is <0 : cannot occur at init
    Serial.println("Alarm! Set Angle Exceeded");
    digitalWrite(Piezo, HIGH);
    delay(100);
    digitalWrite(Piezo, LOW);

  }
  delay(250);

}

Does this (simplified) version display values of flexSensor?

Edited : Arduino is not the best way of teaching C (rather simplified C++):  a native compiler on a PC/RPi is more comfortable (no hardware issues; one concentrates on C -or C++-)

Edited : you almost wrote that : I had to add 4 simple lines and defined the ADC0 line as a const (bet it is soldered; val might be declared a a byte , too -yerst are faster, eat less flesh/RAM).

Last Edited: Mon. May 28, 2018 - 10:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks! This is what I got, I'm trying to understand all this. Please bear with me, Once it clicks I'll be golden. 

 

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\dna95\Documents\Arduino\flex_sensor_project_Base\flex_sensor_project_Base.ino: In function 'void loop()':

flex_sensor_project_Base:19: error: 'SerialPrint' was not declared in this scope

     SerialPrint ("old Flex :");

                              ^

flex_sensor_project_Base:20: error: 'odlFlex' was not declared in this scope

     Serial.print( odlFlex);

                   ^

flex_sensor_project_Base:22: error: 'SerialPrint' was not declared in this scope

  SerialPrint (" Flex :");

                        ^

exit status 1
'SerialPrint' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Dave E.

Last Edited: Mon. May 28, 2018 - 05:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I should have typed "Serial.print" instead of "SerialPrint"

and oldFlex instead of "odlFlex"

 

(I typed without checking, as I have no arduino on week days).

 

It is very difficult detecting one's own typos....

 

Edit : I added (line 4) a global variable oldFlex, and initialised it to a value it can never reach; thus, unless you validate with your button, analog reads  (0 .. 1024) are always above this value -999... It is only updated when your button is pressed...

Last Edited: Mon. May 28, 2018 - 06:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I Tried to go over everything but I don't understand the error "invalid use of Print::Print". And yeah it's not easy spotting syntax errors when you don't know the language. lol. Thank you so much for your help in this!

 

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\dna95\Documents\Arduino\flex_sensor_project_Base\flex_sensor_project_Base.ino: In function 'void loop()':

flex_sensor_project_Base:19: error: invalid use of 'Print::Print'

     Serial.Print ("oldFlex :");

            ^

flex_sensor_project_Base:22: error: invalid use of 'Print::Print'

   Serial.Print (" Flex :");

          ^

exit status 1
invalid use of 'Print::Print'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Dave E.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

C++, like many languages,  is case sensitive.

 

I hope  Serial.Print , when rewritten  (twice : only 'P' of print should change) as Serial.print, will solve the 2 remaining issues... (error message was the same).

And hope there is no logical error (program compiles, but does not do what you wanted)