Putting a code together !!

Hey guys

Am new into Arduino and still developing my programming skills, I have this thing am working on
am trying to use an ultrasonic sensor to detect once an object has been detected. Once the object is detected I then have an FSR sensor to detect if an object is there or not (weight ) so this is what am trying to do:
-The ultrasonic sensor detects object then checks the FSR status, if the Fsr voltage is more than or equal to 1300 mv then an object is detected (this is a threshold I chose) and if its less than 1300 then no object is detected. If the Fsr voltage is more than or equal to 1300 mv then an alarm would go off

-Ofc if the ultrasonic doesn’t detect anything then nothing happens (The ultrasonic will act as the system initiator)

The below code is for when the Fsr detects an object and gives an alarm

int fsrPin = A0;
int fsrVoltage; // the analog reading converted to voltage
int fsrReading; // the analog reading from the FSR resistor divider
int const buzzPin = 6;
void setup(void) {
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
fsrReading = analogRead(fsrPin);
Serial.println("Analog reading = ");
Serial.println(fsrReading);
// We’ll have a few threshholds, qualitatively determined
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print(“Voltage reading in mV = “);
Serial.println(fsrVoltage);
if (fsrVoltage >=1300)
{
Serial.println(“OBJECT DETECTED”);
digitalWrite(buzzPin, HIGH);
}
else {
Serial.println(” NO OBJECT DETECTED !!”);
digitalWrite(buzzPin, LOW);

}
delay(1000);
}

I also have the code for the ultrasonic sensor

*/
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;

void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}

void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 10 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don’t buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won’t hurt any one
delay(60);
}

NOW since am still a beginner to programming I would like to integrate the last ultrasonic sensor code to the first code to do the function mentioned at the beginning of the post but am not sure how to do so

Would appreciate any help
Thnx

I have this thing am working on am trying to use an ultrasonic sensor to detect once an object has been detected.

All the words are spelled correctly, but they don't make sense in that order.

-The ultrasonic sensor detects object then checks the FSR status

I'm almost certain that the ultrasonic sensor you are using can not check the FSR "status".

if the Fsr voltage is more than or equal to 1300 mv

The analogRead() function returns a value in the range 0 to 1024. Rather than convert that to volts, just determine what value means "present".

NOW since am still a beginner to programming I would like to integrate the last ultrasonic sensor code to the first code to do the function mentioned at the beginning of the post but am not sure how to do so

With a text editor, of course. If you KNOW what the two programs do, and you know what the resulting program should do, then picking and choosing is easy.

You have a setup() function. It should so some things. You have a loop() function. It should do some things. Determine which things that the two programs do, in setup(), that the new program should do in setup(). Determine which things that the two programs do, in loop(), that the new program should do in loop(). Then, make it so.

Read up about State Machines - it is a fancy name for a simple concept or using a variable to keep track of the state of your system

In your case the states might be 'I' (Idle), 'W' (Weighing), 'A' (Alarm)

Normally the state is IDLE. When the ultrasonic detector sees something change the state to WEIGHING and read the Fsr value. if the value is too low, go back to IDLE, it it is too high go to ALARM.

I don't think you has said what happens if the weight is OK

...R

Thnx alot for your reply
Basically the program needs to go like thiss:

if(Ultrasonic distance is the one required)
Then call function
else
Restart(go back to the top "Waiting for ultrasonic required distance to be detected )
End if

The function would be

function(FSR sensor)
read data on FSR
if data > 1300mv
then turn on buzzer
else do nothing

This is me justing imagining how it needs to flow i just cant turn it to code language since am still in a basic level

Moe_99:
This is me justing imagining how it needs to flow

Obviously a program written as a State Machine allows all the tasks to be done but there is a sense in which it is almost the opposite of "flow". It is a series of separate functions for different actions. Think how cars are made on a production line with each worker having a specific task.

Think of a series of functions each of which says to itself

  • If the state variable is X then I will execute, otherwise I will return without doing anything.
  • And if I execute and complete my task I will change the state variable to another value which means {A} I won't execute the next time around (because the value will be wrong for me) and {B} the value will be correct for another function to execute but I know nothing about those other functions

That way the different parts of the program can be tested separately as the only connection between them is the value in the State variable.

This is also a system that makes it easy to write code that does not always follow in a linear fashion.

...R